当前位置:网站首页>122. The best time to buy and sell stocks II - one-time traversal
122. The best time to buy and sell stocks II - one-time traversal
2022-04-23 17:32:00 【hequnwang10】
One 、 Title Description
Give you an array of integers prices , among prices[i] Represents a stock i Sky price .
Every day , You can decide whether to buy and / Or sell shares . You at any time most Can only hold A wave of Stocks . You can also buy... First , And then in On the same day sell .
return What you can get Maximum profits .
Example 1:
Input :prices = [7,1,5,3,6,4]
Output :7
explain : In the 2 God ( Stock price = 1) Buy when , In the 3 God ( Stock price = 5) Sell when , The exchange will make a profit = 5 - 1 = 4 .
And then , In the 4 God ( Stock price = 3) Buy when , In the 5 God ( Stock price = 6) Sell when , The exchange will make a profit = 6 - 3 = 3 .
The total profit is 4 + 3 = 7 .
Example 2:
Input :prices = [1,2,3,4,5]
Output :4
explain : In the 1 God ( Stock price = 1) Buy when , In the 5 God ( Stock price = 5) Sell when , The exchange will make a profit = 5 - 1 = 4 .
The total profit is 4 .
Example 2:
Input :prices = [7,6,4,3,1]
Output :0
explain : under these circumstances , There is no positive profit from trading , So you can get the maximum profit by not participating in the transaction , The biggest profit is 0 .
Two 、 Problem solving
One traverse
Find the rising range
class Solution {
public int maxProfit(int[] prices) {
if(prices == null){
return 0;
}
// Use one traversal This is a multiple transaction And each transaction cannot be crossed
// So just count every rise directly Statistical maximum profit
// Find the rising range
int length = prices.length;
int maxProfit = 0;
for(int i = 1;i<length;i++){
if(prices[i] - prices[i-1] > 0){
maxProfit += prices[i] - prices[i-1];
}
}
return maxProfit;
}
}
版权声明
本文为[hequnwang10]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231732009518.html
边栏推荐
- 圆环回原点问题-字节跳动高频题
- node中,如何手动实现触发垃圾回收机制
- 402. 移掉 K 位数字-贪心
- 122. 买卖股票的最佳时机 II-一次遍历
- Allowed latency and side output
- Using quartz under. Net core -- general properties and priority of triggers for [5] jobs and triggers
- Clickhouse SQL operation
- MySQL installation
- El cascade and El select click elsewhere to make the drop-down box disappear
- C listens for WMI events
猜你喜欢
超分之TDAN
440. 字典序的第K小数字(困难)-字典树-数节点-字节跳动高频题
[difference between Oracle and MySQL]
STM32 entry development board choose wildfire or punctual atom?
QT modification UI does not take effect
1217_使用SCons生成目标文件
In embedded system, must the program code in flash be moved to ram to run?
Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]
394. 字符串解码-辅助栈
Perception of linear algebra 2
随机推荐
双指针进阶--leetcode题目--盛最多水的容器
ClickHouse-SQL 操作
Baidu Map Case - Zoom component, map scale component
Collection of common SQL statements
ASP. Net core configuration options (Part 1)
ASP. NET CORE3. 1. Solution to login failure after identity registers users
[logical fallacy in life] Scarecrow fallacy and inability to refute are not proof
Signalr can actively send data from the server to the client
Header built-in object
读《Software Engineering at Google》(15)
1-3 components and modules
uni-app黑马优购项目学习记录(下)
2. Electron's HelloWorld
Generating access keys using JSON webtoken
线性代数感悟之1
How to manually implement the mechanism of triggering garbage collection in node
Qt error: /usr/bin/ld: cannot find -lGL: No such file or directory
[WPF binding 3] listview basic binding and data template binding
【生活中的逻辑谬误】稻草人谬误和无力反驳不算证明
Node template engine (EJS, art template)