codility Lesson 풀어보기 3

Coding Test 준비/codility 2022. 6. 29. 21:30

Codiliy Lesson 풀어보기 Day3.

 - 아직은 Easy 위주로 진행.

 

Codility Lesson 3. Time Complexity - TapeEquilibrium

public int solution(int[] A) {
    // write your code in Java SE 8
    int minNum = Integer.MAX_VALUE;
    int totalSum = 0, leftSum=0;

    for(int i : A)
        totalSum += i;

    for(int i=0; i<A.length-1; i++){
        leftSum += A[i];
        minNum = Math.min(minNum, Math.abs(leftSum - (totalSum-leftSum)));
    }

    return minNum;
}

 - for문 내에서 합계를 매번 구하도록 하면 타임아웃이 난다.

Detected time complexity :O(N)

 

https://app.codility.com/demo/results/trainingA9H573-HVU/

 

Test results - Codility

A non-empty array A consisting of N integers is given. Array A represents numbers on a tape. Any integer P, such that 0 < P < N, splits this tape into two non-empty parts: A[0], A[1], ..., A[P − 1] and A[P], A[P + 1], ..., A[N − 1]. The difference betw

app.codility.com

 

Codility Lesson 4. Counting Elements - FrogRiverOne

public int solution(int X, int[] A) {
    // write your code in Java SE 8
    int[] chkList = new int[X+1];
    for(int i=0, cnt=0; i<A.length; i++) {
        if(chkList[A[i]] != 1 && A[i]<= X){
            chkList[A[i]] = 1;
            cnt++;
        }
        if(cnt == X) return i;
    }
    return -1;
}

 - 모든 배열을 비교할 필요는 없고, X 까지 값 채워지는 순간까지만 비교하면 OK

 

https://app.codility.com/demo/results/trainingFU38QE-6B6/

'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 풀어보기 2  (0) 2022.06.26
codility Lesson 풀어보기  (0) 2022.06.19

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