mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 20:40:39 +08:00
docs: 股票的最佳时机二添加视频链接跳转
This commit is contained in:
@ -4,7 +4,6 @@
|
||||
</a>
|
||||
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
|
||||
|
||||
|
||||
# 122.买卖股票的最佳时机 II
|
||||
|
||||
[力扣题目链接](https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-ii/)
|
||||
@ -15,32 +14,39 @@
|
||||
|
||||
注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
|
||||
|
||||
|
||||
示例 1:
|
||||
* 输入: [7,1,5,3,6,4]
|
||||
* 输出: 7
|
||||
* 解释: 在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4。随后,在第 4 天(股票价格 = 3)的时候买入,在第 5 天(股票价格 = 6)的时候卖出, 这笔交易所能获得利润 = 6-3 = 3 。
|
||||
|
||||
- 输入: [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
|
||||
* 解释: 在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。注意你不能在第 1 天和第 2 天接连购买股票,之后再将它们卖出。因为这样属于同时参与了多笔交易,你必须在再次购买前出售掉之前的股票。
|
||||
|
||||
- 输入: [1,2,3,4,5]
|
||||
- 输出: 4
|
||||
- 解释: 在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。注意你不能在第 1 天和第 2 天接连购买股票,之后再将它们卖出。因为这样属于同时参与了多笔交易,你必须在再次购买前出售掉之前的股票。
|
||||
|
||||
示例 3:
|
||||
* 输入: [7,6,4,3,1]
|
||||
* 输出: 0
|
||||
* 解释: 在这种情况下, 没有交易完成, 所以最大利润为 0。
|
||||
|
||||
- 输入: [7,6,4,3,1]
|
||||
- 输出: 0
|
||||
- 解释: 在这种情况下, 没有交易完成, 所以最大利润为 0。
|
||||
|
||||
提示:
|
||||
* 1 <= prices.length <= 3 * 10 ^ 4
|
||||
* 0 <= prices[i] <= 10 ^ 4
|
||||
|
||||
- 1 <= prices.length <= 3 \* 10 ^ 4
|
||||
- 0 <= prices[i] <= 10 ^ 4
|
||||
|
||||
# 视频讲解
|
||||
|
||||
**《代码随想录》算法视频公开课:[贪心算法也能解决股票问题!LeetCode:122.买卖股票最佳时机 II](https://www.bilibili.com/video/BV1ev4y1C7na),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
|
||||
|
||||
## 思路
|
||||
|
||||
本题首先要清楚两点:
|
||||
|
||||
* 只有一只股票!
|
||||
* 当前只有买股票或者卖股票的操作
|
||||
- 只有一只股票!
|
||||
- 当前只有买股票或者卖股票的操作
|
||||
|
||||
想获得利润至少要两天为一个交易单元。
|
||||
|
||||
@ -62,7 +68,6 @@
|
||||
|
||||
如图:
|
||||
|
||||
|
||||

|
||||
|
||||
一些同学陷入:第一天怎么就没有利润呢,第一天到底算不算的困惑中。
|
||||
@ -92,8 +97,8 @@ public:
|
||||
};
|
||||
```
|
||||
|
||||
* 时间复杂度:O(n)
|
||||
* 空间复杂度:O(1)
|
||||
- 时间复杂度:O(n)
|
||||
- 空间复杂度:O(1)
|
||||
|
||||
### 动态规划
|
||||
|
||||
@ -119,8 +124,8 @@ public:
|
||||
};
|
||||
```
|
||||
|
||||
* 时间复杂度:$O(n)$
|
||||
* 空间复杂度:$O(n)$
|
||||
- 时间复杂度:$O(n)$
|
||||
- 空间复杂度:$O(n)$
|
||||
|
||||
## 总结
|
||||
|
||||
@ -137,6 +142,7 @@ public:
|
||||
### Java:
|
||||
|
||||
贪心:
|
||||
|
||||
```java
|
||||
// 贪心思路
|
||||
class Solution {
|
||||
@ -151,6 +157,7 @@ class Solution {
|
||||
```
|
||||
|
||||
动态规划:
|
||||
|
||||
```java
|
||||
class Solution { // 动态规划
|
||||
public int maxProfit(int[] prices) {
|
||||
@ -173,7 +180,9 @@ class Solution { // 动态规划
|
||||
```
|
||||
|
||||
### Python:
|
||||
|
||||
贪心:
|
||||
|
||||
```python
|
||||
class Solution:
|
||||
def maxProfit(self, prices: List[int]) -> int:
|
||||
@ -184,6 +193,7 @@ class Solution:
|
||||
```
|
||||
|
||||
动态规划:
|
||||
|
||||
```python
|
||||
class Solution:
|
||||
def maxProfit(self, prices: List[int]) -> int:
|
||||
@ -200,6 +210,7 @@ class Solution:
|
||||
### Go:
|
||||
|
||||
贪心算法
|
||||
|
||||
```go
|
||||
func maxProfit(prices []int) int {
|
||||
var sum int
|
||||
@ -212,7 +223,9 @@ func maxProfit(prices []int) int {
|
||||
return sum
|
||||
}
|
||||
```
|
||||
|
||||
动态规划
|
||||
|
||||
```go
|
||||
func maxProfit(prices []int) int {
|
||||
dp := make([][]int, len(prices))
|
||||
@ -239,6 +252,7 @@ func max(a, b int) int {
|
||||
### Javascript:
|
||||
|
||||
贪心
|
||||
|
||||
```Javascript
|
||||
var maxProfit = function(prices) {
|
||||
let result = 0
|
||||
@ -250,6 +264,7 @@ var maxProfit = function(prices) {
|
||||
```
|
||||
|
||||
动态规划
|
||||
|
||||
```javascript
|
||||
const maxProfit = (prices) => {
|
||||
let dp = Array.from(Array(prices.length), () => Array(2).fill(0));
|
||||
@ -282,12 +297,13 @@ function maxProfit(prices: number[]): number {
|
||||
resProfit += Math.max(prices[i] - prices[i - 1], 0);
|
||||
}
|
||||
return resProfit;
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
### Rust
|
||||
|
||||
贪心:
|
||||
|
||||
```Rust
|
||||
impl Solution {
|
||||
fn max(a: i32, b: i32) -> i32 {
|
||||
@ -304,6 +320,7 @@ impl Solution {
|
||||
```
|
||||
|
||||
动态规划:
|
||||
|
||||
```Rust
|
||||
impl Solution {
|
||||
fn max(a: i32, b: i32) -> i32 {
|
||||
@ -323,7 +340,9 @@ impl Solution {
|
||||
```
|
||||
|
||||
### C:
|
||||
|
||||
贪心:
|
||||
|
||||
```c
|
||||
int maxProfit(int* prices, int pricesSize){
|
||||
int result = 0;
|
||||
@ -339,6 +358,7 @@ int maxProfit(int* prices, int pricesSize){
|
||||
```
|
||||
|
||||
动态规划:
|
||||
|
||||
```c
|
||||
#define max(a, b) (((a) > (b)) ? (a) : (b))
|
||||
|
||||
@ -363,6 +383,7 @@ int maxProfit(int* prices, int pricesSize){
|
||||
### Scala
|
||||
|
||||
贪心:
|
||||
|
||||
```scala
|
||||
object Solution {
|
||||
def maxProfit(prices: Array[Int]): Int = {
|
||||
|
Reference in New Issue
Block a user