当前位置:网站首页>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
边栏推荐
- PC电脑使用无线网卡连接上手机热点,为什么不能上网
- Promise (III)
- 【生活中的逻辑谬误】稻草人谬误和无力反驳不算证明
- 双指针进阶--leetcode题目--盛最多水的容器
- 48. 旋转图像
- ASP. Net core reads the configuration file in the class library project
- 为什么有些人说单片机简单,我学起来这么吃力?
- 394. 字符串解码-辅助栈
- ASP. NET CORE3. 1. Solution to login failure after identity registers users
- Use of shell cut command
猜你喜欢
![Using quartz under. Net core -- general properties and priority of triggers for [5] jobs and triggers](/img/65/89473397da4217201eeee85aef3c10.png)
Using quartz under. Net core -- general properties and priority of triggers for [5] jobs and triggers

Collection of common SQL statements

Signalr can actively send data from the server to the client

STM32 entry development board choose wildfire or punctual atom?

.Net Core3. 1 use razorengine NETCORE production entity generator (MVC web version)

ASP. Net core dependency injection service life cycle

C# Task. Delay and thread The difference between sleep

ASP. NET CORE3. 1. Solution to login failure after identity registers users

Tdan over half

C语言函数详解
随机推荐
Baidu Map Case - modify map style
01-初识sketch-sketch优势
[simple understanding of database]
Allowed latency and side output
Preliminary understanding of promse
[C] thoroughly understand the deep copy
Basic case of Baidu map
2. Electron's HelloWorld
Node template engine (EJS, art template)
Some problems encountered in recent programming 2021 / 9 / 8
uni-app黑马优购项目学习记录(下)
超分之TDAN
索引:手把手教你索引从零基础到精通使用
122. 买卖股票的最佳时机 II-一次遍历
386. 字典序排数(中等)-迭代-全排列
C# Task. Delay and thread The difference between sleep
matlab如何绘制已知公式的曲线图,Excel怎么绘制函数曲线图像?
[WPF binding 3] listview basic binding and data template binding
The system cannot be started after AHCI is enabled
Excel quickly and automatically fills the contents of a row on a blank cell