mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 12:15:58 +08:00
更新 买卖股票的最佳时机系列 排版格式修复
This commit is contained in:
@ -24,11 +24,9 @@
|
|||||||
* 输出:0
|
* 输出:0
|
||||||
解释:在这种情况下, 没有交易完成, 所以最大利润为 0。
|
解释:在这种情况下, 没有交易完成, 所以最大利润为 0。
|
||||||
|
|
||||||
# 算法公开课
|
## 算法公开课
|
||||||
|
|
||||||
**《代码随想录》算法视频公开课:[动态规划之 LeetCode:121.买卖股票的最佳时机1](https://www.bilibili.com/video/BV1Xe4y1u77q),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
|
||||||
|
|
||||||
|
|
||||||
|
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划之 LeetCode:121.买卖股票的最佳时机1](https://www.bilibili.com/video/BV1Xe4y1u77q),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||||
|
|
||||||
## 思路
|
## 思路
|
||||||
|
|
||||||
@ -202,7 +200,7 @@ public:
|
|||||||
|
|
||||||
## 其他语言版本
|
## 其他语言版本
|
||||||
|
|
||||||
Java:
|
### Java:
|
||||||
|
|
||||||
> 贪心法:
|
> 贪心法:
|
||||||
|
|
||||||
@ -294,8 +292,7 @@ class Solution {
|
|||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Python:
|
||||||
Python:
|
|
||||||
|
|
||||||
> 贪心法:
|
> 贪心法:
|
||||||
```python
|
```python
|
||||||
@ -351,7 +348,8 @@ class Solution:
|
|||||||
return dp1
|
return dp1
|
||||||
```
|
```
|
||||||
|
|
||||||
Go:
|
### Go:
|
||||||
|
|
||||||
> 贪心法:
|
> 贪心法:
|
||||||
```Go
|
```Go
|
||||||
func maxProfit(prices []int) int {
|
func maxProfit(prices []int) int {
|
||||||
@ -418,7 +416,7 @@ func max(a, b int) int {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
JavaScript:
|
### JavaScript:
|
||||||
|
|
||||||
> 动态规划
|
> 动态规划
|
||||||
|
|
||||||
@ -454,7 +452,7 @@ var maxProfit = function(prices) {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
TypeScript:
|
### TypeScript:
|
||||||
|
|
||||||
> 贪心法
|
> 贪心法
|
||||||
|
|
||||||
@ -492,7 +490,7 @@ function maxProfit(prices: number[]): number {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
C#:
|
### C#:
|
||||||
|
|
||||||
> 贪心法
|
> 贪心法
|
||||||
|
|
||||||
@ -533,7 +531,7 @@ public class Solution
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Rust:
|
### Rust:
|
||||||
|
|
||||||
> 贪心
|
> 贪心
|
||||||
|
|
||||||
|
@ -34,9 +34,9 @@
|
|||||||
* 1 <= prices.length <= 3 * 10 ^ 4
|
* 1 <= prices.length <= 3 * 10 ^ 4
|
||||||
* 0 <= prices[i] <= 10 ^ 4
|
* 0 <= prices[i] <= 10 ^ 4
|
||||||
|
|
||||||
# 算法公开课
|
## 算法公开课
|
||||||
|
|
||||||
**《代码随想录》算法视频公开课:[动态规划,股票问题第二弹 | LeetCode:122.买卖股票的最佳时机II](https://www.bilibili.com/video/BV1D24y1Q7Ls),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划,股票问题第二弹 | LeetCode:122.买卖股票的最佳时机II](https://www.bilibili.com/video/BV1D24y1Q7Ls),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||||
|
|
||||||
|
|
||||||
## 思路
|
## 思路
|
||||||
@ -133,8 +133,8 @@ public:
|
|||||||
|
|
||||||
## 其他语言版本
|
## 其他语言版本
|
||||||
|
|
||||||
|
### Java:
|
||||||
|
|
||||||
Java:
|
|
||||||
```java
|
```java
|
||||||
// 动态规划
|
// 动态规划
|
||||||
class Solution
|
class Solution
|
||||||
@ -191,7 +191,7 @@ class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Python:
|
### Python:
|
||||||
|
|
||||||
> 版本一:
|
> 版本一:
|
||||||
```python
|
```python
|
||||||
@ -221,7 +221,8 @@ class Solution:
|
|||||||
return dp[(length-1) % 2][1]
|
return dp[(length-1) % 2][1]
|
||||||
```
|
```
|
||||||
|
|
||||||
Go:
|
### Go:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
// 买卖股票的最佳时机Ⅱ 动态规划
|
// 买卖股票的最佳时机Ⅱ 动态规划
|
||||||
// 时间复杂度:O(n) 空间复杂度:O(n)
|
// 时间复杂度:O(n) 空间复杂度:O(n)
|
||||||
@ -250,7 +251,8 @@ func max(a, b int) int {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Javascript:
|
### JavaScript:
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
// 方法一:动态规划(dp 数组)
|
// 方法一:动态规划(dp 数组)
|
||||||
const maxProfit = (prices) => {
|
const maxProfit = (prices) => {
|
||||||
@ -290,7 +292,7 @@ const maxProfit = (prices) => {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
TypeScript:
|
### TypeScript:
|
||||||
|
|
||||||
> 动态规划
|
> 动态规划
|
||||||
|
|
||||||
@ -326,7 +328,7 @@ function maxProfit(prices: number[]): number {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
C#:
|
### C#:
|
||||||
|
|
||||||
> 贪心法
|
> 贪心法
|
||||||
|
|
||||||
@ -363,7 +365,7 @@ public class Solution
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Rust:
|
### Rust:
|
||||||
|
|
||||||
> 贪心
|
> 贪心
|
||||||
|
|
||||||
@ -414,4 +416,3 @@ impl Solution {
|
|||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
@ -39,9 +39,9 @@
|
|||||||
* 1 <= prices.length <= 10^5
|
* 1 <= prices.length <= 10^5
|
||||||
* 0 <= prices[i] <= 10^5
|
* 0 <= prices[i] <= 10^5
|
||||||
|
|
||||||
# 算法公开课
|
## 算法公开课
|
||||||
|
|
||||||
**《代码随想录》算法视频公开课:[动态规划,股票至多买卖两次,怎么求? | LeetCode:123.买卖股票最佳时机III](https://www.bilibili.com/video/BV1WG411K7AR),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划,股票至多买卖两次,怎么求? | LeetCode:123.买卖股票最佳时机III](https://www.bilibili.com/video/BV1WG411K7AR),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||||
|
|
||||||
|
|
||||||
## 思路
|
## 思路
|
||||||
@ -221,7 +221,7 @@ public:
|
|||||||
|
|
||||||
## 其他语言版本
|
## 其他语言版本
|
||||||
|
|
||||||
Java:
|
### Java:
|
||||||
|
|
||||||
```java
|
```java
|
||||||
// 版本一
|
// 版本一
|
||||||
@ -277,7 +277,7 @@ class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Python:
|
### Python:
|
||||||
|
|
||||||
> 版本一:
|
> 版本一:
|
||||||
```python
|
```python
|
||||||
@ -314,7 +314,7 @@ class Solution:
|
|||||||
return dp[4]
|
return dp[4]
|
||||||
```
|
```
|
||||||
|
|
||||||
Go:
|
### Go:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func maxProfit(prices []int) int {
|
func maxProfit(prices []int) int {
|
||||||
@ -344,7 +344,7 @@ func max(a, b int) int {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
JavaScript:
|
### JavaScript:
|
||||||
|
|
||||||
> 版本一:
|
> 版本一:
|
||||||
|
|
||||||
@ -383,7 +383,7 @@ const maxProfit = prices => {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
TypeScript:
|
### TypeScript:
|
||||||
|
|
||||||
> 版本一
|
> 版本一
|
||||||
|
|
||||||
@ -413,7 +413,7 @@ function maxProfit(prices: number[]): number {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
Rust:
|
### Rust:
|
||||||
|
|
||||||
> 版本一
|
> 版本一
|
||||||
|
|
||||||
@ -465,3 +465,4 @@ impl Solution {
|
|||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
@ -31,9 +31,9 @@
|
|||||||
* 0 <= prices.length <= 1000
|
* 0 <= prices.length <= 1000
|
||||||
* 0 <= prices[i] <= 1000
|
* 0 <= prices[i] <= 1000
|
||||||
|
|
||||||
# 算法公开课
|
## 算法公开课
|
||||||
|
|
||||||
**《代码随想录》算法视频公开课:[动态规划来决定最佳时机,至多可以买卖K次!| LeetCode:188.买卖股票最佳时机4](https://www.bilibili.com/video/BV16M411U7XJ),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划来决定最佳时机,至多可以买卖K次!| LeetCode:188.买卖股票最佳时机4](https://www.bilibili.com/video/BV16M411U7XJ),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||||
|
|
||||||
|
|
||||||
## 思路
|
## 思路
|
||||||
@ -173,7 +173,7 @@ public:
|
|||||||
|
|
||||||
## 其他语言版本
|
## 其他语言版本
|
||||||
|
|
||||||
Java:
|
### Java:
|
||||||
|
|
||||||
```java
|
```java
|
||||||
// 版本一: 三维 dp数组
|
// 版本一: 三维 dp数组
|
||||||
@ -295,7 +295,7 @@ class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Python:
|
### Python:
|
||||||
|
|
||||||
版本一
|
版本一
|
||||||
|
|
||||||
@ -329,7 +329,7 @@ class Solution:
|
|||||||
dp[j] = max(dp[j],dp[j-1]+prices[i])
|
dp[j] = max(dp[j],dp[j-1]+prices[i])
|
||||||
return dp[2*k]
|
return dp[2*k]
|
||||||
```
|
```
|
||||||
Go:
|
### Go:
|
||||||
|
|
||||||
版本一:
|
版本一:
|
||||||
|
|
||||||
@ -404,7 +404,7 @@ func max188(a, b int) int {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Javascript:
|
### JavaScript:
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
// 方法一:动态规划
|
// 方法一:动态规划
|
||||||
@ -454,7 +454,7 @@ var maxProfit = function(k, prices) {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
TypeScript:
|
### TypeScript:
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
function maxProfit(k: number, prices: number[]): number {
|
function maxProfit(k: number, prices: number[]): number {
|
||||||
@ -474,7 +474,7 @@ function maxProfit(k: number, prices: number[]): number {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
Rust:
|
### Rust:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
impl Solution {
|
impl Solution {
|
||||||
@ -529,3 +529,4 @@ impl Solution {
|
|||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
@ -20,9 +20,9 @@
|
|||||||
* 输出: 3
|
* 输出: 3
|
||||||
* 解释: 对应的交易状态为: [买入, 卖出, 冷冻期, 买入, 卖出]
|
* 解释: 对应的交易状态为: [买入, 卖出, 冷冻期, 买入, 卖出]
|
||||||
|
|
||||||
# 算法公开课
|
## 算法公开课
|
||||||
|
|
||||||
**《代码随想录》算法视频公开课:[动态规划来决定最佳时机,这次有冷冻期!| LeetCode:309.买卖股票的最佳时机含冷冻期](https://www.bilibili.com/video/BV1rP4y1D7ku),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划来决定最佳时机,这次有冷冻期!| LeetCode:309.买卖股票的最佳时机含冷冻期](https://www.bilibili.com/video/BV1rP4y1D7ku),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||||
|
|
||||||
|
|
||||||
## 思路
|
## 思路
|
||||||
@ -174,7 +174,7 @@ public:
|
|||||||
|
|
||||||
## 其他语言版本
|
## 其他语言版本
|
||||||
|
|
||||||
Java:
|
### Java:
|
||||||
|
|
||||||
```java
|
```java
|
||||||
class Solution {
|
class Solution {
|
||||||
@ -273,8 +273,9 @@ class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Python:
|
### Python:
|
||||||
版本一
|
版本一
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
@ -319,7 +320,8 @@ class Solution:
|
|||||||
return max(dp[-1][1], dp[-1][2])
|
return max(dp[-1][1], dp[-1][2])
|
||||||
|
|
||||||
```
|
```
|
||||||
Go:
|
### Go:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
// 最佳买卖股票时机含冷冻期 动态规划
|
// 最佳买卖股票时机含冷冻期 动态规划
|
||||||
// 时间复杂度O(n) 空间复杂度O(n)
|
// 时间复杂度O(n) 空间复杂度O(n)
|
||||||
@ -355,7 +357,7 @@ func max(a, b int) int {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Javascript:
|
### Javascript:
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
const maxProfit = (prices) => {
|
const maxProfit = (prices) => {
|
||||||
@ -397,7 +399,7 @@ const maxProfit = (prices) => {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
TypeScript:
|
### TypeScript:
|
||||||
|
|
||||||
> 版本一,与本文思路一致
|
> 版本一,与本文思路一致
|
||||||
|
|
||||||
@ -455,7 +457,7 @@ function maxProfit(prices: number[]): number {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
Rust:
|
### Rust:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
impl Solution {
|
impl Solution {
|
||||||
@ -484,3 +486,4 @@ impl Solution {
|
|||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
@ -32,9 +32,9 @@
|
|||||||
* 0 < prices[i] < 50000.
|
* 0 < prices[i] < 50000.
|
||||||
* 0 <= fee < 50000.
|
* 0 <= fee < 50000.
|
||||||
|
|
||||||
# 算法公开课
|
## 算法公开课
|
||||||
|
|
||||||
**《代码随想录》算法视频公开课:[动态规划来决定最佳时机,这次含手续费!| LeetCode:714.买卖股票的最佳时机含手续费](https://www.bilibili.com/video/BV1z44y1Z7UR),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划来决定最佳时机,这次含手续费!| LeetCode:714.买卖股票的最佳时机含手续费](https://www.bilibili.com/video/BV1z44y1Z7UR),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||||
|
|
||||||
|
|
||||||
## 思路
|
## 思路
|
||||||
@ -97,8 +97,8 @@ public:
|
|||||||
|
|
||||||
## 其他语言版本
|
## 其他语言版本
|
||||||
|
|
||||||
|
### Java:
|
||||||
|
|
||||||
Java:
|
|
||||||
```java
|
```java
|
||||||
/**
|
/**
|
||||||
* 卖出时支付手续费
|
* 卖出时支付手续费
|
||||||
@ -173,9 +173,9 @@ class Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
```
|
### python
|
||||||
|
|
||||||
|
|
||||||
Python:
|
|
||||||
```python
|
```python
|
||||||
class Solution:
|
class Solution:
|
||||||
def maxProfit(self, prices: List[int], fee: int) -> int:
|
def maxProfit(self, prices: List[int], fee: int) -> int:
|
||||||
@ -188,7 +188,8 @@ class Solution:
|
|||||||
return max(dp[-1][0], dp[-1][1])
|
return max(dp[-1][0], dp[-1][1])
|
||||||
```
|
```
|
||||||
|
|
||||||
Go:
|
### Go:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
// 买卖股票的最佳时机含手续费 动态规划
|
// 买卖股票的最佳时机含手续费 动态规划
|
||||||
// 时间复杂度O(n) 空间复杂度O(n)
|
// 时间复杂度O(n) 空间复杂度O(n)
|
||||||
@ -211,7 +212,8 @@ func max(a, b int) int {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Javascript:
|
### Javascript:
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
const maxProfit = (prices,fee) => {
|
const maxProfit = (prices,fee) => {
|
||||||
let dp = Array.from(Array(prices.length), () => Array(2).fill(0));
|
let dp = Array.from(Array(prices.length), () => Array(2).fill(0));
|
||||||
@ -224,7 +226,7 @@ const maxProfit = (prices,fee) => {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
TypeScript:
|
### TypeScript:
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
function maxProfit(prices: number[], fee: number): number {
|
function maxProfit(prices: number[], fee: number): number {
|
||||||
@ -245,8 +247,9 @@ function maxProfit(prices: number[], fee: number): number {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
Rust:
|
### Rust:
|
||||||
**贪心**
|
**贪心**
|
||||||
|
|
||||||
```Rust
|
```Rust
|
||||||
impl Solution {
|
impl Solution {
|
||||||
pub fn max_profit(prices: Vec<i32>, fee: i32) -> i32 {
|
pub fn max_profit(prices: Vec<i32>, fee: i32) -> i32 {
|
||||||
|
@ -471,21 +471,10 @@ public:
|
|||||||
「代码随想录」值得推荐给身边每一位学习算法的朋友同学们,关注后都会发现相见恨晚!
|
「代码随想录」值得推荐给身边每一位学习算法的朋友同学们,关注后都会发现相见恨晚!
|
||||||
|
|
||||||
|
|
||||||
## 其他语言版本
|
|
||||||
|
|
||||||
|
|
||||||
Java:
|
|
||||||
|
|
||||||
|
|
||||||
Python:
|
|
||||||
|
|
||||||
|
|
||||||
Go:
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user