当前位置:网站首页>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—————
边栏推荐
- Check the WiFi password connected to your computer
- 域名解析,反向域名解析nbtstat
- 菜刀,蚁剑,冰蝎,哥斯拉的流量特征
- winedt常用快捷键 修改快捷键latex编译按钮
- URP下Alpha从Gamma空间到Linner空间转换(二)——多Alpha贴图叠加
- Embedded UC (UNIX System Advanced Programming) -3
- C # mixed graphics and text, written to the database in binary mode
- 普通程序员看代码,顶级程序员看趋势
- Three traversal methods of binary tree
- Alpha conversion from gamma space to linner space under URP (II) -- multi alpha map superposition
猜你喜欢
Database design in multi tenant mode
7. Scala class
Check the WiFi password connected to your computer
Rider 设置选中单词侧边高亮,去除警告建议高亮
ICML 2022 | meta proposes a robust multi-objective Bayesian optimization method to effectively deal with input noise
統計php程序運行時間及設置PHP最長運行時間
33:第三章:开发通行证服务:16:使用Redis缓存用户信息;(以减轻数据库的压力)
Kafaka技术第一课
Embedded UC (UNIX System Advanced Programming) -3
服务器配置 jupyter环境
随机推荐
Design of electronic clock based on 51 single chip microcomputer
What are the precautions for MySQL group by
Beijing internal promotion | the machine learning group of Microsoft Research Asia recruits full-time researchers in nlp/ speech synthesis and other directions
CMake教程Step3(添加库的使用要求)
解决“双击pdf文件,弹出”请安装evernote程序
Embedded-c Language-2
Function sub file writing
flask解决CORS ERR 问题
SQL Server(2)
Tips for extracting JSON fields from MySQL
漫画:如何实现大整数相乘?(上) 修订版
ternary operator
CMake教程Step4(安装和测试)
winedt常用快捷键 修改快捷键latex编译按钮
Learn about MySQL transaction isolation level
北京内推 | 微软亚洲研究院机器学习组招聘NLP/语音合成等方向全职研究员
Q2 encryption market investment and financing report in 2022: gamefi becomes an investment keyword
Judge whether a number is a prime number (prime number)
【7.7直播预告】《SaaS云原生应用典型架构》大咖讲师教你轻松构建云原生SaaS化应用,难题一一击破,更有华为周边好礼等你领!
力扣解法汇总729-我的日程安排表 I