Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from youngyangyang04:master #269

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 6 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
May 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update
  • Loading branch information
youngyangyang04 committed May 20, 2023
commit 16f4a48bd6b0539c60eed95c4f2c54a052685386
2 changes: 1 addition & 1 deletion problems/0053.最大子序和.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public:

## 动态规划

当然本题还可以用动态规划来做,当前[「代码随想录」](https://img-blog.csdnimg.cn/20201124161234338.png)主要讲解贪心系列,后续到动态规划系列的时候会详细讲解本题的 dp 方法。
当然本题还可以用动态规划来做,在代码随想录动态规划章节我会详细介绍,如果大家想在想看,可以直接跳转:[动态规划版本详解](https://programmercarl.com/0053.%E6%9C%80%E5%A4%A7%E5%AD%90%E5%BA%8F%E5%92%8C%EF%BC%88%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%EF%BC%89.html#%E6%80%9D%E8%B7%AF)

那么先给出我的 dp 代码如下,有时间的录友可以提前做一做:

Expand Down
17 changes: 11 additions & 6 deletions problems/0121.买卖股票的最佳时机.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,21 @@

返回你可以从这笔交易中获取的最大利润。如果你不能获取任何利润,返回 0 。

示例 1:
输入:[7,1,5,3,6,4]
输出:5
* 示例 1:
* 输入:[7,1,5,3,6,4]
* 输出:5
解释:在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格;同时,你不能在买入前卖出股票。

示例 2:
输入:prices = [7,6,4,3,1]
输出:0
* 示例 2:
* 输入:prices = [7,6,4,3,1]
* 输出:0
解释:在这种情况下, 没有交易完成, 所以最大利润为 0。

# 算法公开课

**《代码随想录》算法视频公开课:[动态规划之 LeetCode:121.买卖股票的最佳时机1](https://www.bilibili.com/video/BV1Xe4y1u77q),相信结合视频再看本篇题解,更有助于大家对本题的理解**。



## 思路

Expand Down
2 changes: 1 addition & 1 deletion problems/0122.买卖股票的最佳时机II.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public:

### 动态规划

动态规划将在下一个系列详细讲解,本题解先给出我的 C++代码(带详细注释),感兴趣的同学可以自己先学习一下。
动态规划将在下一个系列详细讲解,本题解先给出我的 C++代码(带详细注释),想先学习的话,可以看本篇:[122.买卖股票的最佳时机II(动态规划)](https://programmercarl.com/0122.%E4%B9%B0%E5%8D%96%E8%82%A1%E7%A5%A8%E7%9A%84%E6%9C%80%E4%BD%B3%E6%97%B6%E6%9C%BAII%EF%BC%88%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%EF%BC%89.html#%E6%80%9D%E8%B7%AF)

```CPP
class Solution {
Expand Down
23 changes: 14 additions & 9 deletions problems/0122.买卖股票的最佳时机II(动态规划).md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,30 @@
注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。


示例 1:
输入: [7,1,5,3,6,4]
输出: 7
* 示例 1:
* 输入: [7,1,5,3,6,4]
* 输出: 7
解释: 在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4。随后,在第 4 天(股票价格 = 3)的时候买入,在第 5 天(股票价格 = 6)的时候卖出, 这笔交易所能获得利润 = 6-3 = 3 。

示例 2:
输入: [1,2,3,4,5]
输出: 4
* 示例 2:
* 输入: [1,2,3,4,5]
* 输出: 4
解释: 在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。注意你不能在第 1 天和第 2 天接连购买股票,之后再将它们卖出。因为这样属于同时参与了多笔交易,你必须在再次购买前出售掉之前的股票。

示例 3:
输入: [7,6,4,3,1]
输出: 0
* 示例 3:
* 输入: [7,6,4,3,1]
* 输出: 0
解释: 在这种情况下, 没有交易完成, 所以最大利润为 0。

提示:
* 1 <= prices.length <= 3 * 10 ^ 4
* 0 <= prices[i] <= 10 ^ 4

# 算法公开课

**《代码随想录》算法视频公开课:[动态规划,股票问题第二弹 | LeetCode:122.买卖股票的最佳时机II](https://www.bilibili.com/video/BV1D24y1Q7Ls),相信结合视频再看本篇题解,更有助于大家对本题的理解**。


## 思路

本题我们在讲解贪心专题的时候就已经讲解过了[贪心算法:买卖股票的最佳时机II](https://programmercarl.com/0122.买卖股票的最佳时机II.html),只不过没有深入讲解动态规划的解法,那么这次我们再好好分析一下动规的解法。
Expand Down
27 changes: 16 additions & 11 deletions problems/0123.买卖股票的最佳时机III.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,35 @@

注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。

示例 1:
输入:prices = [3,3,5,0,0,3,1,4]
输出:6
* 示例 1:
* 输入:prices = [3,3,5,0,0,3,1,4]
* 输出:6
解释:在第 4 天(股票价格 = 0)的时候买入,在第 6 天(股票价格 = 3)的时候卖出,这笔交易所能获得利润 = 3-0 = 3 。随后,在第 7 天(股票价格 = 1)的时候买入,在第 8 天 (股票价格 = 4)的时候卖出,这笔交易所能获得利润 = 4-1 = 3。

示例 2:
输入:prices = [1,2,3,4,5]
输出:4
* 示例 2:
* 输入:prices = [1,2,3,4,5]
* 输出:4
解释:在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4。注意你不能在第 1 天和第 2 天接连购买股票,之后再将它们卖出。因为这样属于同时参与了多笔交易,你必须在再次购买前出售掉之前的股票。

示例 3:
输入:prices = [7,6,4,3,1]
输出:0
* 示例 3:
* 输入:prices = [7,6,4,3,1]
* 输出:0
解释:在这个情况下, 没有交易完成, 所以最大利润为0。

示例 4:
输入:prices = [1]
* 示例 4:
* 输入:prices = [1]
输出:0

提示:

* 1 <= prices.length <= 10^5
* 0 <= prices[i] <= 10^5

# 算法公开课

**《代码随想录》算法视频公开课:[动态规划,股票至多买卖两次,怎么求? | LeetCode:123.买卖股票最佳时机III](https://www.bilibili.com/video/BV1WG411K7AR),相信结合视频再看本篇题解,更有助于大家对本题的理解**。


## 思路


Expand Down
17 changes: 11 additions & 6 deletions problems/0188.买卖股票的最佳时机IV.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@

注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。

示例 1:
输入:k = 2, prices = [2,4,1]
输出:2
* 示例 1:
* 输入:k = 2, prices = [2,4,1]
* 输出:2
解释:在第 1 天 (股票价格 = 2) 的时候买入,在第 2 天 (股票价格 = 4) 的时候卖出,这笔交易所能获得利润 = 4-2 = 2。

示例 2:
输入:k = 2, prices = [3,2,6,5,0,3]
输出:7
* 示例 2:
* 输入:k = 2, prices = [3,2,6,5,0,3]
* 输出:7
解释:在第 2 天 (股票价格 = 2) 的时候买入,在第 3 天 (股票价格 = 6) 的时候卖出, 这笔交易所能获得利润 = 6-2 = 4。随后,在第 5 天 (股票价格 = 0) 的时候买入,在第 6 天 (股票价格 = 3) 的时候卖出, 这笔交易所能获得利润 = 3-0 = 3 。


Expand All @@ -31,6 +31,11 @@
* 0 <= prices.length <= 1000
* 0 <= prices[i] <= 1000

# 算法公开课

**《代码随想录》算法视频公开课:[动态规划来决定最佳时机,至多可以买卖K次!| LeetCode:188.买卖股票最佳时机4](https://www.bilibili.com/video/BV16M411U7XJ),相信结合视频再看本篇题解,更有助于大家对本题的理解**。


## 思路

这道题目可以说是[动态规划:123.买卖股票的最佳时机III](https://programmercarl.com/0123.买卖股票的最佳时机III.html)的进阶版,这里要求至多有k次交易。
Expand Down
4 changes: 4 additions & 0 deletions problems/0198.打家劫舍.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
* 0 <= nums.length <= 100
* 0 <= nums[i] <= 400

# 算法公开课

**《代码随想录》算法视频公开课:[动态规划,偷不偷这个房间呢?| LeetCode:198.打家劫舍](https://www.bilibili.com/video/BV1Te411N7SX),相信结合视频再看本篇题解,更有助于大家对本题的理解**。


## 思路

Expand Down
25 changes: 15 additions & 10 deletions problems/0213.打家劫舍II.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,28 @@

示例 1:

输入:nums = [2,3,2]
输出:3
解释:你不能先偷窃 1 号房屋(金额 = 2),然后偷窃 3 号房屋(金额 = 2), 因为他们是相邻的。
* 输入:nums = [2,3,2]
* 输出:3
* 解释:你不能先偷窃 1 号房屋(金额 = 2),然后偷窃 3 号房屋(金额 = 2), 因为他们是相邻的。

示例 2:
输入:nums = [1,2,3,1]
输出:4
解释:你可以先偷窃 1 号房屋(金额 = 1),然后偷窃 3 号房屋(金额 = 3)。偷窃到的最高金额 = 1 + 3 = 4 。
* 示例 2:
* 输入:nums = [1,2,3,1]
* 输出:4
* 解释:你可以先偷窃 1 号房屋(金额 = 1),然后偷窃 3 号房屋(金额 = 3)。偷窃到的最高金额 = 1 + 3 = 4 。

示例 3:
输入:nums = [0]
输出:0
* 示例 3:
* 输入:nums = [0]
* 输出:0

提示:
* 1 <= nums.length <= 100
* 0 <= nums[i] <= 1000

# 算法公开课

**《代码随想录》算法视频公开课:[动态规划,房间连成环了那还偷不偷呢?| LeetCode:213.打家劫舍II](https://www.bilibili.com/video/BV1oM411B7xq),相信结合视频再看本篇题解,更有助于大家对本题的理解**。


## 思路

这道题目和[198.打家劫舍](https://programmercarl.com/0198.打家劫舍.html)是差不多的,唯一区别就是成环了。
Expand Down
4 changes: 4 additions & 0 deletions problems/0309.最佳买卖股票时机含冷冻期.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
* 输出: 3
* 解释: 对应的交易状态为: [买入, 卖出, 冷冻期, 买入, 卖出]

# 算法公开课

**《代码随想录》算法视频公开课:[动态规划来决定最佳时机,这次有冷冻期!| LeetCode:309.买卖股票的最佳时机含冷冻期](https://www.bilibili.com/video/BV1rP4y1D7ku),相信结合视频再看本篇题解,更有助于大家对本题的理解**。


## 思路

Expand Down
5 changes: 5 additions & 0 deletions problems/0337.打家劫舍III.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@

![337.打家劫舍III](https://code-thinking-1253855093.file.myqcloud.com/pics/20210223173849619.png)

# 算法公开课

**《代码随想录》算法视频公开课:[动态规划,房间连成树了,偷不偷呢?| LeetCode:337.打家劫舍3](https://www.bilibili.com/video/BV1H24y1Q7sY),相信结合视频再看本篇题解,更有助于大家对本题的理解**。


## 思路

这道题目和 [198.打家劫舍](https://programmercarl.com/0198.打家劫舍.html),[213.打家劫舍II](https://programmercarl.com/0213.打家劫舍II.html)也是如出一辙,只不过这个换成了树。
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
* 0 < prices[i] < 50000.
* 0 <= fee < 50000.

# 算法公开课

**《代码随想录》算法视频公开课:[动态规划来决定最佳时机,这次含手续费!| LeetCode:714.买卖股票的最佳时机含手续费](https://www.bilibili.com/video/BV1z44y1Z7UR),相信结合视频再看本篇题解,更有助于大家对本题的理解**。


## 思路

本题贪心解法:[贪心算法:买卖股票的最佳时机含手续费](https://programmercarl.com/0714.买卖股票的最佳时机含手续费.html)
Expand Down
23 changes: 0 additions & 23 deletions problems/1005.K次取反后最大化的数组和.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -130,29 +130,6 @@ class Solution {
}
```

```java
class Solution {
public int largestSumAfterKNegations(int[] A, int K) {
if (A.length == 1) return k % 2 == 0 ? A[0] : -A[0];
Arrays.sort(A);
int sum = 0;
int idx = 0;
for (int i = 0; i < K; i++) {
if (i < A.length - 1 && A[idx] < 0) {
A[idx] = -A[idx];
if (A[idx] >= Math.abs(A[idx + 1])) idx++;
continue;
}
A[idx] = -A[idx];
}

for (int i = 0; i < A.length; i++) {
sum += A[i];
}
return sum;
}
}
```

### Python
```python
Expand Down
5 changes: 5 additions & 0 deletions problems/动态规划理论基础.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@

<img src='https://code-thinking.cdn.bcebos.com/pics/动态规划-总结大纲1.jpg' width=600> </img>

## 算法公开课

**《代码随想录》算法视频公开课:[动态规划理论基础](https://www.bilibili.com/video/BV13Q4y197Wg),相信结合视频再看本篇题解,更有助于大家对本题的理解**。


## 什么是动态规划

动态规划,英文:Dynamic Programming,简称DP,如果某一问题有很多重叠子问题,使用动态规划是最有效的。
Expand Down

AltStyle によって変換されたページ (->オリジナル) /