Skip to content

Commit bd4d344

Browse files
committed
[Gold IV] Title: 가장 긴 증가하는 부분 수열 4, Time: 8 ms, Memory: 69108 KB -BaekjoonHub
1 parent 70b031b commit bd4d344

2 files changed

Lines changed: 20 additions & 32 deletions

File tree

백준/Gold/14002. 가장 긴 증가하는 부분 수열 4/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
### 제출 일자
1414

15-
2025년 7월 18일 17:01:08
15+
2025년 7월 18일 22:22:36
1616

1717
### 문제 설명
1818

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,33 @@
11
let n = Int(readLine()!)!
22
let s = readLine()!.split { $0 == " " }.map { Int(String($0))! }
33

4-
// dp[i]: s[i]를 마지막 원소로 하는 가장 긴 증가 부분 수열의 길이
5-
var dp = [Int](repeating: 1, count: n)
6-
// parent[i]: s[i] 앞에 오는 원소의 인덱스 (LIS 구성용)
7-
var parent = [Int](0..<n) // 초기화는 자기 자신을 가리키도록
4+
var parent = [Int](0..<s.count)
5+
var count = [Int](repeating: 1, count: s.count)
86

9-
var maxLength = 0
10-
var lastIndex = 0
7+
var maxLengthIndex = 0
8+
var maxLength = 1
119

12-
if n > 0 {
13-
maxLength = 1
14-
}
15-
16-
for i in 1..<n {
17-
for j in 0..<i {
18-
if s[i] > s[j] {
19-
if dp[i] < dp[j] + 1 {
20-
dp[i] = dp[j] + 1
21-
parent[i] = j // s[i] 앞에 s[j]가 옴
10+
for i in stride(from: 1, to: s.count, by: 1) {
11+
for j in stride(from: 0, to: i, by: 1) {
12+
if s[j] < s[i], count[j]+1 > count[i] {
13+
count[i] = count[j] + 1
14+
parent[i] = j
15+
if count[i] > maxLength {
16+
maxLength = count[i]
17+
maxLengthIndex = i
2218
}
2319
}
2420
}
25-
// 현재까지의 최대 길이와 해당 길이의 마지막 인덱스 갱신
26-
if dp[i] > maxLength {
27-
maxLength = dp[i]
28-
lastIndex = i
29-
}
3021
}
3122

32-
// LIS 구성
3323
var answer = [Int]()
34-
if maxLength > 0 { // LIS가 존재할 경우에만
35-
var currentIndex = lastIndex
36-
while dp[currentIndex] != 1 { // 길이가 1이 될 때까지 역추적
37-
answer.append(s[currentIndex])
38-
currentIndex = parent[currentIndex]
39-
}
40-
answer.append(s[currentIndex]) // 마지막으로 길이가 1인 원소 추가
41-
}
24+
var i = maxLengthIndex
4225

26+
while parent[i] != i {
27+
answer.append(s[i])
28+
i = parent[i]
29+
}
4330

31+
answer.append(s[i])
4432
print(maxLength)
45-
print(answer.reversed().map { "\($0)" }.joined(separator: " "))
33+
print(answer.reversed().map { "\($0)"}.joined(separator: " "))

0 commit comments

Comments
 (0)