forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path4.java
More file actions
41 lines (33 loc) Β· 1.13 KB
/
4.java
File metadata and controls
41 lines (33 loc) Β· 1.13 KB
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
import java.util.*;
public class Main {
static int n;
static ArrayList<Integer> v = new ArrayList<Integer>();
static int[] dp = new int[2000];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
for (int i = 0; i < n; i++) {
v.add(sc.nextInt());
}
// μμλ₯Ό λ€μ§μ΄ 'μ΅μ₯ μ¦κ° λΆλΆ μμ΄' λ¬Έμ λ‘ λ³ν
Collections.reverse(v);
// λ€μ΄λλ―Ή νλ‘κ·Έλλ°μ μν 1μ°¨μ DP ν
μ΄λΈ μ΄κΈ°ν
for (int i = 0; i < n; i++) {
dp[i] = 1;
}
// κ°μ₯ κΈ΄ μ¦κ°νλ λΆλΆ μμ΄(LIS) μκ³ λ¦¬μ¦ μν
for (int i = 1; i < n; i++) {
for (int j = 0; j < i; j++) {
if (v.get(j) < v.get(i)) {
dp[i] = Math.max(dp[i], dp[j] + 1);
}
}
}
// μ΄μΈν΄μΌ νλ λ³μ¬μ μ΅μ μλ₯Ό μΆλ ₯
int maxValue = 0;
for (int i = 0; i < n; i++) {
maxValue = Math.max(maxValue, dp[i]);
}
System.out.println(n - maxValue);
}
}