Skip to content

Commit

Permalink
Merge branch 'master' of github.com:youngyangyang04/leetcode-master
Browse files Browse the repository at this point in the history
  • Loading branch information
youngyangyang04 committed Sep 14, 2024
2 parents 4651291 + 82c977b commit 25003d7
Show file tree
Hide file tree
Showing 18 changed files with 967 additions and 43 deletions.
Binary file modified .DS_Store
Binary file not shown.
42 changes: 42 additions & 0 deletions problems/0188.买卖股票的最佳时机IV.md
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,47 @@ func max188(a, b int) int {
}
```

版本三:空间优化版本

```go
func maxProfit(k int, prices []int) int {
n := len(prices)
// k次交易,2 * k种状态
// 状态从1开始计算,避免判断
// 奇数时持有(保持或买入)
// 偶数时不持有(保持或卖出)
dp := make([][]int, 2)
dp[0] = make([]int, k * 2 + 1)
dp[1] = make([]int, k * 2 + 1)

// 奇数状态时持有,i += 2
for i := 1; i <= k * 2; i += 2 {
dp[0][i] = -prices[0]
}

for i := 1; i < len(prices); i++ {
for j := 1; j <= k * 2; j++ {
if j % 2 == 1 {
dp[i % 2][j] = max(dp[(i - 1) % 2][j], dp[(i - 1) % 2][j - 1] - prices[i])
} else {
dp[i % 2][j] = max(dp[(i - 1) % 2][j], dp[(i - 1) % 2][j - 1] + prices[i])
}
}
}

return dp[(n - 1) % 2][k * 2]
}

func max(a, b int) int {
if a > b {
return a
}
return b
}
```



### JavaScript:

```javascript
Expand Down Expand Up @@ -558,3 +599,4 @@ impl Solution {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

37 changes: 37 additions & 0 deletions problems/0309.最佳买卖股票时机含冷冻期.md
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,42 @@ func max(a, b int) int {
}
```

```go
// 一维优化版本
// 时间复杂度O(n), 空间复杂度O(1)
func maxProfit(prices []int) int {

// 0: 持有,一直持有和买入
// 1: 不持有,一直不持有(不包含前一天卖出,因为这样的一天是冷静期,状态有区别)
// 2:不持有,今天卖出
// 3:冷静期,前一天卖出(一直不持有)
dp0, dp1, dp2, dp3 := -prices[0], 0, 0, 0

n := len(prices)

for i := 1; i < n; i++ {
t0 := max(dp0, max(dp1, dp3)-prices[i])
t1 := max(dp1, dp3)
t2 := dp0 + prices[i]
t3 := dp2

// 更新
dp0, dp1, dp2, dp3 = t0, t1, t2, t3
}

return max(dp1, max(dp2, dp3))
}

func max(a, b int) int {
if a > b {
return a
}
return b
}
```



### Javascript:

> 不同的状态定义 感觉更容易理解些
Expand Down Expand Up @@ -540,3 +576,4 @@ impl Solution {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

2 changes: 1 addition & 1 deletion problems/0383.赎金信.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ class Solution:

for x in ransomNote:
value = hashmap.get(x)
if not value or not value:
if not value:
return False
else:
hashmap[x] -= 1
Expand Down
30 changes: 29 additions & 1 deletion problems/0503.下一个更大元素II.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ class Solution {
```

### Python:
> 版本一:

```python
class Solution:
Expand All @@ -181,6 +182,34 @@ class Solution:
stack.append(i%len(nums))
return dp
```
> 版本二:针对版本一的优化
```python3
class Solution:
def nextGreaterElements(self, nums: List[int]) -> List[int]:
res = [-1] * len(nums)
stack = []
#第一次遍历nums
for i, num in enumerate(nums):
while stack and num > nums[stack[-1]]:
res[stack[-1]] = num
stack.pop()
stack.append(i)
#此时stack仍有剩余,有部分数‘无下一个更大元素’待修正
#第二次遍历nums
for num in nums:
#一旦stack为空,就表明所有数都有下一个更大元素,可以返回结果
if not stack:
return res
while stack and num > nums[stack[-1]]:
res[stack[-1]] = num
stack.pop()
#不要将已经有下一个更大元素的数加入栈,这样会重复赋值,只需对第一次遍历剩余的数再尝试寻找下一个更大元素即可
#最后仍有部分最大数无法有下一个更大元素,返回结果
return res
```

### Go:

```go
Expand All @@ -203,7 +232,6 @@ func nextGreaterElements(nums []int) []int {
return result
}
```

### JavaScript:

```JS
Expand Down
72 changes: 46 additions & 26 deletions problems/0701.二叉搜索树中的插入操作.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,32 +283,10 @@ class Solution:
return TreeNode(val)
self.traversal(root, val)
return root

```

递归法(版本二)
```python
class Solution:
def insertIntoBST(self, root, val):
if root is None:
return TreeNode(val)
parent = None
cur = root
while cur:
parent = cur
if val < cur.val:
cur = cur.left
else:
cur = cur.right
if val < parent.val:
parent.left = TreeNode(val)
else:
parent.right = TreeNode(val)
return root
```

递归法(版本三)
```python
class Solution:
def insertIntoBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
if root is None or root.val == val:
Expand All @@ -326,7 +304,7 @@ class Solution:
return root
```

递归法(版本四
递归法(版本三
```python
class Solution:
def insertIntoBST(self, root, val):
Expand All @@ -340,10 +318,9 @@ class Solution:
root.right = self.insertIntoBST(root.right, val)

return root

```

迭代法
迭代法(版本一)
```python
class Solution:
def insertIntoBST(self, root, val):
Expand All @@ -366,10 +343,53 @@ class Solution:
else:
parent.right = node # 将新节点连接到父节点的右子树

return root
```

迭代法(版本二)
```python
class Solution:
def insertIntoBST(self, root, val):
if root is None:
return TreeNode(val)
parent = None
cur = root
while cur:
parent = cur
if val < cur.val:
cur = cur.left
else:
cur = cur.right
if val < parent.val:
parent.left = TreeNode(val)
else:
parent.right = TreeNode(val)
return root
```

迭代法(精简)
```python
class Solution:
def insertIntoBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
if not root: # 如果根节点为空,创建新节点作为根节点并返回
return TreeNode(val)
cur = root
while cur:
if val < cur.val:
if not cur.left: # 如果此时父节点的左子树为空
cur.left = TreeNode(val) # 将新节点连接到父节点的左子树
return root
else:
cur = cur.left
elif val > cur.val:
if not cur.right: # 如果此时父节点的左子树为空
cur.right = TreeNode(val) # 将新节点连接到父节点的右子树
return root
else:
cur = cur.right


```

-----
### Go

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,20 @@ class Solution:
return max(dp[-1][0], dp[-1][1])
```

```python
class Solution:
def maxProfit(self, prices: List[int], fee: int) -> int:
# 持有股票手上的最大現金
hold = -prices[0] - fee
# 不持有股票手上的最大現金
not_hold = 0
for price in prices[1:]:
new_hold = max(hold, not_hold - price - fee)
new_not_hold = max(not_hold, hold + price)
hold, not_hold = new_hold, new_not_hold
return not_hold
```

### Go:

```go
Expand Down
39 changes: 39 additions & 0 deletions problems/1049.最后一块石头的重量II.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ class Solution:

```
### Go:

一维dp
```go
func lastStoneWeightII(stones []int) int {
// 15001 = 30 * 1000 /2 +1
Expand Down Expand Up @@ -341,6 +343,43 @@ func max(a, b int) int {
}
```

二维dp
```go
func lastStoneWeightII(stones []int) int {
sum := 0
for _, val := range stones {
sum += val
}
target := sum / 2

dp := make([][]int, len(stones))
for i := range dp {
dp[i] = make([]int, target + 1)
}
for j := stones[0]; j <= target; j++ {
dp[0][j] = stones[0]
}

for i := 1; i < len(stones); i++ {
for j := 0; j <= target; j++ {
if stones[i] > j {
dp[i][j] = dp[i-1][j]
} else {
dp[i][j] = max(dp[i-1][j], dp[i-1][j-stones[i]] + stones[i])
}
}
}
return (sum - dp[len(stones)-1][target]) - dp[len(stones)-1][target]
}

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

### JavaScript:

```javascript
Expand Down
Loading

0 comments on commit 25003d7

Please sign in to comment.