当前位置:网站首页>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
边栏推荐
- 394. 字符串解码-辅助栈
- ASP. Net core reads the configuration file in the class library project
- 1217_使用SCons生成目标文件
- STM32 entry development board choose wildfire or punctual atom?
- 练习:求偶数和、阈值分割和求差( list 对象的两个基础小题)
- Advantages and disadvantages of several note taking software
- [ES6] promise related (event loop, macro / micro task, promise, await / await)
- Solution of Navicat connecting Oracle library is not loaded
- stm32入门开发板选野火还是正点原子呢?
- How to change input into text
猜你喜欢
ASP. Net core dependency injection service life cycle
Using quartz under. Net core -- operation transfer parameters of [3] operation and trigger
Using quartz under. Net core -- general properties and priority of triggers for [5] jobs and triggers
PC电脑使用无线网卡连接上手机热点,为什么不能上网
1217_使用SCons生成目标文件
ClickHouse-表引擎
双指针进阶--leetcode题目--盛最多水的容器
线性代数感悟之1
In embedded system, must the program code in flash be moved to ram to run?
XTask与Kotlin Coroutine的使用对比
随机推荐
[difference between Oracle and MySQL]
剑指 Offer 22. 链表中倒数第k个节点-快慢指针
開期貨,開戶雲安全還是相信期貨公司的軟件?
Future 用法详解
圆环回原点问题-字节跳动高频题
[ES6] promise related (event loop, macro / micro task, promise, await / await)
Using quartz under. Net core - [1] quick start
Shell-入门、变量、以及基本的语法
Shell-awk命令的使用
Webapi + form form upload file
Generating access keys using JSON webtoken
. net cross platform principle (Part I)
Qt 修改UI没有生效
[C] thoroughly understand the deep copy
嵌入式系统中,FLASH中的程序代码必须搬到RAM中运行吗?
Further optimize Baidu map data visualization
.Net Core3. 1 use razorengine NETCORE production entity generator (MVC web version)
Baidu Map Case - modify map style
Clickhouse SQL operation
Matlab / Simulink simulation of double closed loop DC speed regulation system