[백준 알고리즘] (DP) 11722번 Java 풀이
만약 이 문제 전에 11053번 문제를 안풀어봤다면 먼저 풀어보고 오기를 추천한다.
https://www.acmicpc.net/problem/11053
DP - 1463, 11726, 11727, 9095, 10844, 11057, 2193, 9465, 2156, 11053, 11055, 11722, 11054, 1912, 2579, 1699, 2133, 9461, 2225, 2011, 1105
출처: https://plzrun.tistory.com/entry/알고리즘-문제풀이PS-시작하기 [plzrun's algorithm]
<첫 번째 오답 풀이>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
package dp;
public class N11722 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int arr[] = new int[n+1];
for(int i=1; i<=n; i++) {
arr[i] = sc.nextInt();
}
int dp[] = new int[n+1];
for(int i=1; i<=n;i++) {
dp[i] = 1;
for(int j=1; j<i; j++) {
if(arr[i]<arr[j] && dp[i]>=dp[j]) {
dp[i]= dp[j]+1;
}
}
}
int max = 0;
for(int i=1; i<=n; i++) {
max = Math.max(dp[i], max);
}
System.out.println(max);
}
}
Colored by Color Scripter
|
11053번 문제와 똑같잖아! 하고 금방 풀었는데 오답을 받았다.
예제 케이스도 잘 만족하는데 뭐가 문제일까 찾아보다가 24번 라인 dp[i] >= dp[j]의 부등호 방향 문제라는 것을 알아차렸다.
예제 케이스에서는 다행히 dp[]값 업데이트가 순서대로 1, 2, 3, 4식으로 오름차순으로 있었기때문에 문제가 없었지만
별도로 문제를 확인하기 위해 만든 케이스 입력 {50, 40, 30, 42, 29}에서는 문제가 발생했다.

원래대로는 arr[5](배열이 1부터 시작한다고 가정)인 29에서 dp[5]는 4가 되어야 맞으나,
i=5, j=3에서 4로 업데이트 되었다가, 직후에 j++과 함께 i=5, j=4인 상황에서
dp[5]=dp[4]+1, 즉 3으로 업데이트가 되어버린다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
package dp;
public class N11722 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int arr[] = new int[n+1];
for(int i=1; i<=n; i++) {
arr[i] = sc.nextInt();
}
int dp[] = new int[n+1];
for(int i=1; i<=n;i++) {
dp[i] = 1;
for(int j=1; j<i; j++) {
if(arr[i]<arr[j] && dp[i]>=dp[j]) {
System.out.println(i+" "+j);
dp[i]= dp[j]+1;
}
}
}
int max = 0;
for(int i=1; i<=n; i++) {
max = Math.max(dp[i], max);
}
for(int i=1; i<=n; i++) {
System.out.println(dp[i]);
}
System.out.println(max);
}
}
Colored by Color Scripter
|
값의 변화를 tracking하기 위해, 24라인에 dp값 업데이트가 이뤄질때마다 i, j값을 출력하도록 하고,
34~36라인에서 전체 dp[]값을 출력하도록 했다.

역시나 dp[5]에서 3이 저장되어 있는 모습 확인.
이런 문제는 문제가 된 23라인 부등호 방향만 다시 바꿔주면 금방 해결된다.
<정답 풀이>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
package dp;
public class N11722 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int arr[] = new int[n+1];
for(int i=1; i<=n; i++) {
arr[i] = sc.nextInt();
}
int dp[] = new int[n+1];
for(int i=1; i<=n;i++) {
dp[i] = 1;
for(int j=1; j<i; j++) {
if(arr[i]<arr[j] && dp[i]<=dp[j]) {
dp[i]= dp[j]+1;
}
}
}
int max = 0;
for(int i=1; i<=n; i++) {
max = Math.max(dp[i], max);
}
System.out.println(max);
}
}
Colored by Color Scripter
|
부등호 방향만 바꿔주고 바로 정답 결과를 확인할 수 있었다.
Github: https://github.com/jaeuk9407/AlgorithmBOJ
jaeuk9407/AlgorithmBOJ
BaekJoon Online Judge Problems. Contribute to jaeuk9407/AlgorithmBOJ development by creating an account on GitHub.
github.com