当前位置:网站首页>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;
}边栏推荐
- Develop scalable contracts based on hardhat and openzeppelin (I)
- Leetcode122 买卖股票的最佳时机 II
- YYGH-BUG-05
- 【C语言】十进制数转换成二进制数
- php 二维、多维 数组打乱顺序,PHP_php打乱数组二维数组多维数组的简单实例,php中的shuffle函数只能打乱一维
- 史上最易懂的f-string教程,收藏这一篇就够了
- HOW TO ADD P-VALUES TO GGPLOT FACETS
- BEAUTIFUL GGPLOT VENN DIAGRAM WITH R
- Research on and off the Oracle chain
- CONDA common command summary
猜你喜欢

How does Premiere (PR) import the preset mogrt template?

Jenkins用户权限管理

动态内存(进阶四)

小程序链接生成

自然语言处理系列(三)——LSTM

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

BEAUTIFUL GGPLOT VENN DIAGRAM WITH R
![[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](/img/6a/111da81436659c7502648907ec1367.jpg)
[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

Depth filter of SvO2 series

Small guide for rapid formation of manipulator (VII): description method of position and posture of manipulator
随机推荐
H5, add a mask layer to the page, which is similar to clicking the upper right corner to open it in the browser
Power Spectral Density Estimates Using FFT---MATLAB
Develop scalable contracts based on hardhat and openzeppelin (I)
K-Means Clustering Visualization in R: Step By Step Guide
Time format display
Natural language processing series (III) -- LSTM
Fabric.js 3个api设置画布宽高
HOW TO CREATE AN INTERACTIVE CORRELATION MATRIX HEATMAP IN R
File operation (detailed!)
Analyse de l'industrie
Jenkins用户权限管理
CMake交叉编译
What week is a date obtained by QT
Uniapp uni list item @click, uniapp uni list item jump with parameters
【2022 ACTF-wp】
uniapp uni-list-item @click,uniapp uni-list-item带参数跳转
二分刷题记录(洛谷题单)区间的甄别
Those logs in MySQL
Small guide for rapid formation of manipulator (VII): description method of position and posture of manipulator
qt 仪表自定义控件