MCPcopy Create free account
hub / github.com/austingebauer/go-leetcode / lengthOfLIS

Function lengthOfLIS

longest_increasing_subsequence_300/solution.go:6–33  ·  view source on GitHub ↗

Note: study again. Good dp problem.

(nums []int)

Source from the content-addressed store, hash-verified

4
5// Note: study again. Good dp problem.
6func lengthOfLIS(nums []int) int {
7 if len(nums) == 0 {
8 return 0
9 }
10
11 // dp[i] is equal to the length of the longest increasing
12 // subsequence possible considering elements from [0,i)
13 dp := make([]int, len(nums))
14 dp[0] = 1
15
16 maxResult := 1
17 for i := 0; i < len(nums); i++ {
18
19 // dp[i] = max(dp[j]) + 1, for j in [0,i), where nums[i] > nums[j]
20 jMax := 0
21 for j := 0; j < i; j++ {
22 if nums[j] < nums[i] {
23 jMax = int(math.Max(float64(jMax), float64(dp[j])))
24 }
25 }
26 dp[i] = jMax + 1
27
28 // set the final max result as we see new jMax
29 maxResult = int(math.Max(float64(maxResult), float64(dp[i])))
30 }
31
32 return maxResult
33}
34
35func lengthOfLISOther(nums []int) int {
36 if len(nums) == 0 {

Callers 1

Test_lengthOfLISFunction · 0.85

Calls

no outgoing calls

Tested by 1

Test_lengthOfLISFunction · 0.68