当前位置:网站首页>[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;
}
}
边栏推荐
猜你喜欢
普洛斯数据中心发布DC Brain系统,科技赋能智慧化运营管理
ECU introduction
SQL injection of cisp-pte (Application of secondary injection)
Etcd 构建高可用Etcd集群
Copy mode DMA
The survey shows that the failure rate of traditional data security tools in the face of blackmail software attacks is as high as 60%
Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
Basic introduction to the control of the row component displaying its children in the horizontal array (tutorial includes source code)
Deep dive kotlin synergy (XXI): flow life cycle function
File operation --i/o
随机推荐
【组队 PK 赛】本周任务已开启 | 答题挑战,夯实商品详情知识
C how TCP restricts the access traffic of a single client
Do sqlserver have any requirements for database performance when doing CDC
WSL2.0安装
tf. sequence_ Mask function explanation case
Hiengine: comparable to the local cloud native memory database engine
浏览器渲染原理以及重排与重绘
Jarvis OJ Flag
2020-2022 two-year anniversary of creation
Android privacy sandbox developer preview 3: privacy, security and personalized experience
ECU introduction
树莓派4b安装Pytorch1.11
Jarvis OJ Webshell分析
如何安装mysql
【 brosser le titre 】 chemise culturelle de l'usine d'oies
How does the outer disk futures platform distinguish formal security?
齐宣王典故
飞桨EasyDL实操范例:工业零件划痕自动识别
中间表是如何被消灭的?
Scratch colorful candied haws Electronic Society graphical programming scratch grade examination level 3 true questions and answers analysis June 2022