当前位置:网站首页>[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;
}
}
边栏推荐
猜你喜欢
Jarvis OJ webshell analysis
[61dctf]fm
If you can't afford a real cat, you can use code to suck cats -unity particles to draw cats
Cs231n notes (bottom) - applicable to 0 Foundation
用键盘输入一条命令
[deep learning] how does deep learning affect operations research?
The survey shows that the failure rate of traditional data security tools in the face of blackmail software attacks is as high as 60%
China Radio and television officially launched 5g services, and China Mobile quickly launched free services to retain users
npm安装
Android privacy sandbox developer preview 3: privacy, security and personalized experience
随机推荐
Seaborn draws 11 histograms
手机开证券账户安全吗?怎么买股票详细步骤
极坐标扇图使用场景与功能详解
Solve cmakelist find_ Package cannot find Qt5, ECM cannot be found
树莓派4b安装Pytorch1.11
Apple has abandoned navigationview and used navigationstack and navigationsplitview to implement swiftui navigation
Enter a command with the keyboard
Do sqlserver have any requirements for database performance when doing CDC
It is forbidden to copy content JS code on the website page
Etcd build a highly available etcd cluster
解决CMakeList find_package找不到Qt5,找不到ECM
How to set the WiFi password of the router on the computer
[729. My schedule I]
深耕5G,芯讯通持续推动5G应用百花齐放
【剑指 Offer】62. 圆圈中最后剩下的数字
PHP talent recruitment system development source code recruitment website source code secondary development
PHP 严格模式
What is ROM
Benji Bananas 会员通行证持有人第二季奖励活动更新一览
Win11 prompt: what if the software cannot be downloaded safely? Win11 cannot download software safely