-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathReverseMin.java
More file actions
60 lines (56 loc) · 2.21 KB
/
ReverseMin.java
File metadata and controls
60 lines (56 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import java.io.IOException;
import java.util.Arrays;
public class ReverseMin {
private static final int MIN_ARRAY_SIZE = 8;
private static int min(int a, int b) {
return a < b ? a : b;
}
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in);
int[] lineMins = new int[MIN_ARRAY_SIZE];
int[] columnMins = new int[MIN_ARRAY_SIZE];
int[] widths = new int[MIN_ARRAY_SIZE];
Arrays.fill(lineMins, Integer.MAX_VALUE);
Arrays.fill(widths, Integer.MAX_VALUE);
Arrays.fill(columnMins, Integer.MAX_VALUE);
int height = 0;
int maxWidth = 0;
while (in.hasNextLine()) {
// System.err.println("New line");
Scanner lineScanner = new Scanner(in.nextLine());
int width = 0;
while (lineScanner.hasNextInt()) {
// System.err.println("New int");
width++;
if (width >= columnMins.length) {
columnMins = Arrays.copyOf(columnMins, columnMins.length * 2);
Arrays.fill(columnMins, columnMins.length / 2, columnMins.length, Integer.MAX_VALUE);
}
int curr = lineScanner.nextInt();
columnMins[width - 1] = Integer.min(curr, columnMins[width - 1]);
lineMins[height] = Integer.min(curr, lineMins[height]);
}
if (height >= widths.length) {
widths = Arrays.copyOf(widths, widths.length * 2);
}
if (width > maxWidth) {
maxWidth = width;
}
widths[height] = width;
height++;
if (height >= lineMins.length) {
lineMins = Arrays.copyOf(lineMins, lineMins.length * 2);
Arrays.fill(lineMins, lineMins.length / 2, lineMins.length, Integer.MAX_VALUE);
}
lineScanner.close();
}
in.close();
// g = Arrays.copyOf(g, height);
for (int i = 0; i < height; i++) {
for (int j = 0; j < widths[i]; j++) {
System.out.print(min(lineMins[i], columnMins[j]) + " ");
}
System.out.println();
}
}
}