2016年10月17日 星期一

[Codility] Training ticket -- 100%

Score 83
效能不合格....
// 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 = A.length;
        
        for(int i = 0 ; i < l ;i++){
            int tmp = A[i];
            for(int j = i+1 ; j < l ;j++){
                
                if(A[j] == tmp){
                    i++;
                    A[j] += A[i];
                    A[i] = A[j] - A[i];
                    A[j] = A[j] - A[i];
                    l2--;
                }
                
            }
            
            
        }
        
        return l2;
    }
}

https://codility.com/demo/results/trainingYKSGPS-9KJ/


// 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;
        
        for(int i = 1 ; i < l ;i++){
            for(int j = 0 ; j < i ;j++){
                
                if(A[i] == A[j]){
                    A[j] = 1000001;
                }
                if(A[j] == 1000001){
                    A[j] = A[i];
                    A[i] = 1000001;
                    
                    if(j > 0){
                        A[j-1] += A[j];
                        A[j] = A[j-1] - A[j];
                        A[j-1] = A[j-1] - A[j];
                    }
                    
                    
                    j = i;
                }
                
            }
            
        }
        
        for(int i = 0 ; i < l ;i++){
            if(A[i] == 1000001)
                return i;
        }
        
        return l;
        
    }
}

https://codility.com/demo/results/trainingZKNY6F-3GA/
成績沒有改善

// 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 count = 0;
        
        for(int i = 0 ; i < l ; i++){
            if(A[i] != 1000001){
                count++;
                for(int j = i+1; j < l ; j++){
                    if(A[j] == A[i]){
                        A[j] = 1000001;
                        if(j == l-1)
                            l--;
                    }
                }
                A[i] = 1000001;
            }
        }
        
        return count;
    }
}

https://codility.com/demo/results/training2H2FYZ-FT7/
依然沒有改善


// 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 count = 0;
        int[] A2 = new int[2000001];
        
        for(int i = 0 ; i < l ; i++){
            int tmp = A[i] + 1000000;
            if(A2[tmp] == 0)
                count++;
            A2[tmp]++;
        }
        
        return count;
        
        
    }
}
https://codility.com/demo/results/trainingCXSQ2W-JFG/
其實資料長度只有100000,並不算大!
直接計算出現次數就可以輕鬆解決效能問題!並不會造成記憶體不足的問題!
跟青蛙過河有異曲同工之妙!

沒有留言:

張貼留言