2016年9月29日 星期四

MaxProductOfThree

// 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[] max = new int[3];
        int[] min = new int[2];
        int l = A.length;
        for(int i = 0 ; i < l ; i++){
            int tmp = A[i];
            for(int j = 0 ; j < 3; j++){
                if(max[j] < tmp){
                    tmp += max[j];
                    max[j] = tmp - max[j];
                    tmp = tmp - max[j];
                }
            }
            for(int j = 0 ; j < 2; j++){
                if(min[j] > A[i]){
                    min[j] = A[i];
                    j = 2;
                }
            }
            
            
        }
        
        int[] ms = new int[8];
        ms[0] = max[0] * max[1] * max[2];
        ms[1] = max[0] * min[0] * min[1];
        ms[2] = max[1] * min[0] * min[1];
        ms[3] = max[2] * min[0] * min[1];
        ms[4] = max[0] * max[1] * min[0];
        ms[5] = max[0] * max[1] * min[1];
        ms[6] = max[2] * max[1] * min[0];
        ms[7] = max[2] * max[1] * min[1];
        
        
        for(int i = 1; i < 8; i++){
            if(ms[0] < ms[i])
            ms[0] = ms[i];
        }
        
        return ms[0];
        
        
    }
}

https://codility.com/demo/results/trainingK9CV5K-5XF/


// 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 l2 = 1 + (l/2);
        for(int a = 0 ; a < l2 ; a++){
            for(int i = a ; i < l-a ; i++){
                
                if(A[i] > A[l - a -1]){
                    A[i] += A[l -a -1];
                    A[l -a -1] = A[i] - A[l -a -1];
                    A[i] = A[i] - A[l -a -1];
                    
                }
                if(A[i] < A[a]){
                    A[i] += A[a];
                    A[a] = A[i] - A[a];
                    A[i] = A[i] - A[a];
                    
                }
                
            }
        }
        
        
        int p = 0;
        int n = 0;
        int z = 0;
        
        for(int i = 0 ; i < l ; i++){
            if(A[i] > 0)p++;
            else if(A[i] < 0)n++;
            else z++;
        }
        
        int m1 = A[l-1] * A[l-2] * A[l-3];
        int m2 = A[l-1] * A[0] * A[1];
        if(m1 > m2)
        return m1;
        
        return m2;
        
    }
}

https://codility.com/demo/results/trainingR97YQN-JBK/


[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/