当前位置:网站首页>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;
}
};
边栏推荐
- 快速幂模板求逆元,逆元的作用以及例题【第20届上海大学程序设计联赛夏季赛】排列计数
- Mind map + source code + Notes + project, ByteDance + JD +360+ Netease interview question sorting
- Reflection and illegalaccessexception exception during application
- Simple understanding of MySQL database
- 【翻译】数字内幕。KubeCon + CloudNativeCon在2022年欧洲的选择过程
- Countdown 2 days | live broadcast preview of Tencent cloud message queue data import platform
- 通俗的讲解,带你入门协程
- 【基础架构】Flink/Flink-CDC的部署和配置(MySQL / ES)
- [玩转Linux] [Docker] MySQL安装和配置
- spark基础-scala
猜你喜欢

力扣101题:对称二叉树

Hudi vs Delta vs Iceberg

Leetcode 30. 串联所有单词的子串

Cereals Mall - Distributed Advanced p129~p339 (end)

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

通俗的讲解,带你入门协程
Benefit a lot, Android interview questions

Druid database connection pool details

Black Horse - - Redis Chapter

Take a look at how cabloyjs workflow engine implements activiti boundary events
随机推荐
Using clip path to draw irregular graphics
面试突击63:MySQL 中如何去重?
Help improve the professional quality of safety talents | the first stage of personal ability certification and assessment has been successfully completed!
A method of removing text blur based on pixel repair
How to type multiple spaces when editing CSDN articles
Systematic and detailed explanation of redis operation hash type data (with source code analysis and test results)
Is not a drawable (color or path): the vector graph downloaded externally cannot be called when it is put into mipmap, and the calling error program crashes
【翻译】数字内幕。KubeCon + CloudNativeCon在2022年欧洲的选择过程
How to do smoke test
学习探索-函数防抖
DaGAN论文解读
Use of map (the data of the list is assigned to the form, and the JSON comma separated display assignment)
Modulenotfounderror: no module named 'PIL' solution
手把手教你学会js的原型与原型链,猴子都能看懂的教程
通俗的讲解,带你入门协程
Mysql Information Schema 学习(一)--通用表
MySQL information Schema Learning (i) - - General table
CPU负载很低,loadavg很高处理方法
关于图像的读取及处理等
MySql必知必会学习