当前位置:网站首页>[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;
}
}
边栏推荐
- Yarn common commands
- Benji Bananas 会员通行证持有人第二季奖励活动更新一览
- How was the middle table destroyed?
- Spring Festival Limited "forget trouble in the year of the ox" gift bag waiting for you to pick it up~
- Copy mode DMA
- If you can't afford a real cat, you can use code to suck cats -unity particles to draw cats
- ECU简介
- 浏览器渲染原理以及重排与重绘
- 时间戳strtotime前一天或后一天的日期
- Win11 prompt: what if the software cannot be downloaded safely? Win11 cannot download software safely
猜你喜欢
Keras crash Guide
机器学习编译第2讲:张量程序抽象
Bs-xx-042 implementation of personnel management system based on SSM
Copy mode DMA
Learnopongl notes (I)
Benji Banas membership pass holders' second quarter reward activities update list
[team PK competition] the task of this week has been opened | question answering challenge to consolidate the knowledge of commodity details
Iphone14 with pill screen may trigger a rush for Chinese consumers
Get ready for the pre-season card game MotoGP ignition champions!
DenseNet
随机推荐
How to uninstall MySQL cleanly
[team PK competition] the task of this week has been opened | question answering challenge to consolidate the knowledge of commodity details
Win11 prompt: what if the software cannot be downloaded safely? Win11 cannot download software safely
【刷题篇】有效的数独
【剑指 Offer】61. 扑克牌中的顺子
Jarvis OJ shell traffic analysis
Do sqlserver have any requirements for database performance when doing CDC
关于new Map( )还有哪些是你不知道的
Excuse me, is the redis syntax used in DMS based on the commands of the redis community version of the cloud database
Data verification before and after JSON to map -- custom UDF
[js] 技巧 简化if 判空
外盘期货平台如何辨别正规安全?
Games101 notes (I)
干货!半监督预训练对话模型 SPACE
C how TCP restricts the access traffic of a single client
Dare not buy thinking
【刷題篇】鹅廠文化衫問題
阈值同态加密在隐私计算中的应用:解读
Seaborn draws 11 histograms
JSON转MAP前后数据校验 -- 自定义UDF