当前位置:网站首页>121. The best time to buy and sell stocks
121. The best time to buy and sell stocks
2022-07-06 19:36:00 【yitahutu79】
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 <= 105
0 <= prices[i] <= 104
class Solution {
public:
int maxProfit(vector<int>& prices) {
int n = prices.size();
int minprice = prices[0];
int maxprofit = 0;
for (int i = 1; i < n; i++) {
maxprofit = max(maxprofit, prices[i] - minprice);
minprice = min(minprice,prices[i]);
}
return maxprofit;
}
};
边栏推荐
- swagger2报错Illegal DefaultValue null for parameter type integer
- [translation] linkerd's adoption rate in Europe and North America exceeded istio, with an increase of 118% in 2021.
- Leetcode topic [array] - 119 Yang Hui triangle II
- Benefit a lot, Android interview questions
- 10 schemes to ensure interface data security
- MRO industrial products enterprise procurement system: how to refine procurement collaborative management? Industrial products enterprises that want to upgrade must see!
- [pytorch] yolov5 train your own data set
- [translation] micro survey of cloud native observation ability. Prometheus leads the trend, but there are still obstacles to understanding the health of the system
- Vmware虚拟机无法打开内核设备“\\.\Global\vmx86“的解决方法
- 深入分析,Android面试真题解析火爆全网
猜你喜欢
![[translation] micro survey of cloud native observation ability. Prometheus leads the trend, but there are still obstacles to understanding the health of the system](/img/63/3addcecb69dcb769c4736653952f66.png)
[translation] micro survey of cloud native observation ability. Prometheus leads the trend, but there are still obstacles to understanding the health of the system

PMP practice once a day | don't get lost in the exam -7.6
In depth analysis, Android interview real problem analysis is popular all over the network

凤凰架构3——事务处理

三面蚂蚁金服成功拿到offer,Android开发社招面试经验

How to access localhost:8000 by mobile phone
时钟轮在 RPC 中的应用

Vmware虚拟机无法打开内核设备“\\.\Global\vmx86“的解决方法

Solution of commercial supply chain management platform for packaging industry: layout smart supply system and digitally integrate the supply chain of packaging industry

Spark foundation -scala
随机推荐
Benefit a lot, Android interview questions
Solution of commercial supply chain management platform for packaging industry: layout smart supply system and digitally integrate the supply chain of packaging industry
Solution of intelligent management platform for suppliers in hardware and electromechanical industry: optimize supply chain management and drive enterprise performance growth
Simple application of VBA script in Excel
LeetCode_格雷编码_中等_89.格雷编码
MySQL information Schema Learning (i) - - General table
关于图像的读取及处理等
如何自定义动漫头像?这6个免费精品在线卡通头像生成器,看一眼就怦然心动!
php+redis实现超时取消订单功能
In depth analysis, Android interview real problem analysis is popular all over the network
Spark foundation -scala
零基础入门PolarDB-X:搭建高可用系统并联动数据大屏
How can my Haskell program or library find its version number- How can my Haskell program or library find its version number?
力扣101题:对称二叉树
Synchronous development of business and application: strategic suggestions for application modernization
Low CPU load and high loadavg processing method
C # - realize serialization with Marshall class
利用 clip-path 绘制不规则的图形
学习探索-使用伪元素清除浮动元素造成的高度坍塌
通俗的讲解,带你入门协程