当前位置:网站首页>LeetCode122. 买卖股票的最佳时机II
LeetCode122. 买卖股票的最佳时机II
2022-06-28 20:59:00 【Yuyy】
本文最后更新于 484 天前,其中的信息可能已经有所发展或是发生改变。
一、思路
找出选择和状态
二、问题
给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。
设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。
注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
示例 1:
输入: [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 天接连购买股票,之后再将它们卖出。
因为这样属于同时参与了多笔交易,你必须在再次购买前出售掉之前的股票。示例 3:
输入: [7,6,4,3,1]
输出: 0
解释: 在这种情况下, 没有交易完成, 所以最大利润为 0。提示:
1 <= prices.length <= 3 * 10 ^ 40 <= prices[i] <= 10 ^ 4
Related Topics
- 贪心算法
- 数组
\n
- 1107
- 0
三、代码
public int maxProfit(int[] prices) {
if (prices.length < 2) {
return 0;
}
int[][] arr = new int[prices.length][2];
arr[0][1] = -prices[0];
arr[0][0] = 0;
for (int i = 1; i < prices.length; i++) {
arr[i][0] = Math.max(arr[i - 1][1] + prices[i], arr[i - 1][0]);
arr[i][1] = Math.max(arr[i - 1][0] - prices[i], arr[i - 1][1]);
}
return arr[prices.length - 1][0];
}Post Views: 317
边栏推荐
- [try to hack] cobalt strike (I)
- 炒股票能赚钱么?开户安全嘛
- Bitbucket failed to pull the warehouse Using SSH
- No module named ‘PyEMD‘ ; Use plt figure()TypeError: ‘module‘ object is not callable
- 学习太极创客 — MQTT 第二章(七)ESP8266 MQTT 遗嘱应用
- [learning notes] factor analysis
- 市值1200亿美金,老牌财税巨头Intuit是如何做到的?
- Analysis of variance
- 【笔记:模拟MOS集成电路】带隙基准(基本原理+电流模+电压模电路详解)
- The principle and source code analysis of Lucene index construction
猜你喜欢
![[Note: analog MOS integrated circuit] bandgap reference (basic principle + current mode + voltage mode circuit explanation)](/img/cd/be62272d465ca990456c222b38df67.png)
[Note: analog MOS integrated circuit] bandgap reference (basic principle + current mode + voltage mode circuit explanation)

On the complexity of software development and the way to improve its efficiency

with torch.no_grad():的使用原因

Figure neural network can also be used as CV backbone model. Huawei Noah Vig architecture is comparable to CNN and transformer

Learn Tai Chi maker mqtt Chapter 2 (VIII) esp8266 mqtt user password authentication

数据资产为王,如何解析企业数字化转型与数据资产管理的关系?

方 差 分 析

Bitbucket 使用 SSH 拉取仓库失败的问题

【笔记:模拟MOS集成电路】带隙基准(基本原理+电流模+电压模电路详解)
![[learning notes] Introduction to principal component analysis](/img/24/a760d1cd095a967ef258b623eb465c.png)
[learning notes] Introduction to principal component analysis
随机推荐
Please allow the "imperfection" of the current domestic Tob
Bitbucket failed to pull the warehouse Using SSH
ComparisonChain-文件名排序
Bitbucket failed to pull the warehouse Using SSH
Stability summary
ThreadLocal principle
[learning notes] factor analysis
Embedded dynamic Arabic string conversion LCD display string [thanks for Jianguo ambition]
理解整个网络模型的构建
I almost ran away
Figure neural network can also be used as CV backbone model. Huawei Noah Vig architecture is comparable to CNN and transformer
Lucene构建索引的原理及源代码分析
Comparisonchain file name sort
Can you make money by speculating in stocks? It's safe to open an account
With a market value of $120billion, how did intuit, an old tax giant, do it?
不同框架的绘制神经网络结构可视化
LeetCode每日一题——522. 最长特殊序列 II
【读书会第13期】视频文件的封装格式
输入和输出实型数据
Input and output real data