태하팍 2025. 9. 11. 01:24
반응형

너무 간만에 문제를 풀어봅니다ㅋㅋ
온라인 직무테스트 링크를 보니 Codility가 있어서 찾아보니 아래처럼 
프로그래머스처럼 기업이 사용하는 코테 사이트가 있었습니다.

coding test site : https://app.codility.com/programmers/

특징으로는 영어로 문제가 나옵니다!

문제1)  A binary gap - Find longest sequence of zeros in binary representation of an integer.

// 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");
import java.util.*;
class Solution {
    public int solution(int N) {
        // 1. 양수 -> 이진법으로 변경 (=String으로? 나래비?)
        // 9 = 1001 9%2
        // 1041(=10000010001)
        int next = 0;
        List<Integer> binaryList = new ArrayList<>();
        next = N;
        while(next!=1){
            if(next%2 == 1){
                binaryList.addFirst(1);
            } else {
                binaryList.addFirst(0);
            }
            next = next/2;
        }

        if(next ==1) binaryList.addFirst(1);

        // 9 - binaryList(1)
        // 4 - binaryList(0,1)
        // 2 - binaryList(0,0,1)
        // 1 - binaryList(1,0,0,1)
        // 1041 -  binaryList(1)
        // 520 - binaryList(0,1)
        // 260 - binaryList(0,0,1)
        // 130 - binaryList(0,0,0,1)
        // 65 - binaryList(1,0,0,0,1)
        // 32 - binaryList(0,1,0,0,0,1)
        // 16 - binaryList(0,0,1,0,0,0,1)
        // 8 - binaryList(0,0,0,1,0,0,0,1)
        // 4 - binaryList(0,0,0,0,1,0,0,0,1)
        // 2 - binaryList(0,0,0,0,0,1,0,0,0,1)
        // 1 - binaryList(1,0,0,0,0,0,1,0,0,0,1)
        
        // 2. 1사이의 0의 갯수 카운트 
        // 1,0,0,0,0,0,1,0,0,0,1
         //1을 찾아야한다.
        //1과 1사이의 0의 갯수 
        Boolean isCnt = false;
        int check = 0;
        int resultZero = 0;
        int resultZeroCnt = 0;
        for(int i:binaryList) {
            if(i==1){
                isCnt = true;
                check++;
            }else {
                if(isCnt&&check==1) { // i == 1을 발견
                    resultZero++;
                }else {
                    // 초기화 
                    check = 1;
                    if(resultZeroCnt < resultZero) {  // 2.1 여러개면 그중 제일 큰 값 리턴, 없으면 0리턴
                        resultZeroCnt = resultZero;        
                    }
                    resultZero = 0;
                }
            }

        } 

        return resultZeroCnt;
    
    }
}

문제2) - 배열 오른쪽으로 K번 회전하는 문제

- for문 2개로 풀면..성능문제라고 뜬다 ㅋㅋ;;
 for문 하나로 하려면? 다 구해서 넣어야 합니다.
// 성능을 고려하려면 for가 2번 돌면 안된다. 한번돌려면? 다 구해서 넣어줘야 함! 
 // 4 - 2 , 3-1, 2-0, 1-4, 0-3
 // (i + K) % aLength
 // (0 + 3)%5 = 
 // 7%5 = 2 (0) , 6%5=1, 4%5 = 4 , 3%5 = 3

// 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) {
        // Implement your solution here
            // A = [3, 8, 9, 7, 6], K = 3
            //        9, 7, 6, 3, 8
            //     A = [1, 2, 3, 4], K = 4
            //          4 1 2 3  , 3 4 1 2 , 2 3 4 1 , 1 2 3 4
            // 0 1 2 3  4면 
            // 길이랑 K가 같으면 그대로 
            // 3번밀면 (x-3)+1 = 4
            /
        int aLength = A.length;
        System.out.println("------------->"+aLength);
        int [] results = new int[aLength];
        int [] temp = new int[aLength];
        for(int i=0; i < K; i++){
            if(i==0){
                temp = Arrays.copyOf(A, aLength);
            }else{
                System.out.println("here?");
                temp = Arrays.copyOf(results, aLength);
            }

            System.out.println("target data =>"+temp[aLength-1]);
            results[0]=temp[aLength-1];
            System.out.println("is good?"+results[0]);

            for(int j=0; j < aLength-1; j++) {
                results[j+1] = temp[j]; 
                System.out.println("rest data="+results[j+1]);

            }
            
        }
        return results;

    }
}

// 성능을 고려하려면 for가 2번 돌면 안된다. 한번돌려면? 다 구해서 넣어줘야 함! 
 // 4 - 2 , 3-1, 2-0, 1-4, 0-3
 // (i + K) % aLength
 // (0 + 3)%5 = 최종 
 // 7%5 = 2 (0) , 6%5=1, 4%5 = 4 , 3%5 = 3

FrogJmp

Count minimal number of jumps from position X to Y.

// you can also use imports, for example:
import java.util.*;
import java.math.*;

// 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 Y, int D) {
        // Implement your solution here
        // X : position X , Y 
        // D : jumps
        // X=10, Y=85, D=30
        // 10+30 = 40
        // 40+30=70
        // 70+30 = 100 
        // Jump 3 - return

        // x+y /d = 95/30 = 3
        double result = Math.ceil((X+Y)/D);
        System.out.println(result);
        // double to int
        return (int)result;

    }
}

PermMissingElem

Find the missing element in a given permutation.

// 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) {
        // Implement your solution here
        //1 + .. N+1 = 2+3+1+5 = 11
        // 11 - x = 4 ,x = 7 
        // N이 4 1+2+3+4+5 = 15
        // 11 
        // 15-11=4
        int n = A.length;
        int all = 0;
        int sum = 0;
        // n=4
        // 0,1,2,3
        for(int i=0; i < n; i++){
            all +=i+1; // 1,2,3,4
            sum += A[i];
        }

        all += n+1;

        return all - sum;
    }
}

 

// you can also use imports, for example:
import java.util.*;
import java.math.*;
// 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) {
        // Implement your solution here
        // 1) A[0],A[1]...A[P-1]
        // - 
        // 2) A[P], A[P+1]...A[N-1]
        // |왼쪽 - 오른쪽| = 최소값
        // 
        // absolute (sum)
        // P는?? 어디서? 
        // 모든 인덱스?
        
        long aSum = 0L;
        long pSum = 0L;
        long returnValue = Long.MAX_VALUE;
        for(int p=0; p < A.length; p++){ 
            pSum = 0L;
            aSum = 0L;
            // p = 0 , pSum A[0] = 3 /// aSum A[1] = 1, A[2] = 2, A[3] = 4, A[4] = 3
            // p = 1 , pSum A[0] = 3, A[1] = 1 //// aSum A[2] = 2, A[3] = 4, A[4] = 3
            // ....
            for(int i=0; i <= p; i++){
                pSum +=A[i];
            }
            for(int j=p+1; j < A.length; j++){
                aSum+=A[j];
            }

            System.out.println("pSum="+pSum);
            System.out.println("aSum="+aSum);

            long abcValue = Math.abs(pSum - aSum);

            System.out.println(abcValue);

            if(abcValue < returnValue) returnValue = abcValue;

        }

        return (int)returnValue;

    }
}

MissingInteger 

Find the smallest positive integer that does not occur in a given sequence.

// 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) {
        // Implement your solution here
        Arrays.sort(A);
        int resultValue = 1;
        for(int i : A) {
            if(i == resultValue) {
                resultValue++;
            } 
        }   
        return resultValue;
    }
}

 

// 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) {
        // Implement your solution here
        // Array A   0 : east / 1: west
        // The goal is to count passing cars
        // cars(P,Q) P : east, Q: west 
        // (0, 1), (0, 3), (0, 4), (2, 3), (2, 4)
        //   A[0] = 0 A[1] = 1 A[2] = 0 A[3] = 1 A[4] = 1
        // return 5 

        int eastCars = 0;
        long pairs = 0l;

        for(int car : A) {
            if(car == 0) {
                eastCars++;
            }else{ // car == 1 :west
                pairs +=eastCars;
                if(pairs > 1000000000) {
                    return -1;
                }
            }
        }

        return (int)pairs;


    }
}

 

// you can also use imports, for example:
import java.util.*;
import java.math.*;

// 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) {
        // Implement your solution here
        // A array..
        // triplet 
        // get maximal value
        // sort
        Arrays.sort(A);
        // maximal은 가장 작은수 2개 * 가장 큰수 음수*음수*양수 
        // or 가장 뒷부분 3개
        int arrayLen = A.length; 
        long maxValue1 = A[arrayLen-1] * A[arrayLen-2] * A[arrayLen-3];
        long maxValue2 = A[0]*A[1]*A[arrayLen-1];

        return (int) Math.max(maxValue1, maxValue2);

    }
}
// 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(String S) {
        // Implement your solution here
        //input String S {[()()]} 요런식으로 들어옴 
        // output 0 이냐 1이냐 
        //condition 올바르면 1 틀리면 0 리턴 
        // 스택? LIFO
        Stack<Character> stack = new Stack<>(); 
        for(char c:S.toCharArray()) {
            if(c == '(' || c == '[' || c == '{') {
                stack.push(c);
            }else{
                if(stack.isEmpty()) return 0;
                char top = stack.pop();
                if((c == ')' && top !='(') 
                || (c == ']' && top !='[')
                || (c == '}' && top !='{')) {
                    return 0;
                }

            }

        }

        return stack.isEmpty() ? 1:0;
    

    }
}

 

// 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) {
        // Implement your solution here
        // input A
        // output index or -1
        // condition half > 

        int size = 0;
        int candidate = 0;

        for(int i:A) {
            if(size == 0) {
                candidate = i;
                size++;
            }else if(i == candidate) {
                size++;
            } else {
                size--;
            }
        }

        System.out.println("candidate="+candidate);

        int dominatorCnt = 0;
        int index = 0;
        for(int c:A) {
            if(candidate == c) {
                dominatorCnt++;
            }
        }

        return (dominatorCnt > A.length/2) ? 0:-1;


    }
}

급하게..코딩테스트를 준비했는데..
아..개발 할 때 너무 IDE에 의존과 검색에 의존을 했나봅니다 ㅠㅠ
어떻게 해야하는지는 알겠는데..기억이 잘 나지 않네요..
175분 총 3문제였는데..

무튼..10년만에 본 코딩 테스트! ㅋㅋ 너~~~무 아쉽지만..그래도 재밌었다..

 

으악~너무 아쉽다!! ㅠ_ㅠ..

반응형