当前位置:网站首页>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;
}
边栏推荐
- qt 仪表自定义控件
- Uniapp uni list item @click, uniapp uni list item jump with parameters
- Implementation of address book (file version)
- K-Means Clustering Visualization in R: Step By Step Guide
- HOW TO ADD P-VALUES TO GGPLOT FACETS
- easyExcel和lombok注解以及swagger常用注解
- SVO2系列之深度濾波DepthFilter
- SVO2系列之深度滤波DepthFilter
- CONDA common command summary
- 进入前六!博云在中国云管理软件市场销量排行持续上升
猜你喜欢
[geek challenge 2019] upload
6. Introduce you to LED soft film screen. LED soft film screen size | price | installation | application
H5,为页面添加遮罩层,实现类似于点击右上角在浏览器中打开
Deep understanding of NN in pytorch Embedding
(C语言)输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
How to Create a Beautiful Plots in R with Summary Statistics Labels
BEAUTIFUL GGPLOT VENN DIAGRAM WITH R
HOW TO CREATE A BEAUTIFUL INTERACTIVE HEATMAP IN R
Take you ten days to easily finish the finale of go micro services (distributed transactions)
深入理解PyTorch中的nn.Embedding
随机推荐
CMake交叉编译
YYGH-BUG-05
Fabric. JS 3 APIs to set canvas width and height
Develop scalable contracts based on hardhat and openzeppelin (I)
高德地图测试用例
GGPLOT: HOW TO DISPLAY THE LAST VALUE OF EACH LINE AS LABEL
Repeat, tile and repeat in pytorch_ The difference between interleave
B high and beautiful code snippet sharing image generation
From scratch, develop a web office suite (3): mouse events
Data analysis - Matplotlib sample code
Natural language processing series (I) -- RNN Foundation
Uniapp uni list item @click, uniapp uni list item jump with parameters
Yygh-10-wechat payment
Log4j2
Cmake cross compilation
php 二维、多维 数组打乱顺序,PHP_php打乱数组二维数组多维数组的简单实例,php中的shuffle函数只能打乱一维
输入一个三位的数字,输出它的个位数,十位数、百位数。
PyTorch nn. Full analysis of RNN parameters
Leetcode14 最长公共前缀
Esp32 audio frame esp-adf add key peripheral process code tracking