Skip to content

Commit a7c71b1

Browse files
committed
acwing: add 895_longest_Increasing_subsequence.go
1 parent 506afb4 commit a7c71b1

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package main
2+
3+
/*
4+
@author: CodeWater
5+
@since: 2025/03/05
6+
@desc: 898. 895. 最长上升子序列
7+
*/
8+
9+
import "fmt"
10+
11+
const N = 1010
12+
13+
var (
14+
n int
15+
// f[i]以 第i个数结尾的最长子序列
16+
a, f [N]int
17+
)
18+
19+
func max(a, b int) int {
20+
if a > b {
21+
return a
22+
}
23+
return b
24+
}
25+
26+
func main() {
27+
fmt.Scan(&n)
28+
for i := 1; i <= n; i++ {
29+
fmt.Scan(&a[i])
30+
}
31+
32+
for i := 1; i <= n; i++ {
33+
// 极端情况:只有以i为结尾的一个,就是i自己。
34+
f[i] = 1
35+
// 枚举i前面每一个值以j结尾的情况,所有情况的最大值
36+
for j := 0; j < i; j++ {
37+
// 前提是前面的值都必须小于i结尾的这个值,题目要求严格上升。
38+
if a[j] < a[i] {
39+
f[i] = max(f[i], f[j]+1)
40+
}
41+
}
42+
}
43+
44+
res := 0
45+
// 看所有数字结尾的情况是多少,取最大值。
46+
for i := 1; i <= n; i++ {
47+
res = max(res, f[i])
48+
}
49+
50+
fmt.Println(res)
51+
}

0 commit comments

Comments
 (0)