当前位置:网站首页>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—————
边栏推荐
- Alpha conversion from gamma space to linner space under URP (II) -- multi alpha map superposition
- Detailed explanation of printf() and scanf() functions of C language
- 2022 information system management engineer examination outline
- Domain name resolution, reverse domain name resolution nbtstat
- [7.7 live broadcast preview] the lecturer of "typical architecture of SaaS cloud native applications" teaches you to easily build cloud native SaaS applications. Once the problem is solved, Huawei's s
- CMake教程Step1(基本起点)
- 查看自己电脑连接过的WiFi密码
- 世界上最难的5种编程语言
- Cartoon: how to multiply large integers? (next)
- 力扣解法汇总729-我的日程安排表 I
猜你喜欢
Winedt common shortcut key modify shortcut key latex compile button
Rider 设置选中单词侧边高亮,去除警告建议高亮
CMake教程Step4(安装和测试)
統計php程序運行時間及設置PHP最長運行時間
MySql 查询符合条件的最新数据行
7. Scala class
Embedded UC (UNIX System Advanced Programming) -3
thinkphp3.2.3
Rider set the highlighted side of the selected word, remove the warning and suggest highlighting
深入理解Redis内存淘汰策略
随机推荐
MySQL之知识点(六)
Oracle缩表空间的完整解决实例
ternary operator
Is it safe for China Galaxy Securities to open an account? How long can I buy stocks after opening an account
漏洞复现----48、Airflow dag中的命令注入(CVE-2020-11978)
漫画:有趣的【海盗】问题
SQL Server(2)
CMake教程Step2(添加库)
CMake教程Step4(安装和测试)
mysql5.6解析JSON字符串方式(支持复杂的嵌套格式)
mysql中取出json字段的小技巧
WR | Jufeng group of West Lake University revealed the impact of microplastics pollution on the flora and denitrification function of constructed wetlands
Flow characteristics of kitchen knife, ant sword, ice scorpion and Godzilla
Force deduction solution summary 729- my schedule I
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
【7.7直播预告】《SaaS云原生应用典型架构》大咖讲师教你轻松构建云原生SaaS化应用,难题一一击破,更有华为周边好礼等你领!
The first lesson of EasyX learning
Oracle Recovery Tools ----oracle数据库恢复利器
IDEA 项目启动报错 Shorten the command line via JAR manifest or via a classpath file and rerun.
Embedded-c language-6