Last active
August 30, 2019 08:38
-
-
Save xtea/1f0a47aa718b728fc0b0e190e7737184 to your computer and use it in GitHub Desktop.
TapeEquilibrium - Codility
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// you can also use imports, for example: | |
// import java.util.*; | |
// you can write to stdout for debugging purposes, e.g. | |
// System.out.println("this is a debug message"); | |
class Solution { | |
public int solution(int[] A) { | |
// write your code in Java SE 8 | |
int size = A.length; | |
int[] presum = new int[size]; | |
presum[size - 1] = A[size - 1]; | |
for (int i = A.length - 2; i >= 0; i --) { | |
presum[i] = presum[i + 1] + A[i]; | |
} | |
// | |
int ans = Integer.MAX_VALUE; | |
int leftSum = A[0]; | |
for (int i = 1; i < A.length; i ++) { | |
int diff = Math.abs(leftSum - presum[i]); | |
ans = Math.min(ans, diff); | |
leftSum += A[i]; | |
} | |
return ans; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment