Algorithm/Problem Solving

[Java] Codility - TapeEquilibrium 문제 풀이

JeongKyun 2022. 9. 5.

문제

문제 바로가기

 


 

Task Score

100%

 

Correctness

100%

 

Performance

100%

 


 

문제 풀이

class Solution {
    public int solution(int[] A) {

        int sumAllVal = 0;
        for(int cand : A)
            sumAllVal += cand;

        int minDiff = Integer.MAX_VALUE;
        int currentDiff = Integer.MAX_VALUE;
        int sumLeftVal = 0;
        int sumRightVal = 0;
        for (int i = 0; i < A.length - 1; i++) {
            sumLeftVal += A[i];
            sumRightVal = sumAllVal - sumLeftVal;
            currentDiff = Math.abs(sumLeftVal - sumRightVal);
            minDiff = Math.min(currentDiff, minDiff);
        }
        return minDiff;
    }
}
반응형

댓글

💲 많이 본 글