mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 04:06:51 +08:00
0300.最长上升子序列-go动态规划求解
This commit is contained in:
@ -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
|
||||||
```javascript
|
```javascript
|
||||||
const lengthOfLIS = (nums) => {
|
const lengthOfLIS = (nums) => {
|
||||||
|
Reference in New Issue
Block a user