当前位置:网站首页>LeetCode123. The best time to buy and sell stocks III
LeetCode123. The best time to buy and sell stocks III
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 An element is a given stock in the i Sky price .
Design an algorithm to calculate the maximum profit you can get . You can do at most Two pens transaction .
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 :prices = [3,3,5,0,0,3,1,4]
Output :6
explain : In the 4 God ( Stock price = 0) Buy when , In the 6 God ( Stock price = 3) Sell when , The exchange will make a profit = 3-0 = 3 .
And then , In the 7 God ( Stock price = 1) Buy when , In the 8 God ( Stock price = 4) Sell when , The exchange will make a profit = 4-1 = 3 .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 .
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 :prices = [7,6,4,3,1]
Output :0
explain : In this case , No deal is done , So the biggest profit is 0.Example 4:
Input :prices = [1]
Output :0Tips :
1 <= prices.length <= 1050 <= prices[i] <= 105
Related Topics
- Array
- Dynamic programming
\n
- 691
- 0
3、 ... and 、 Code
public int maxProfit(int[] prices) {
if (prices.length < 2) {
return 0;
}
int[][][] arr = new int[prices.length][3][2];
arr[0][2][1] = -prices[0];
arr[0][2][0] = 0;
arr[0][1][1] = -prices[0];
arr[0][1][0] = 0;
arr[0][0][0] = 0;
for (int i = 1; i < prices.length; i++) {
arr[i][2][0] = Math.max(arr[i - 1][2][0], arr[i - 1][2][1] + prices[i]);
arr[i][2][1] = Math.max(arr[i - 1][2][1], arr[i - 1][1][0] - prices[i]);
arr[i][1][0] = Math.max(arr[i - 1][1][0], arr[i - 1][1][1] + prices[i]);
arr[i][1][1] = Math.max(arr[i - 1][1][1], arr[i - 1][0][0] - prices[i]);
}
return arr[prices.length - 1][2][0];
}Post Views: 233
边栏推荐
- Analysis of variance
- Leetcode daily question - 515 Find the maximum value in each tree row
- LeetCode121. 买卖股票的最佳时机
- 什么是接口?什么是接口测试?
- 题解 Andy s First Dictionary(UVa10815)紫书P112set的应用
- APISIX 助力中东社交软件,实现本地化部署
- 学习太极创客 — MQTT 第二章(七)ESP8266 MQTT 遗嘱应用
- Globalsign's Pan domain SSL certificate
- The blocks problem (uva101) Purple Book p110vector application
- Anr no response introduction
猜你喜欢

【Try to Hack】Cobalt Strike(一)

The principle and source code analysis of Lucene index construction

On the complexity of software development and the way to improve its efficiency

pyechart绘制多条y轴折线图

Pie (poj3122) super detailed and easy to understand binary introduction

Visualization of neural network structure in different frames

openGauss内核分析之查询重写

Alibaba cloud MSE full link grayscale solution practice based on Apache apisik

MongoDB——副本集与分片

Query rewriting for opengauss kernel analysis
随机推荐
如何添加 logs来debug ANR 问题
Please allow the "imperfection" of the current domestic Tob
[try to hack] cobalt strike (I)
Comparisonchain file name sort
Embedded dynamic Arabic string conversion LCD display string [thanks for Jianguo ambition]
Bitbucket 使用 SSH 拉取仓库失败的问题
Characters and integers
力扣树的进一步应用
2. integrate filter
穩定性總結
Anr analysis - question 1
Pie (poj3122) super detailed and easy to understand binary introduction
【学习笔记】主成分分析法介绍
Apisik helps Middle East social software realize localized deployment
I almost ran away
MySQL system error occurred 1067
Various types of long
LeetCode188. 买卖股票的最佳时机IV
ANR无响应介绍
How to analyze the relationship between enterprise digital transformation and data asset management?