본문 바로가기

Codility/JAVA

TapeEquilibrium

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
// 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 total = 0;
        
        for (int i = 0; i < A.length; i++) {
            total += A[i];
        }
        
        int p=1;
        int left = A[0];
        int right = total - A[0];
        int min_res = Math.abs(left - right);
        
        if (A.length == 1return min_res;
        
        while(p < A.length-1) {
            left += A[p];
            right -= A[p];
            
            min_res = min_res > Math.abs(left - right) ?Math.abs(left - right) : min_res ;
            p++;
        }
        
        return min_res;
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

'Codility > JAVA' 카테고리의 다른 글

FlogJump  (0) 2020.02.16
OddOccurencesInArray  (0) 2020.02.15
Cyclic Rotation  (0) 2020.02.15
Binary Gap  (0) 2020.02.15