Say you have an array `prices` for which the ith element is the price of a given stock on day i.

Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).

Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).

Example 1:

```Input: [7,1,5,3,6,4]
Output: 7
Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.
```

Example 2:

```Input: [1,2,3,4,5]
Output: 4
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are
engaging multiple transactions at the same time.You must sell before buying again.
```

Example 3:

```Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.```

Constraints:

• `1 <= prices.length <= 3 * 10 ^ 4`
• `0 <= prices[i] <= 10 ^ 4`

JavaScript实现

``` 1 /**
2  * @param {number[]} prices
3  * @return {number}
4  */
5 var maxProfit = function (prices) {
6     // corner case
7     if (prices === null || prices.length < 2) return 0;
8
9     // normal case
10     let profit = 0;
11     for (let i = 1; i < prices.length; i++) {
12         if (prices[i] > prices[i - 1]) {
13             profit += prices[i] - prices[i - 1];
14         }
15     }
16     return profit;
17 };```

Java实现

``` 1 class Solution {
2     public int maxProfit(int[] prices) {
3         // corner case
4         if (prices == null || prices.length == 0) {
5             return 0;
6         }
7
8         // normal case
9         int profit = 0;
10         for (int i = 1; i < prices.length; i++) {
11             if (prices[i] > prices[i - 1]) {
12                 profit += prices[i] - prices[i - 1];
13             }
14         }
15         return profit;
16     }
17 }```

dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i]);

dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]);

Java实现

``` 1 class Solution {
2     public int maxProfit(int[] prices) {
3         int len = prices.length;
4         if (len < 2) {
5             return 0;
6         }
7         // 0：持有现金
8         // 1：持有股票
9         // 状态转移：0 → 1 → 0 → 1 → 0 → 1 → 0
10         int[][] dp = new int[len][2];
11         dp[0][0] = 0;
12         dp[0][1] = -prices[0];
13         for (int i = 1; i < len; i++) {
14             // 这两行调换顺序也是可以的
15             dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i]);
16             dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]);
17         }
18         return dp[len - 1][0];
19     }
20 }```

LeetCode 题目总结