mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 23:28:29 +08:00
@ -243,8 +243,27 @@ class Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
> 动态规划:版本二(使用二維數組(和卡哥思路一致),下面還有使用一維滾動數組的更優化版本)
|
||||||
|
|
||||||
> 动态规划:版本二
|
```Java
|
||||||
|
class Solution {
|
||||||
|
public int maxProfit(int[] prices) {
|
||||||
|
int len = prices.length;
|
||||||
|
int dp[][] = new int[2][2];
|
||||||
|
|
||||||
|
dp[0][0] = - prices[0];
|
||||||
|
dp[0][1] = 0;
|
||||||
|
|
||||||
|
for (int i = 1; i < len; i++){
|
||||||
|
dp[i % 2][0] = Math.max(dp[(i - 1) % 2][0], - prices[i]);
|
||||||
|
dp[i % 2][1] = Math.max(dp[(i - 1) % 2][1], prices[i] + dp[(i - 1) % 2][0]);
|
||||||
|
}
|
||||||
|
return dp[(len - 1) % 2][1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
> 动态规划:版本二(使用一維數組)
|
||||||
|
|
||||||
``` java
|
``` java
|
||||||
class Solution {
|
class Solution {
|
||||||
@ -271,6 +290,10 @@ class Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
```Java
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user