当前位置:网站首页>LeetCode121. The best time to buy and sell stocks
LeetCode121. The best time to buy and sell stocks
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 prices , It's the first i Elements prices[i] Represents the number of shares in a given stock i Sky price .
You can only choose One day Buy this stock , And choose A different day in the future Sell the stock . Design an algorithm to calculate the maximum profit you can get .
Return the maximum profit you can make from the deal . If you can't make any profit , return 0 .
Example 1:
Input :[7,1,5,3,6,4]
Output :5
explain : In the 2 God ( Stock price = 1) Buy when , In the 5 God ( Stock price = 6) Sell when , Maximum profit = 6-1 = 5 .
Note that profit cannot be 7-1 = 6, Because the selling price needs to be higher than the buying price ; meanwhile , You can't sell stocks before you buy them .Example 2:
Input :prices = [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 <= 1050 <= prices[i] <= 104
Related Topics
- Array
- Dynamic programming
\n
- 1447
- 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(- prices[i], arr[i - 1][1]);
}
return arr[prices.length-1][0];
}Post Views: 225
边栏推荐
- Pyechart drawing multiple Y-axis line graphs
- LeetCode每日一题——522. 最长特殊序列 II
- RT thread thread synchronization and thread communication
- ANR问题--相机相关的debug
- ThreadLocal原理
- 2. integrate filter
- Resilience4j retry source code analysis and retry index collection
- LeetCode每日一题——324. 摆动排序 II
- LeetCode877. 石子游戏
- LeetCode123. 买卖股票的最佳时机III
猜你喜欢

Figure neural network can also be used as CV backbone model. Huawei Noah Vig architecture is comparable to CNN and transformer

如何使用 DataAnt 监控 Apache APISIX

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

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

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

图神经网络也能用作CV骨干模型,华为诺亚ViG架构媲美CNN、Transformer

Learn Tai Chi maker mqtt Chapter 2 (VIII) esp8266 mqtt user password authentication

方 差 分 析

ThreadLocal原理

Query rewriting for opengauss kernel analysis
随机推荐
[try to hack] cobalt strike (I)
LeetCode每日一题——324. 摆动排序 II
Binary tree problems
LeetCode1114. 按序打印
Is it safe to open a dig money account? Is it reliable?
Which is the most reliable and safe for a securities company to open an account
1. integrate servlets
GlobalSign的泛域名SSL证书
Resilience4j retry source code analysis and retry index collection
Leetcode daily question - 324 Swing sort II
LeetCode:合并两个有序链表_21
Alibaba cloud MSE full link grayscale solution practice based on Apache apisik
3. integrate listener
LeetCode每日一题——522. 最长特殊序列 II
Flask - Summary
Learn Tai Chi maker mqtt Chapter 2 (VIII) esp8266 mqtt user password authentication
How to open an account in great wisdom? Is it safe
ANR无响应介绍
LeetCode:合并K个升序链表_23
Web自动化工具选择