当前位置:网站首页>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;
}
};
边栏推荐
- How to customize animation avatars? These six free online cartoon avatar generators are exciting at a glance!
- Learning and Exploration - Seamless rotation map
- 三面蚂蚁金服成功拿到offer,Android开发社招面试经验
- 中缀表达式转后缀表达式详细思路及代码实现
- 学习探索-函数防抖
- [translation] Digital insider. Selection process of kubecon + cloudnativecon in Europe in 2022
- 思维导图+源代码+笔记+项目,字节跳动+京东+360+网易面试题整理
- Unbalance balance (dynamic programming, DP)
- 利用 clip-path 绘制不规则的图形
- Learn to explore - use pseudo elements to clear the high collapse caused by floating elements
猜你喜欢

史上超级详细,想找工作的你还不看这份资料就晚了
受益匪浅,安卓面试问题

快速幂模板求逆元,逆元的作用以及例题【第20届上海大学程序设计联赛夏季赛】排列计数

MySQL information schema learning (II) -- InnoDB table

全套教学资料,阿里快手拼多多等7家大厂Android面试真题
In depth analysis, Android interview real problem analysis is popular all over the network

Interpretation of Dagan paper

保证接口数据安全的10种方案

思维导图+源代码+笔记+项目,字节跳动+京东+360+网易面试题整理

Low CPU load and high loadavg processing method
随机推荐
Hudi vs Delta vs Iceberg
【计算情与思】扫地僧、打字员、信息恐慌与奥本海默
A popular explanation will help you get started
Analysis of rainwater connection
MRO industrial products enterprise procurement system: how to refine procurement collaborative management? Industrial products enterprises that want to upgrade must see!
终于可以一行代码也不用改了!ShardingSphere 原生驱动问世
How to access localhost:8000 by mobile phone
CPU负载很低,loadavg很高处理方法
short i =1; i=i+1与short i=1; i+=1的区别
Take a look at how cabloyjs workflow engine implements activiti boundary events
CF960G - Bandit Blues(第一类斯特林数+OGF)
Interview assault 63: how to remove duplication in MySQL?
凤凰架构3——事务处理
LeetCode-1279. Traffic light intersection
Chic Lang: attributeerror: partially initialized module 'CV2' has no attribute 'GAPI_ wip_ gst_ GStreamerPipe
Solution of commercial supply chain management platform for packaging industry: layout smart supply system and digitally integrate the supply chain of packaging industry
Countdown 2 days | live broadcast preview of Tencent cloud message queue data import platform
[translation] linkerd's adoption rate in Europe and North America exceeded istio, with an increase of 118% in 2021.
学习探索-使用伪元素清除浮动元素造成的高度坍塌
Elastic search indexes are often deleted [closed] - elastic search indexes gets deleted frequently [closed]