当前位置:网站首页>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;
}
};
边栏推荐
- It's super detailed in history. It's too late for you to read this information if you want to find a job
- An error occurs when installing MySQL: could not create or access the registry key needed for the
- Yyds dry goods inventory leetcode question set 751 - 760
- tensorflow和torch代码验证cuda是否安装成功
- Benefit a lot, Android interview questions
- 测试用里hi
- short i =1; i=i+1与short i=1; i+=1的区别
- [translation] Digital insider. Selection process of kubecon + cloudnativecon in Europe in 2022
- Swagger2 reports an error illegal DefaultValue null for parameter type integer
- 系统性详解Redis操作Hash类型数据(带源码分析及测试结果)
猜你喜欢
Hudi vs Delta vs Iceberg
Synchronous development of business and application: strategic suggestions for application modernization
Mysql Information Schema 學習(一)--通用錶
思維導圖+源代碼+筆記+項目,字節跳動+京東+360+網易面試題整理
Solution of commercial supply chain management platform for packaging industry: layout smart supply system and digitally integrate the supply chain of packaging industry
Actf 2022 came to a successful conclusion, and 0ops team won the second consecutive championship!!
[translation] micro survey of cloud native observation ability. Prometheus leads the trend, but there are still obstacles to understanding the health of the system
学习探索-无缝轮播图
如何自定义动漫头像?这6个免费精品在线卡通头像生成器,看一眼就怦然心动!
How to customize animation avatars? These six free online cartoon avatar generators are exciting at a glance!
随机推荐
Leetcode topic [array] - 119 Yang Hui triangle II
Mathematical knowledge -- code implementation of Gaussian elimination (elementary line transformation to solve equations)
Help improve the professional quality of safety talents | the first stage of personal ability certification and assessment has been successfully completed!
Simple understanding of MySQL database
打家劫舍III[后序遍历与回溯+动态规划]
Systematic and detailed explanation of redis operation hash type data (with source code analysis and test results)
Using clip path to draw irregular graphics
学习探索-函数防抖
Low CPU load and high loadavg processing method
学习探索-使用伪元素清除浮动元素造成的高度坍塌
map的使用(列表的数据赋值到表单,json逗号隔开显示赋值)
Actf 2022 came to a successful conclusion, and 0ops team won the second consecutive championship!!
思維導圖+源代碼+筆記+項目,字節跳動+京東+360+網易面試題整理
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
Vmware虚拟机无法打开内核设备“\\.\Global\vmx86“的解决方法
A method of removing text blur based on pixel repair
How to type multiple spaces when editing CSDN articles
[translation] linkerd's adoption rate in Europe and North America exceeded istio, with an increase of 118% in 2021.
凤凰架构3——事务处理
Looting iii[post sequence traversal and backtracking + dynamic planning]