当前位置:网站首页>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;
}边栏推荐
- 多文件程序X32dbg动态调试
- SCM power supply
- SSH automatically disconnects (pretends to be dead) after a period of no operation
- [untitled] how to mount a hard disk in armbian
- YYGH-9-预约下单
- How to Add P-Values onto Horizontal GGPLOTS
- How to Visualize Missing Data in R using a Heatmap
- Data analysis - Matplotlib sample code
- qt 仪表自定义控件
- 6. Introduce you to LED soft film screen. LED soft film screen size | price | installation | application
猜你喜欢

基于Arduino和ESP8266的连接手机热点实验(成功)

HOW TO CREATE A BEAUTIFUL INTERACTIVE HEATMAP IN R

HR wonderful dividing line

Larvel modify table fields
![[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)

自然语言处理系列(二)——使用RNN搭建字符级语言模型

Beautiful and intelligent, Haval H6 supreme+ makes Yuanxiao travel safer

多文件程序X32dbg动态调试

Depth filter of SvO2 series

PyTorch nn. Full analysis of RNN parameters
随机推荐
to_ Bytes and from_ Bytes simple example
HOW TO ADD P-VALUES TO GGPLOT FACETS
GGHIGHLIGHT: EASY WAY TO HIGHLIGHT A GGPLOT IN R
Deep understanding of NN in pytorch Embedding
php 二维、多维 数组打乱顺序,PHP_php打乱数组二维数组多维数组的简单实例,php中的shuffle函数只能打乱一维
Easyexcel and Lombok annotations and commonly used swagger annotations
进入前六!博云在中国云管理软件市场销量排行持续上升
File operation (detailed!)
BEAUTIFUL GGPLOT VENN DIAGRAM WITH R
Log4j2
【工控老马】西门子PLC Siemens PLC TCP协议详解
史上最易懂的f-string教程,收藏这一篇就够了
H5, add a mask layer to the page, which is similar to clicking the upper right corner to open it in the browser
On data preprocessing in sklearn
PyTorch nn. Full analysis of RNN parameters
Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement
Esp32 stores the distribution network information +led displays the distribution network status + press the key to clear the distribution network information (source code attached)
Enter the top six! Boyun's sales ranking in China's cloud management software market continues to rise
CONDA common command summary
史上最易懂的f-string教程,收藏這一篇就够了