// 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
        
        if (A.length < 3) {
            return 0;
        }
        Arrays.sort(A);
        
        int start = 0;
        while (start < A.length) {
            if (A[start] > 0) {
                break;
            }
            start++;
        }
        
        if (start >= A.length) {
            return 0;
        }
        
        int result = 0;
        for (int i = 0; i < A.length-1; i++) {
            for (int j = (i+1); j < A.length-1; j++) {
                
                if ((long)A[i] + (long)A[j] > (long)A[j+1]) {
                    result = 1;
                    break;
                }
            }
            if (result == 1) {
                break;
            }
        }
        
        return result;
    }
}

'알고리즘' 카테고리의 다른 글

[codility] lesson5 - PassingCars  (0) 2020.08.14
[codility] lesson4 - FrogRiverOne  (0) 2020.08.13
[codility] lesson3 - FrogJmp  (0) 2020.08.12
[codility] lesson2 - CyclicRotation  (0) 2020.08.12
[codility] Lesson1 - Binary Gap  (0) 2020.08.12
// 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
        
        if (A.length == 1) {
            return 0;
        }
        
        int start = 0;
        while (start < A.length) {
            if (A[start] == 0) {
                break;
            }
            start++;
        }
        
        if (start == A.length) {
            return 0;
        }
        
        int pCount = 0;
        int result = 0;
        for (int i = start; i < A.length; i++) {
            if (A[i] == 0) {
                pCount++;
            } else {
                result += pCount;
            }
            
            if (result > 1000000000) {
                result = -1;
                break;
            }
        }
            
        return result;
    }
}

'알고리즘' 카테고리의 다른 글

[codility] lesson6 - Triangle  (0) 2020.08.15
[codility] lesson4 - FrogRiverOne  (0) 2020.08.13
[codility] lesson3 - FrogJmp  (0) 2020.08.12
[codility] lesson2 - CyclicRotation  (0) 2020.08.12
[codility] Lesson1 - Binary Gap  (0) 2020.08.12
// 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 X, int[] A) {
        // write your code in Java SE 8
        
        int[] positions = new int[X];
        int result = -1;
        int count = 0;
        int i;
        for (i = 0; i < A.length; i++) {
            int idx = A[i] - 1;
            if (idx < X) {
                if ( positions[idx] == 0 ) {
                    positions[idx]++;
                    count++;
                }
            }
            
            if (count == X) {
                result = i;
                break;
            }
        }
        
        return result;
    }
    
}

 

'알고리즘' 카테고리의 다른 글

[codility] lesson6 - Triangle  (0) 2020.08.15
[codility] lesson5 - PassingCars  (0) 2020.08.14
[codility] lesson3 - FrogJmp  (0) 2020.08.12
[codility] lesson2 - CyclicRotation  (0) 2020.08.12
[codility] Lesson1 - Binary Gap  (0) 2020.08.12
// 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 static int solution(int X, int Y, int D) {
        // write your code in Java SE 8
        
        int diff = Y-X;
        if (diff == 0) {
            return diff;
        }
        
        int result = diff / D;
        if ((result * D + X) == Y) {
            return result;
        }
        return result + 1;
    }
}

'알고리즘' 카테고리의 다른 글

[codility] lesson5 - PassingCars  (0) 2020.08.14
[codility] lesson4 - FrogRiverOne  (0) 2020.08.13
[codility] lesson2 - CyclicRotation  (0) 2020.08.12
[codility] Lesson1 - Binary Gap  (0) 2020.08.12
[codility] lesson2 - OddOccurrencesInArray  (0) 2020.08.12
// 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, int K) {
        // write your code in Java SE 8
        
        int len = A.length;
        int[] result = new int[len];
        for (int i = 0; i < len; i++) {
            int movedIdx = (i + K) % len;
            result[movedIdx] = A[i];            
        }
        
        return result;
    }
}

'알고리즘' 카테고리의 다른 글

[codility] lesson5 - PassingCars  (0) 2020.08.14
[codility] lesson4 - FrogRiverOne  (0) 2020.08.13
[codility] lesson3 - FrogJmp  (0) 2020.08.12
[codility] Lesson1 - Binary Gap  (0) 2020.08.12
[codility] lesson2 - OddOccurrencesInArray  (0) 2020.08.12
// 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 N) {
        // write your code in Java SE 8
        
        char[] charArr = Integer.toBinaryString(N).toCharArray();
        int bg = 0;
        int maxBg = 0;
        int zeroCount = 0;
        int oneCount = 0;
        
        for (int i = 0; i < charArr.length; i++) {
            int el = charArr[i] - '0';
            if (el == 1) {
                bg = zeroCount;
                if (bg > maxBg) {
                    maxBg = bg;
                }
                oneCount = 1;
                zeroCount = 0;
            } else if (el == 0) {
                zeroCount++;
            }
        }
        
        return maxBg;
    }
}

'알고리즘' 카테고리의 다른 글

[codility] lesson5 - PassingCars  (0) 2020.08.14
[codility] lesson4 - FrogRiverOne  (0) 2020.08.13
[codility] lesson3 - FrogJmp  (0) 2020.08.12
[codility] lesson2 - CyclicRotation  (0) 2020.08.12
[codility] lesson2 - OddOccurrencesInArray  (0) 2020.08.12
// 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

        Arrays.sort(A);
        int result = 0;

        for (int i = 0; i < A.length; i+=2) {

        if (A[i] == A.length - 1 || A[i] != A[i+1] ) {
                result = A[i];
                break;
            }
        }

        return result;

    }

}

'알고리즘' 카테고리의 다른 글

[codility] lesson5 - PassingCars  (0) 2020.08.14
[codility] lesson4 - FrogRiverOne  (0) 2020.08.13
[codility] lesson3 - FrogJmp  (0) 2020.08.12
[codility] lesson2 - CyclicRotation  (0) 2020.08.12
[codility] Lesson1 - Binary Gap  (0) 2020.08.12

+ Recent posts