当前位置:网站首页>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;
}边栏推荐
- Take you ten days to easily finish the finale of go micro services (distributed transactions)
- 还不会安装WSL 2?看这一篇文章就够了
- b格高且好看的代码片段分享图片生成
- xss-labs-master靶场环境搭建与1-6关解题思路
- Those logs in MySQL
- Esp32 audio frame esp-adf add key peripheral process code tracking
- YYGH-BUG-04
- GGPLOT: HOW TO DISPLAY THE LAST VALUE OF EACH LINE AS LABEL
- HOW TO ADD P-VALUES ONTO A GROUPED GGPLOT USING THE GGPUBR R PACKAGE
- Orb-slam2 data sharing and transmission between different threads
猜你喜欢

Natural language processing series (I) -- RNN Foundation

基于Arduino和ESP8266的Blink代码运行成功(包含错误分析)

Esp32 audio frame esp-adf add key peripheral process code tracking
![[QT] Qt development environment installation (QT version 5.14.2 | QT download | QT installation)](/img/18/f0c9ef6250a717f8e66c95da4de08c.jpg)
[QT] Qt development environment installation (QT version 5.14.2 | QT download | QT installation)

Power Spectral Density Estimates Using FFT---MATLAB

深入理解P-R曲线、ROC与AUC

How to Easily Create Barplots with Error Bars in R

File operation (detailed!)

Mish shake the new successor of the deep learning relu activation function

还不会安装WSL 2?看这一篇文章就够了
随机推荐
[geek challenge 2019] upload
【2022 ACTF-wp】
Leetcode922 按奇偶排序数组 II
How to Easily Create Barplots with Error Bars in R
ORB-SLAM2不同线程间的数据共享与传递
机械臂速成小指南(七):机械臂位姿的描述方法
Orb-slam2 data sharing and transmission between different threads
ES集群中节点与分片的区别
[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
xss-labs-master靶场环境搭建与1-6关解题思路
HOW TO CREATE A BEAUTIFUL INTERACTIVE HEATMAP IN R
Natural language processing series (III) -- LSTM
MSI announced that its motherboard products will cancel all paper accessories
Leetcode739 每日温度
史上最易懂的f-string教程,收藏这一篇就够了
b格高且好看的代码片段分享图片生成
HOW TO EASILY CREATE BARPLOTS WITH ERROR BARS IN R
5g era, learning audio and video development, a super hot audio and video advanced development and learning classic
Flesh-dect (media 2021) -- a viewpoint of material decomposition
GGPUBR: HOW TO ADD ADJUSTED P-VALUES TO A MULTI-PANEL GGPLOT