-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlengthOfLIS.go
More file actions
53 lines (44 loc) · 1.06 KB
/
Copy pathlengthOfLIS.go
File metadata and controls
53 lines (44 loc) · 1.06 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
42
43
44
45
46
47
48
49
50
51
52
53
/* https://leetcode.com/problems/longest-increasing-subsequence/description/
Given an unsorted array of integers, find the length of longest increasing subsequence.
For example,
Given [10, 9, 2, 5, 3, 7, 101, 18],
The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there may be more than one LIS combination, it is only necessary for you to return the length.
Your algorithm should run in O(n2) complexity.
Follow up: Could you improve it to O(n log n) time complexity?
*/
package ldp
import (
"sort"
)
func lengthOfLIS(nums []int) int {
tail, res := make([]int, len(nums)), 0
for _, n := range nums {
i := sort.SearchInts(tail[:res], n)
tail[i] = n
if i == res {
res++
}
}
return res
}
/*
func lengthOfLIS(nums []int) int {
max := func(a, b int) int {
if a > b {
return a
}
return b
}
dp, res := make([]int, len(nums)), 0
for i := 0; i < len(nums); i++ {
dp[i] = 1
for j := 0; j < i; j++ {
if nums[i] > nums[j] {
dp[i] = max(dp[i], dp[j]+1)
}
}
res = max(res, dp[i])
}
return res
}
*/