当前位置:网站首页>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;
}
};
边栏推荐
- [translation] micro survey of cloud native observation ability. Prometheus leads the trend, but there are still obstacles to understanding the health of the system
- Solution of commercial supply chain management platform for packaging industry: layout smart supply system and digitally integrate the supply chain of packaging industry
- Excel 中VBA脚本的简单应用
- 【基础架构】Flink/Flink-CDC的部署和配置(MySQL / ES)
- swagger2报错Illegal DefaultValue null for parameter type integer
- Use of map (the data of the list is assigned to the form, and the JSON comma separated display assignment)
- Actf 2022 came to a successful conclusion, and 0ops team won the second consecutive championship!!
- 终于可以一行代码也不用改了!ShardingSphere 原生驱动问世
- An error occurs when installing MySQL: could not create or access the registry key needed for the
- 在解决了 2961 个用户反馈后,我做出了这样的改变...
猜你喜欢
Analysis of rainwater connection
Vmware虚拟机无法打开内核设备“\\.\Global\vmx86“的解决方法
Mathematical knowledge -- code implementation of Gaussian elimination (elementary line transformation to solve equations)
打家劫舍III[后序遍历与回溯+动态规划]
Systematic and detailed explanation of redis operation hash type data (with source code analysis and test results)
手把手教你学会js的原型与原型链,猴子都能看懂的教程
受益匪浅,安卓面试问题
Cereals Mall - Distributed Advanced p129~p339 (end)
zabbix 代理服务器 与 zabbix-snmp 监控
Mysql Information Schema 學習(一)--通用錶
随机推荐
[translation] supply chain security project in toto moved to CNCF incubator
GCC【7】- 编译检查的是函数的声明,链接检查的是函数的定义bug
Yyds dry goods inventory leetcode question set 751 - 760
How to customize animation avatars? These six free online cartoon avatar generators are exciting at a glance!
Interview assault 63: how to remove duplication in MySQL?
手把手教你学会js的原型与原型链,猴子都能看懂的教程
LeetCode_ Gray code_ Medium_ 89. Gray code
CF960G - Bandit Blues(第一类斯特林数+OGF)
Using clip path to draw irregular graphics
时钟轮在 RPC 中的应用
凤凰架构3——事务处理
Actf 2022 came to a successful conclusion, and 0ops team won the second consecutive championship!!
It's super detailed in history. It's too late for you to read this information if you want to find a job
LeetCode_格雷编码_中等_89.格雷编码
RT-Thread 组件 FinSH 使用时遇到的问题
Carte de réflexion + code source + notes + projet, saut d'octets + jd + 360 + tri des questions d'entrevue Netease
倒计时2天|腾讯云消息队列数据接入平台(Data Import Platform)直播预告
Hudi vs Delta vs Iceberg
swagger2报错Illegal DefaultValue null for parameter type integer
Interpretation of Dagan paper