본문 바로가기

코딩테스트/codility

FrogRiverOne

https://app.codility.com/programmers/lessons/4-counting_elements/frog_river_one/

 

FrogRiverOne coding task - Learn to Code - Codility

Find the earliest time when a frog can jump to the other side of a river.

app.codility.com

 

영어 문제여서 역시 번역의 중요성을 느낌...

 

개구리가 0에서 X+1의 위치로 이동해야함.

그런데 0~X까지 나뭇잎으로 모두 덮여 있어야만 개구리가 이동할 수 있음.

A[K]에 저장된 값은 K초에 떨어진 나뭇잎의 위치임. A[0] = 1 이라면, 0초에 1의 위치에 나뭇잎이 떨어졌단 얘기.

 

풀어보자면,

class Solution {
    public int solution(int X, int[] A) {
        boolean[] check = new boolean[X + 1];

        int time = 0;
        for (int a : A) {
            if (!check[a]) {
                check[a] = true;
                X--;
                if (X == 0) return time;
            }
            time++;
        }

        return -1;
    }
}

 

'코딩테스트 > codility' 카테고리의 다른 글

PassingCars  (0) 2023.09.29
Distinct  (0) 2023.09.29
PermCheck  (0) 2023.09.29
TapeEquilibrium  (0) 2023.09.18
OddOccurrencesInArray  (0) 2023.09.16