当前位置:网站首页>Cartoon: looking for the best time to buy and sell stocks
Cartoon: looking for the best time to buy and sell stocks
2022-07-05 17:32:00 【Small ash】
————— the second day —————
What does that mean ? Let's take an example , Given the following array :
The corresponding stock up and down curve of this array is as follows :
obviously , From 2 The price is 1 Buy when , From 5 The price is 8 Sell when , You can get the most out of it :
The biggest payoff at this point is 8-1=7.
In the example above , Maximum 9 At the minimum 1 In front of , How can we trade ? We can't turn back time ?
————————————
The following is an example , If we have set the price 4 It's time to sell , So which is the best time to buy ?
We have to choose the price 4 The previous interval , And must be the minimum value in the interval , obviously , The best choice is price 2 The timing of the .
The first 1 Step , Initialization operation , Put the first 1 Elements as temporary minimum price ; The initial value of maximum return is 0:
The first 2 Step , Traverse to the 2 Elements , because 2<9, So the current minimum price becomes 2; There is no need to calculate the difference ( Because the front element is bigger than it ), The biggest benefit is still 0:
The first 3 Step , Traverse to the 3 Elements , because 7>2, So the current minimum price is still 2; As we said just now , Suppose the price 7 For selling point , The best buy point for that is the price 2, The difference between the two 7-2=5,5>0, So the biggest payoff right now is 5:
The first 4 Step , Traverse to the 4 Elements , because 4>2, So the current minimum price is still 2;4-2=2,2<5, So the biggest payoff is still 5:
The first 5 Step , Traverse to the 5 Elements , because 3>2, So the current minimum price is still 2;3-2=1,1<5, So the biggest payoff is still 5:
And so on , We walk through the end of the array , The minimum price at this time is 1; The biggest benefit is 8-1=7:
public class StockProfit {
public static int maxProfitFor1Time(int prices[]) {
if(prices==null || prices.length==0) {
return 0;
}
int minPrice = prices[0];
int maxProfit = 0;
for (int i = 1; i < prices.length; i++) {
if (prices[i] < minPrice) {
minPrice = prices[i];
} else if(prices[i] - minPrice > maxProfit){
maxProfit = prices[i] - minPrice;
}
}
return maxProfit;
}
public static void main(String[] args) {
int[] prices = {9,2,7,4,3,1,8,4};
System.out.println(maxProfitFor1Time(prices));
}
}
Before each purchase , You have to sell your shares . Let's take the array in the following figure for example , Take a visual look at the timing of buying and selling :
In the picture , The green line represents the stage of price rise . Since there is no limit to the number of transactions , So we can buy at every low point , Sell at every high .
thus , All the green parts are our gains , The maximum total return is the sum of these local returns :
(6-1)+(8-3)+(4-2)+(7-4) = 15
As for how to judge these green rising stages ? It's simple , We traverse the entire array , But where the latter is greater than the former , It means that the stock price is in the rising stage .
public int maxProfitForAnyTime(int[] prices) {
int maxProfit = 0;
for (int i = 1; i < prices.length; i++) {
if (prices[i] > prices[i-1])
maxProfit += prices[i] - prices[i-1];
}
return maxProfit;
}
—————END—————
边栏推荐
- 蚂蚁金服的暴富还未开始,Zoom的神话却仍在继续!
- Embedded-c Language-5
- 哈趣K1和哈趣H1哪个性价比更高?谁更值得入手?
- CMake教程Step6(添加自定义命令和生成文件)
- IDC报告:腾讯云数据库稳居关系型数据库市场TOP 2!
- ICML 2022 | Meta propose une méthode robuste d'optimisation bayésienne Multi - objectifs pour faire face efficacement au bruit d'entrée
- Troubleshooting - about clip not found Visual Studio
- Winedt common shortcut key modify shortcut key latex compile button
- Cartoon: interesting [pirate] question
- Cartoon: how to multiply large integers? (integrated version)
猜你喜欢
Winedt common shortcut key modify shortcut key latex compile button
ICML 2022 | Meta提出魯棒的多目標貝葉斯優化方法,有效應對輸入噪聲
Kafaka technology lesson 1
基于Redis实现延时队列的优化方案小结
In depth understanding of redis memory obsolescence strategy
机器学习01:绪论
机器学习02:模型评估
33:第三章:开发通行证服务:16:使用Redis缓存用户信息;(以减轻数据库的压力)
Embedded UC (UNIX System Advanced Programming) -2
一个满分的项目文档是如何书写的|得物技术
随机推荐
华为云云原生容器综合竞争力,中国第一!
Cloud security daily 220705: the red hat PHP interpreter has found a vulnerability of executing arbitrary code, which needs to be upgraded as soon as possible
漫画:有趣的海盗问题 (完整版)
MySQL之知识点(七)
Embedded-c Language-5
力扣解法汇总729-我的日程安排表 I
Embedded-c Language-3
winedt常用快捷键 修改快捷键latex编译按钮
Complete solution instance of Oracle shrink table space
机器学习02:模型评估
Detailed explanation of printf() and scanf() functions of C language
Alpha conversion from gamma space to linner space under URP (II) -- multi alpha map superposition
Error in compiling libssh2. OpenSSL cannot be found
叩富网开期货账户安全可靠吗?怎么分辨平台是否安全?
Cartoon: interesting [pirate] question
中国银河证券开户安全吗 开户后多久能买股票
Embedded UC (UNIX System Advanced Programming) -3
ICML 2022 | Meta提出鲁棒的多目标贝叶斯优化方法,有效应对输入噪声
Cartoon: looking for the k-th element of an unordered array (Revised)
漫画:一道数学题引发的血案