当前位置:网站首页>【剑指 Offer】63. 股票的最大利润
【剑指 Offer】63. 股票的最大利润
2022-07-05 16:22:00 【LuZhouShiLi】
剑指 Offer 63. 股票的最大利润
题目
假设把某股票的价格按照时间先后顺序存储在数组中,请问买卖该股票一次可能获得的最大利润是多少?
思路
- dp[i]代表以prices[i]为结尾的子数组最大利润
- 状态转移方程:前i日最大利润 = max(前(i - 1)日最大利润,第i日价格 - 前i日最低价格)

代码
class Solution {
public int maxProfit(int[] prices) {
int cost = Integer.MAX_VALUE,profit = 0;
for(int price:prices){
cost = Math.min(cost,price);// 找出历史最低价格
profit = Math.max(profit,price - cost);// 更新最大利润
}
return profit;
}
}
边栏推荐
- 阈值同态加密在隐私计算中的应用:解读
- Apple 已弃用 NavigationView,使用 NavigationStack 和 NavigationSplitView 实现 SwiftUI 导航
- What is the difference between EDI license and ICP business license
- [es6] 模板字符串内添加if判断或添加三元运算符判断
- 【机器人坐标系第一讲】
- Sentinel-流量防卫兵
- Clear restore the scene 31 years ago, volcanic engine ultra clear repair beyond classic concert
- 美国芯片傲不起来了,中国芯片成功在新兴领域夺得第一名
- You should have your own persistence
- Practice independent and controllable 3.0 and truly create the open source business of the Chinese people
猜你喜欢

Win11提示无法安全下载软件怎么办?Win11无法安全下载软件

【 brosser le titre 】 chemise culturelle de l'usine d'oies

有序链表集合求交集 方法 总结

采用药丸屏的iPhone14或引发中国消费者的热烈抢购

PSPNet | 语义分割及场景分析

Flet tutorial 12 stack overlapping to build a basic introduction to graphic and text mixing (tutorial includes source code)

Jarvis OJ Flag

2020-2022两周年创作纪念日
![[brush title] goose factory shirt problem](/img/c8/44496c767a778101cf3982bd911933.png)
[brush title] goose factory shirt problem

Jarvis OJ 远程登录协议
随机推荐
微信公众号网页授权登录实现起来如此简单
[echart] resize lodash 实现窗口缩放时图表自适应
【729. 我的日程安排錶 I】
搜索 正排索引 和 倒排索引 区别
Solve cmakelist find_ Package cannot find Qt5, ECM cannot be found
详解SQL中Groupings Sets 语句的功能和底层实现逻辑
How to uninstall MySQL cleanly
浏览器渲染原理以及重排与重绘
How to install MySQL
Benji Banas membership pass holders' second quarter reward activities update list
【学术相关】多位博士毕业去了三四流高校,目前惨不忍睹……
Jarvis OJ webshell analysis
数据访问 - EntityFramework集成
Dare not buy thinking
What is the difference between EDI license and ICP business license
PHP strict mode
How to set the WiFi password of the router on the computer
Desci: is decentralized science the new trend of Web3.0?
【机器人坐标系第一讲】
[61dctf]fm