当前位置:网站首页>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—————
边栏推荐
- 力扣解法汇总1200-最小绝对差
- In depth understanding of redis memory obsolescence strategy
- 【二叉树】根到叶路径上的不足节点
- Summary of optimization scheme for implementing delay queue based on redis
- 漫画:如何实现大整数相乘?(下)
- ICML 2022 | Meta提出鲁棒的多目标贝叶斯优化方法,有效应对输入噪声
- Three traversal methods of binary tree
- 排错-关于clion not found visual studio 的问题
- Example tutorial of SQL deduplication
- C # realizes crystal report binding data and printing 3-qr code barcode
猜你喜欢

ICML 2022 | Meta提出魯棒的多目標貝葉斯優化方法,有效應對輸入噪聲

机器学习01:绪论

Alpha conversion from gamma space to linner space under URP (II) -- multi alpha map superposition

MySQL之知识点(七)

Embedded -arm (bare board development) -1

ICML 2022 | Meta propose une méthode robuste d'optimisation bayésienne Multi - objectifs pour faire face efficacement au bruit d'entrée

Using C language to realize palindrome number

URP下Alpha从Gamma空间到Linner空间转换(二)——多Alpha贴图叠加

CMake教程Step2(添加库)
Complete solution instance of Oracle shrink table space
随机推荐
33: Chapter 3: develop pass service: 16: use redis to cache user information; (to reduce the pressure on the database)
ternary operator
机器学习01:绪论
Learn about MySQL transaction isolation level
Cmake tutorial step6 (add custom commands and generate files)
mysql5.6解析JSON字符串方式(支持复杂的嵌套格式)
About JSON parsing function JSON in MySQL_ EXTRACT
Use of ThinkPHP template
Beijing internal promotion | the machine learning group of Microsoft Research Asia recruits full-time researchers in nlp/ speech synthesis and other directions
統計php程序運行時間及設置PHP最長運行時間
Judge whether a number is a prime number (prime number)
哈趣K1和哈趣H1哪个性价比更高?谁更值得入手?
Which is more cost-effective, haqu K1 or haqu H1? Who is more worth starting with?
MySQL之知识点(六)
mysql如何使用JSON_EXTRACT()取json值
漏洞复现----48、Airflow dag中的命令注入(CVE-2020-11978)
Debug kernel code through proc interface
C#实现水晶报表绑定数据并实现打印3-二维码条形码
漫画:如何实现大整数相乘?(整合版)
Using C language to realize palindrome number