当前位置:网站首页>Maximum profit of jz63 shares
Maximum profit of jz63 shares
2022-07-02 12:05:00 【Monsters 114】
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 ?
Input : [7,1,5,3,6,4]
Output : 5
explain : In the 2 God ( Stock price = 1) Buy when , In the 5 God ( Stock price = 6) Sell when , Maximum profit = 6-1 = 5 .
Note that profit cannot be 7-1 = 6, Because the selling price needs to be higher than the buying price .
Ideas :
Let's suppose we buy stocks ourselves . as time goes on , Every day we can choose whether to sell shares or not . that , Suppose it's in i God , If we want to sell shares today , So how much money can we make ?
obviously , If we're really buying and selling stocks , We will definitely think : If only I had bought stocks at an all-time low ! Great , In the title , We just use a variable to record a historical lowest price minprice, We can assume that our stock was bought on that day . So we're in the second i The profit from selling stocks in one day is prices[i] - minprice.
therefore , We just need to traverse the price array once , Record the lowest point in history , Then think about such a problem every day : If I bought at the lowest point in history , So how much money can I make from selling today ? When all days are considered , We got the best answer .
Implementation code
public int maxProfit(int[] prices) {
int maxProfit = 0;
int minPrice = Integer.MAX_VALUE;
for(int i = 0; i < prices.length;i++){
if(prices[i] < minPrice){
minPrice = prices[i];
}
else if(prices[i] - minPrice > maxProfit){
maxProfit = prices[i] - minPrice;
}
}
return maxProfit;
}边栏推荐
- Log4j2
- Mish-撼动深度学习ReLU激活函数的新继任者
- to_bytes与from_bytes简单示例
- HOW TO ADD P-VALUES ONTO A GROUPED GGPLOT USING THE GGPUBR R PACKAGE
- Deep understanding of NN in pytorch Embedding
- MSI announced that its motherboard products will cancel all paper accessories
- 小程序链接生成
- Depth filter of SvO2 series
- ES集群中节点与分片的区别
- 通讯录的实现(文件版本)
猜你喜欢

Read the Flink source code and join Alibaba cloud Flink group..

SVO2系列之深度濾波DepthFilter

小程序链接生成

Applet link generation

conda常用命令汇总

【C语言】十进制数转换成二进制数

MSI announced that its motherboard products will cancel all paper accessories

Fresh, 2022 advanced Android interview must know 100 questions (interview questions + answer analysis)

HOW TO ADD P-VALUES ONTO A GROUPED GGPLOT USING THE GGPUBR R PACKAGE

Filtre de profondeur de la série svo2
随机推荐
B high and beautiful code snippet sharing image generation
求16以内正整数的阶乘,也就是n的阶层(0=<n<=16)。输入1111退出。
GGHIGHLIGHT: EASY WAY TO HIGHLIGHT A GGPLOT IN R
GGPlot Examples Best Reference
[visual studio 2019] create MFC desktop program (install MFC development components | create MFC application | edit MFC application window | add click event for button | Modify button text | open appl
通讯录的实现(文件版本)
[geek challenge 2019] upload
From scratch, develop a web office suite (3): mouse events
Flesh-dect (media 2021) -- a viewpoint of material decomposition
Lekao: contents of the provisions on the responsibility of units for fire safety in the fire protection law
Analyse de l'industrie
二分刷题记录(洛谷题单)区间的甄别
Fabric.js 3个api设置画布宽高
[untitled] how to mount a hard disk in armbian
HOW TO EASILY CREATE BARPLOTS WITH ERROR BARS IN R
HOW TO ADD P-VALUES ONTO A GROUPED GGPLOT USING THE GGPUBR R PACKAGE
conda常用命令汇总
Yygh-10-wechat payment
字符串回文hash 模板题 O(1)判字符串是否回文
YYGH-10-微信支付