Skip to content

[1주차] 배수빈 #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Sep 14, 2024
Prev Previous commit
Next Next commit
배수빈: [PG] 154539 뒤에있는 큰 수 찾기_240913
  • Loading branch information
baexxbin committed Sep 13, 2024
commit db902e43d1491c1c1f9ebb9b0a006a7a0bb1a642
32 changes: 32 additions & 0 deletions Programmers/Level2/SB_154539_뒤에있는_큰_수_찾기.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package Programmers.Level2;

import java.util.Arrays;

public class SB_154539_뒤에있는_큰_수_찾기 {
public static int[] solution(int[] numbers) {
int N = numbers.length;
int[] dp = new int[N];
dp[N-1] = -1;

// 거꾸로 채워가기
for (int i = N - 2; i >= 0; i--) {
if (numbers[i+1] > numbers[i]) { // i 바로 뒤에 수가 크면 넣기
dp[i] = numbers[i + 1];
continue;
}

int k = 1;
while (i+k<N-1 && dp[i+k] <= numbers[i]) { // 아닐경우, dp값 돌면서 제일 처음만나는 큰 값 넣기
if (dp[i+k] == -1) break; // -1이면 더이상 없는 것으로 탈출
k++;
}
dp[i] = dp[i+k];
}
return dp;

}
public static void main(String[] args) {
int[] numbers = {2, 3, 3, 5};
System.out.println(Arrays.toString(solution(numbers)));
}
}