Note: study again. Good dp problem.
(nums []int)
| 4 | |
| 5 | // Note: study again. Good dp problem. |
| 6 | func 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 | |
| 35 | func lengthOfLISOther(nums []int) int { |
| 36 | if len(nums) == 0 { |
no outgoing calls