2016年9月29日 星期四

[Codility] TapeEquilibrium

// 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 l = A.length;
        int max = 0;
        for(int i = l -1 ; i >= 0 ; i-- ){
            max += A[i];
            A[i] = max;
        }
        
        int tmp = A[0];
        for(int i = 0 ; i < l-1 ; i++ ){
            A[i] = tmp - (A[i+1] * 2);
            if(A[i] < 0)
            A[i] = -A[i];
        }
        
        int min = A[0];
        int ans = 0;
        for(int i = 1 ; i < l-1 ; i++){
            if(A[i] < min){
                min = A[i];
                ans = i;
            }
        }
        
        return min;
    }
}
Analysis summary


https://codility.com/demo/results/trainingUNYT7U-S2M/

沒有留言:

張貼留言