codility Lesson 풀어보기 2

Coding Test 준비/codility 2022. 6. 26. 17:29

Codiliy Lesson 풀어보기 Day2.

 - Day1(06.19)에 Lesson 2까지 전부 풀었으나, 작성한 글이 날아가서 Lesson2 Arrays 먼저 작성 후 Lesson3 Time Complexity 문제 풀이 진행

 - 아직은 Easy 위주로 진행.

 

 

Codility Lesson 2. Arrays - CyclicRotation

public int[] solution(int[] A, int K) {
    LinkedList<Integer> list = new LinkedList<>();
    boolean checkValue;

    //validation value
    checkValue = checkValue(A, K);
    if(!checkValue) {
        return A;
    }

    //int[] to LinkedList
    for(int a : A){
        list.add(a);
    }

    for(int i=0; i<K; i++) {
        list.addFirst(list.pollLast());
    }

    return list.stream().mapToInt(Integer::intValue).toArray();
}

private boolean checkValue(int[] a, int k) {
    if(a.length < 1) {
        return false;
    }

    if(k < 1 ) {
        return false;
    }
    return true;
}

 

Codility Lesson 2. Arrays - OddOccurencesInArray

public int solution(int[] A) {
    // write your code in Java SE 8
    Map<Integer, Integer> map = new HashMap<>();

    //make map
    for(int a : A) {
        map.put(a, map.getOrDefault(a, 0) + 1);
    }

    //find odd num
    for(int key : map.keySet()) {
        if(map.get(key)%2 == 1) return key;
    }
    return -1;
}

 

 

 

Codility Lesson 3. Time Complexity - CyclicRotation

public int solution(int X, int Y, int D) {
    // write your code in Java SE 8
    return (int)Math.ceil((double)(Y-X)/D);
}

 

https://app.codility.com/demo/results/trainingA5PMWS-AFH/

 

Test results - Codility

A small frog wants to get to the other side of the road. The frog is currently located at position X and wants to get to a position greater than or equal to Y. The small frog always jumps a fixed distance, D. Count the minimal number of jumps that the smal

app.codility.com

 

Codility Lesson 3. Time Complexity - PermMissingElem

public int solution(int[] A) {
    // write your code in Java SE 8
    int[] matchNum = new int[A.length+2];
    for(int i=0; i<A.length; i++){
        matchNum[A[i]] = 1;
    }
    for(int i=1; i<matchNum.length; i++){
        if(matchNum[i] == 0) return i;
    }

    return -1;
}

https://app.codility.com/demo/results/training626AX7-JTG/

 

Test results - Codility

An array A consisting of N different integers is given. The array contains integers in the range [1..(N + 1)], which means that exactly one element is missing. Your goal is to find that missing element. Write a function: class Solution { public int solutio

app.codility.com

 

 

'Coding Test 준비 > codility' 카테고리의 다른 글

codility Lesson 풀어보기 6  (0) 2022.08.20
codility Lesson 풀어보기 5  (0) 2022.08.15
codility Lesson 풀어보기 4  (0) 2022.08.06
codility Lesson 풀어보기 3  (0) 2022.06.29
codility Lesson 풀어보기  (0) 2022.06.19