当前位置:网站首页>LeetCode121. 买卖股票的最佳时机
LeetCode121. 买卖股票的最佳时机
2022-06-28 20:59:00 【Yuyy】
本文最后更新于 484 天前,其中的信息可能已经有所发展或是发生改变。
一、思路
找出选择和状态
二、问题
给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。
你只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票。设计一个算法来计算你所能获取的最大利润。
返回你可以从这笔交易中获取的最大利润。如果你不能获取任何利润,返回 0 。
示例 1:
输入:[7,1,5,3,6,4]
输出:5
解释:在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。
注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格;同时,你不能在买入前卖出股票。示例 2:
输入:prices = [7,6,4,3,1]
输出:0
解释:在这种情况下, 没有交易完成, 所以最大利润为 0。提示:
1 <= prices.length <= 1050 <= prices[i] <= 104
Related Topics
- 数组
- 动态规划
\n
- 1447
- 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(- prices[i], arr[i - 1][1]);
}
return arr[prices.length-1][0];
}Post Views: 225
边栏推荐
- Input and output real data
- Bitbucket failed to pull the warehouse Using SSH
- Is it safe for CICC fortune to open an account? Let's talk about CICC fortune
- Various types of long
- Comparisonchain file name sort
- Ref attribute, props configuration, mixin mixing, plug-in, scoped style
- 炒股票能赚钱么?开户安全嘛
- 不同框架的绘制神经网络结构可视化
- 题解 Pie(POJ3122)超详细易懂的二分入门
- How do I download videos? Look at the super simple method!
猜你喜欢

Application practice | 1billion data second level correlation. Huolala's OLAP System Evolution Based on Apache Doris (with PPT download)

Apisik helps Middle East social software realize localized deployment

应用实践 | 10 亿数据秒级关联,货拉拉基于 Apache Doris 的 OLAP 体系演进(附 PPT 下载)

Leetcode daily question - 515 Find the maximum value in each tree row

MySQL system error occurred 1067

API 网关 Apache APISIX 助力雪球双活架构演进
![[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)

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

阿里云 MSE 基于 Apache APISIX 的全链路灰度方案实践

Flatten of cnn-lstm
随机推荐
API gateway Apache APIs IX helps the evolution of snowball dual active architecture
学习太极创客 — MQTT 第二章(八)ESP8266 MQTT 用户密码认证
大智慧上怎么进行开户啊, 安全吗
[Note: analog MOS integrated circuit] bandgap reference (basic principle + current mode + voltage mode circuit explanation)
RT thread thread synchronization and thread communication
ANR无响应介绍
Flask——总结
[book club issue 13] packaging format of video files
iterator中的next()为什么要强转?
03.hello_ rust
Pie (poj3122) super detailed and easy to understand binary introduction
Application practice | 1billion data second level correlation. Huolala's OLAP System Evolution Based on Apache Doris (with PPT download)
Figure neural network can also be used as CV backbone model. Huawei Noah Vig architecture is comparable to CNN and transformer
Characters and integers
Input and output character data
二叉树类题目 力扣
Learning Tai Chi Maker - mqtt Chapter II (VII) esp8266 mqtt Testament application
Data standardization processing
LeetCode每日一题——710. 黑名单中的随机数
Real number operation