-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMain.java
More file actions
35 lines (28 loc) ยท 866 Bytes
/
Main.java
File metadata and controls
35 lines (28 loc) ยท 866 Bytes
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
package TimeComplexity.TapeEquilibrium;
public class Main {
public static void main(String[] args) {
Solution sol = new Solution();
System.out.println(sol.solution(new int[]{1, 2, 3}));
System.out.println(sol.solution(new int[]{3, 1, 2, 4, 3}));
}
}
class Solution {
static int N;
static int[] toRight, toLeft;
public int solution(int[] A) {
N = A.length;
toRight = new int[N];
toLeft = new int[N];
toRight[0] = A[0];
toLeft[N-1] = A[N-1];
for (int i = 1; i < N; i++) {
toRight[i] = toRight[i-1] + A[i];
toLeft[N-i-1] = toLeft[N-i] + A[N-i-1];
}
int ans = Integer.MAX_VALUE;
for (int p = 1; p < A.length; p++) {
ans = Math.min(ans, Math.abs(toRight[p-1] - toLeft[p]));
}
return ans;
}
}