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

codility Lesson 풀어보기

Coding Test 준비/codility 2022. 6. 19. 14:30

이미 굳어버린 머리에 산소호흡 시켜보고자 강의 수강외에 코딩 테스트도 추가로 풀기로 함.

첫 시작은 codility Lesson 부터 시작 (06.19)

 

Codility Lesson 1. binary Gap

// 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
        String binaryCode;
		int maxGap;
		boolean checkValue;

		//get binary code
		binaryCode = getBinaryCode(N);

		//validate binaryCode
		checkValue = validateBinaryCode(binaryCode);

		//validation failed string return without calculation
		if(!checkValue) return 0;

		//get max gap value
		maxGap = getMaxGap(binaryCode);

		return maxGap;
	}

	private static int getMaxGap(String binaryCode) {
		int gapNum=0, maxGap=0;
		for(int i=0; i<binaryCode.length(); i++) {
			if (binaryCode.charAt(i) == '0') {
				gapNum++;
			} else {
				if (gapNum > maxGap) {
					//update max Gap Num
					maxGap = gapNum;
				}
				//init gapNum count
				gapNum = 0;
			}
		}
		return maxGap;
	}

	private static boolean validateBinaryCode(String binaryCode) {
		//0, 1, 00, 01, 10, 10 is no gap
		if(binaryCode.length()<2) return false;

		//check string has only '1' value
		if (binaryCode.indexOf('0')<0) return false;

		//check string has only one '1' value
		if(binaryCode.indexOf('1') == binaryCode.lastIndexOf('1')) return false;

		//add additional validation code

		return true;
	}

	private static String getBinaryCode(int n) {
		return Integer.toBinaryString(n);
	}
}

 

 

결과 : 

 

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