当前位置:网站首页>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
边栏推荐
- Understanding of incomplete types
- 我也差点“跑路”
- 题解 Ananagrams(UVa156)紫书P113map的应用
- 应用实践 | 10 亿数据秒级关联,货拉拉基于 Apache Doris 的 OLAP 体系演进(附 PPT 下载)
- Leetcode daily question - 30 Concatenate substrings of all words
- 学习太极创客 — MQTT 第二章(八)ESP8266 MQTT 用户密码认证
- 【读书会第13期】视频文件的封装格式
- How to understand the fast iteration of cloud native database?
- MongoDB——副本集与分片
- 关于不完全类型的认识
猜你喜欢

Bitbucket 使用 SSH 拉取仓库失败的问题

Learning Tai Chi Maker - mqtt Chapter II (VII) esp8266 mqtt Testament application

CNN-LSTM的flatten

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

阿里云 MSE 基于 Apache APISIX 的全链路灰度方案实践

MongoDB——副本集与分片

Leetcode daily question - 515 Find the maximum value in each tree row
![[Note: analog MOS integrated circuit] bandgap reference (basic principle + current mode + voltage mode circuit explanation)](/img/cd/be62272d465ca990456c222b38df67.png)
[Note: analog MOS integrated circuit] bandgap reference (basic principle + current mode + voltage mode circuit explanation)

Bitbucket failed to pull the warehouse Using SSH

如何使用 DataAnt 监控 Apache APISIX
随机推荐
with torch. no_ Grad(): reason for using
炒股票能赚钱么?开户安全嘛
The further application of Li Kou tree
pyechart绘制多条y轴折线图
How to do a good job in customer's successful bottom design | tob Master Course
Resilience4j retry source code analysis and retry index collection
Input and output real data
LeetCode每日一题——324. 摆动排序 II
学习太极创客 — MQTT 第二章(七)ESP8266 MQTT 遗嘱应用
视频号如何下载视频?来看超简单方法!
题解 Andy s First Dictionary(UVa10815)紫书P112set的应用
LeetCode123. 买卖股票的最佳时机III
How to add logs to debug anr problems
ID access card copied to mobile phone_ How to turn a mobile phone into an access card mobile NFC copy access card graphic tutorial
学习太极创客 — MQTT 第二章(八)ESP8266 MQTT 用户密码认证
How to recover after Oracle delete accidentally deletes table data
方 差 分 析
How to "calculate" in the age of computing power? The first mover advantage of "convergence of computing and networking" is very important!
ref属性,props配置,mixin混入,插件,scoped样式
基于 Apache APISIX 的自动化运维平台