# 买卖股票的最佳时机

## 买卖股票的最佳时机1

给定一个数组 prices ，它的第 i 个元素 prices\[i] 表示一支给定股票第 i 天的价格。

你只能选择 某一天 买入这只股票，并选择在 未来的某一个不同的日子 卖出该股票。设计一个算法来计算你所能获取的最大利润。

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

思路：记录遍历到的最小值，算出今日卖出的最大利润【cur - min】与res比较即可

```java
class Solution {
    public int maxProfit(int[] prices) {
        // 直接贪心 最大的就行
        int res = 0;
        for (int i = 1; i < prices.length; i++) {
            if (prices[i] > prices[i - 1]) {
                res += (prices[i] - prices[i - 1]);
            }
        }

        return res;
    }
}
```

## 买卖股票的最佳时机2

给定一个数组 prices ，其中 prices\[i] 是一支给定股票第 i 天的价格。

设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易（多次买卖一支股票）。

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

思路：一旦股价有上涨，就将上涨的加入到收益当中

```java
class Solution {
    public int maxProfit(int[] prices) {
        // 直接贪心 最大的就行
        int res = 0;
        for (int i = 1; i < prices.length; i++) {
            if (prices[i] > prices[i - 1]) {
                res += (prices[i] - prices[i - 1]);
            }
        }

        return res;
    }
}
```

## 买卖股票的最佳时机3

给定一个数组，它的第 i 个元素是一支给定的股票在第 i 天的价格。

设计一个算法来计算你所能获取的最大利润。你最多可以完成 两笔 交易。

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

思路：1、穷举，因为不能同时持有，划线左边买卖最大，划线右边买卖最大，时间复杂度n的方

2、动态规划，注解里面比较详细了，就是初始化赋值需要注意，第一次买入和第二次买入的值为-prices\[0]，卖出均为0

```java
class Solution {
    public int maxProfit(int[] prices) {
        // dp[i][j] 存储的值是当前金额，如果已经买入了就是负数了，i表示第i兲情况
        // j 可取：0123分别表示第一次买入，第一次卖出，第二次买入，第二次卖出，注意这里如果处于
        // 第一次卖出状态，有可能是已经麦完了，并不要求一定是当天卖出了
        int[][] dp = new int[prices.length][4];

        dp[0][0] = -prices[0];
        dp[0][1] = 0;
        dp[0][2] = -prices[0];
        dp[0][3] = 0;

        for (int i = 1; i < prices.length; i++) {
            dp[i][0] = Math.max(dp[i - 1][0], -prices[i]);
            dp[i][1] = Math.max(dp[i - 1][1], dp[i][0] + prices[i]);
            dp[i][2] = Math.max(dp[i - 1][2], dp[i][1] - prices[i]);
            dp[i][3] = Math.max(dp[i - 1][3], dp[i][2] + prices[i]);
        }

        return dp[prices.length - 1][3];
    }
}
```

## 买卖股票的最佳时机4

给定一个整数数组 prices ，它的第 i 个元素 prices\[i] 是一支给定的股票在第 i 天的价格。

设计一个算法来计算你所能获取的最大利润。你最多可以完成 k 笔交易。

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

思路：这里dp\[i]\[j]当中的j为奇数卖出，偶数买入，这样来进行推倒，思路和上面的保持一致即可

注意需要判定边界条件，给定数组不为空，卖买次数不能为0

```java
class Solution {
    public int maxProfit(int k, int[] prices) {
        // 依然使用上面的思路，使用二维数组来做
        // 0123 偶数买入，奇数卖出

        if (prices.length == 0 || k == 0) {
            return 0;
        }

        int[][] dp = new int[prices.length][k * 2];

        for (int i = 0; i < k; i++) {
            dp[0][2 * i] = -prices[0];
        }

        for (int i = 1; i < prices.length; i++) {
            dp[i][0] = Math.max(dp[i - 1][0], -prices[i]);
            dp[i][1] = Math.max(dp[i - 1][1], dp[i][0] + prices[i]);
            for (int j = 1; j < k; j++) {
                // 2j 2j+1
                // 买入的
                dp[i][2 * j] = Math.max(dp[i - 1][2 * j], dp[i][2 * j - 1] - prices[i]);
                // 卖出的
                dp[i][2 * j + 1] = Math.max(dp[i - 1][2 * j + 1], dp[i][2 * j] + prices[i]);
            }
        }

        return dp[prices.length - 1][2 * k - 1];
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://luckycurvec.gitbook.io/java-knowledge-architecture/mian-shi-15/leetcode/mai-ru-gu-piao-de-zui-jia-shi-ji.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
