当前位置:网站首页>[Jianzhi offer] 63 Maximum profit of stock
[Jianzhi offer] 63 Maximum profit of stock
2022-07-05 16:58:00 【LuZhouShiLi】
The finger of the sword Offer 63. The biggest profit of stocks
subject
Suppose you store the price of a stock in an array in chronological order , What's the maximum profit you can get from buying and selling this stock at one time ?
Ideas
- dp[i] Representative to prices[i] Maximum profit for the ending subarray
- State transition equation : front i Maximum daily profit = max( front (i - 1) Maximum daily profit , The first i Daily price - front i Daily minimum price )

Code
class Solution {
public int maxProfit(int[] prices) {
int cost = Integer.MAX_VALUE,profit = 0;
for(int price:prices){
cost = Math.min(cost,price);// Find the lowest price in history
profit = Math.max(profit,price - cost);// Update maximum profit
}
return profit;
}
}
边栏推荐
- 有序链表集合求交集 方法 总结
- Timestamp strtotime the day before or after the date
- The survey shows that the failure rate of traditional data security tools in the face of blackmail software attacks is as high as 60%
- 微信公众号网页授权登录实现起来如此简单
- composer安装报错:No composer.lock file present.
- Deep dive kotlin synergy (XXI): flow life cycle function
- Etcd build a highly available etcd cluster
- PHP talent recruitment system development source code recruitment website source code secondary development
- Hiengine: comparable to the local cloud native memory database engine
- 浏览器渲染原理以及重排与重绘
猜你喜欢

Benji Banas membership pass holders' second quarter reward activities update list

npm安装

How to install MySQL

Pspnet | semantic segmentation and scene analysis

Oneforall installation and use

养不起真猫,就用代码吸猫 -Unity 粒子实现画猫咪

Copy mode DMA

极坐标扇图使用场景与功能详解

How does win11 change icons for applications? Win11 method of changing icons for applications

Fleet tutorial 09 basic introduction to navigationrail (tutorial includes source code)
随机推荐
[729. My Schedule i]
Spring Festival Limited "forget trouble in the year of the ox" gift bag waiting for you to pick it up~
Facing new challenges and becoming a better self -- attacking technology er
Twig数组合并的写法
【刷题篇】有效的数独
Domestic API management artifact used by the company
Benji Bananas 会员通行证持有人第二季奖励活动更新一览
Yarn common commands
Cs231n notes (bottom) - applicable to 0 Foundation
Keras crash Guide
File operation --i/o
[brush title] goose factory shirt problem
Learnopongl notes (I)
Sentinel-流量防卫兵
Iphone14 with pill screen may trigger a rush for Chinese consumers
普洛斯数据中心发布DC Brain系统,科技赋能智慧化运营管理
PHP strict mode
Jarvis OJ shell traffic analysis
Binary tree related OJ problems
【729. 我的日程安排錶 I】