728x90
반응형
문제 설명
1. 간단한 DP 문제이다.
2. '0' 만 밟을 수 있다고 생각하면 편하다.
3. 현재 위치에서 1칸 2칸을 점프할 수 있다.
4. 그래서 마지막까지 몇번의 점프가 최소인지를 구하면 된다.
풀이 과정
1. 간단하다. 0부터 시작해서 마지막까지 가고, 지금 다음 구름에는 현재 위치에서 점프하는 때(현위치 값 + 1)가 최소인지 파악해서 넣어주면 된다.
2. 1은 그냥 넘기면 된다.
3. 끗
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static int jumpingOnClouds(List<Integer> c) { | |
int num = c.size(); | |
int jump[] = new int[num]; | |
Arrays.fill(jump, Integer.MAX_VALUE); | |
jump[0] = 0; | |
for(int i=0; i<num; i++) { | |
int index = c.get(i); | |
if (index == 1) { | |
continue; | |
} | |
if (i + 1 < num) { | |
jump[i+1] = Math.min(jump[i] +1, jump[i+1]); | |
} | |
if (i + 2 < num) { | |
jump[i+2] = Math.min(jump[i] +1, jump[i+2]); | |
} | |
} | |
return jump[num-1]; | |
} |
반응형
'알고리즘 공부' 카테고리의 다른 글
[HackerRank] 2d-array java 풀이 (1) | 2024.10.09 |
---|---|
[HackerRank] repeated-string java 풀이 (0) | 2024.10.08 |
[백준 5635번] 생일 - java (0) | 2023.02.03 |
[백준 2133번] 타일 채우기 - java (0) | 2022.05.15 |
[백준 14940번] 쉬운 최단거리 - java (0) | 2022.04.19 |