当前位置:网站首页>LeetCode122. The best time to buy and sell stocks II
LeetCode122. The best time to buy and sell stocks II
2022-06-28 21:05:00 【Yuyy】
This paper is finally updated at 484 Days ago, , The information may have developed or changed .
One 、 Ideas
Find the selection and status
Two 、 problem
Given an array , It's the first i Element (s) of a given stock i Sky price .
Design an algorithm to calculate the maximum profit you can get . You can do as many deals as you can ( Buy and sell a stock many times ).
Be careful : You can't participate in multiple transactions at the same time ( You have to sell the stock before you buy it again ).
Example 1:
Input : [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 .Example 2:
Input : [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 .
Notice that you can't be in the 1 Day and day 2 Day after day buying stock , Then sell them .
Because it's involved in multiple transactions at the same time , You have to sell the stock before you buy it again .Example 3:
Input : [7,6,4,3,1]
Output : 0
explain : under these circumstances , No deal is done , So the biggest profit is 0.Tips :
1 <= prices.length <= 3 * 10 ^ 40 <= prices[i] <= 10 ^ 4
Related Topics
- Greedy Algorithm
- Array
\n
- 1107
- 0
3、 ... and 、 Code
public int maxProfit(int[] prices) {
if (prices.length < 2) {
return 0;
}
int[][] arr = new int[prices.length][2];
arr[0][1] = -prices[0];
arr[0][0] = 0;
for (int i = 1; i < prices.length; i++) {
arr[i][0] = Math.max(arr[i - 1][1] + prices[i], arr[i - 1][0]);
arr[i][1] = Math.max(arr[i - 1][0] - prices[i], arr[i - 1][1]);
}
return arr[prices.length - 1][0];
}Post Views: 317
边栏推荐
- Why does next() in iterator need to be forcibly converted?
- The further application of Li Kou tree
- 大智慧上怎么进行开户啊, 安全吗
- Leetcode daily question - 30 Concatenate substrings of all words
- input separator
- ref属性,props配置,mixin混入,插件,scoped样式
- Characters and integers
- [learning notes] cluster analysis
- 基于 Apache APISIX 的自动化运维平台
- RT thread thread synchronization and thread communication
猜你喜欢

MongoDB——副本集与分片

RT thread thread synchronization and thread communication

Application practice | 1billion data second level correlation. Huolala's OLAP System Evolution Based on Apache Doris (with PPT download)

ThreadLocal原理

openGauss内核分析之查询重写

Mongodb - replica set and sharding

方 差 分 析

ref属性,props配置,mixin混入,插件,scoped样式

不同框架的绘制神经网络结构可视化

API gateway Apache APIs IX helps the evolution of snowball dual active architecture
随机推荐
Relevant calculation of sphere, etc
Flask——总结
How to recover after Oracle delete accidentally deletes table data
Real number operation
大智慧上怎么进行开户啊, 安全吗
Learn Tai Chi maker mqtt Chapter 2 (VIII) esp8266 mqtt user password authentication
LeetCode:二叉树展开为链表_114
T检验(检验两个总体的均值差异是否显著)
接口测试流程
2. integrate filter
二叉树类题目 力扣
pyechart绘制多条y轴折线图
LeetCode每日一题——515. 在每个树行中找最大值
Alibaba cloud MSE full link grayscale solution practice based on Apache apisik
with torch.no_grad():的使用原因
Figure neural network can also be used as CV backbone model. Huawei Noah Vig architecture is comparable to CNN and transformer
学习太极创客 — MQTT 第二章(七)ESP8266 MQTT 遗嘱应用
嵌入式中 动态阿拉伯语字符串 转换 LCD显示字符串【感谢建国雄心】
ANR分析--问题1
GlobalSign的泛域名SSL证书