-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday3_equal_stacks.java
More file actions
97 lines (76 loc) · 2.66 KB
/
day3_equal_stacks.java
File metadata and controls
97 lines (76 loc) · 2.66 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;
public class Solution {
/*
* Complete the equalStacks function below.
*/
static int equalStacks(int[] h1, int[] h2, int[] h3) {
int sum1=0, sum2=0, sum3=0;
int end = Math.max(Math.max(h1.length, h2.length), h3.length);
for(int i=0; i<end; i++) {
if(i<h1.length) {
sum1 += h1[i];
}
if(i<h2.length) {
sum2 += h2[i];
}
if(i<h3.length) {
sum3 += h3[i];
}
}
boolean flag = true;
int min = 0;
int i=0,j=0,k=0;
while(flag) {
min = Math.min(Math.min(sum1, sum2), sum3);
if(sum1 == min && sum2 == min && sum3 == min) {
flag = false;
break;
} else {
if(sum1 > min) {
sum1 -= h1[i++];
}
if(sum2 > min) {
sum2 -= h2[j++];
}
if(sum3 > min) {
sum3 -= h3[k++];
}
}
}
return sum1;
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
String[] n1N2N3 = scanner.nextLine().split(" ");
int n1 = Integer.parseInt(n1N2N3[0].trim());
int n2 = Integer.parseInt(n1N2N3[1].trim());
int n3 = Integer.parseInt(n1N2N3[2].trim());
int[] h1 = new int[n1];
String[] h1Items = scanner.nextLine().split(" ");
for (int h1Itr = 0; h1Itr < n1; h1Itr++) {
int h1Item = Integer.parseInt(h1Items[h1Itr].trim());
h1[h1Itr] = h1Item;
}
int[] h2 = new int[n2];
String[] h2Items = scanner.nextLine().split(" ");
for (int h2Itr = 0; h2Itr < n2; h2Itr++) {
int h2Item = Integer.parseInt(h2Items[h2Itr].trim());
h2[h2Itr] = h2Item;
}
int[] h3 = new int[n3];
String[] h3Items = scanner.nextLine().split(" ");
for (int h3Itr = 0; h3Itr < n3; h3Itr++) {
int h3Item = Integer.parseInt(h3Items[h3Itr].trim());
h3[h3Itr] = h3Item;
}
int result = equalStacks(h1, h2, h3);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedWriter.close();
}
}