Skip to content

Commit

Permalink
0300.最长上升子序列-go动态规划求解
Browse files Browse the repository at this point in the history
  • Loading branch information
dxx99 committed Apr 28, 2022
1 parent 8e716b2 commit 5fd43cf
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions problems/0300.最长上升子序列.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,39 @@ func lengthOfLIS(nums []int ) int {
}
```

```go
// 动态规划求解
func lengthOfLIS(nums []int) int {
// dp数组的定义 dp[i]表示取第i个元素的时候,表示子序列的长度,其中包括 nums[i] 这个元素
dp := make([]int, len(nums))

// 初始化,所有的元素都应该初始化为1
for i := range dp {
dp[i] = 1
}

ans := dp[0]
for i := 1; i < len(nums); i++ {
for j := 0; j < i; j++ {
if nums[i] > nums[j] {
dp[i] = max(dp[i], dp[j] + 1)
}
}
if dp[i] > ans {
ans = dp[i]
}
}
return ans
}

func max(x, y int) int {
if x > y {
return x
}
return y
}
```

Javascript
```javascript
const lengthOfLIS = (nums) => {
Expand Down

0 comments on commit 5fd43cf

Please sign in to comment.