当前位置:网站首页>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;
}
};
边栏推荐
- Using clip path to draw irregular graphics
- Computer network: sorting out common network interview questions (I)
- spark基础-scala
- Vmware虚拟机无法打开内核设备“\\.\Global\vmx86“的解决方法
- 今日直播 | “人玑协同 未来已来”2022弘玑生态伙伴大会蓄势待发
- The second day of rhcsa study
- C # - realize serialization with Marshall class
- 反射及在运用过程中出现的IllegalAccessException异常
- Php+redis realizes the function of canceling orders over time
- Looting iii[post sequence traversal and backtracking + dynamic planning]
猜你喜欢
Dark horse -- redis
Mysql Information Schema 学习(一)--通用表
MySQL information Schema Learning (i) - - General table
Looting iii[post sequence traversal and backtracking + dynamic planning]
zabbix 代理服务器 与 zabbix-snmp 监控
CCNP Part 11 BGP (III) (essence)
Mysql Information Schema 學習(一)--通用錶
面试突击63:MySQL 中如何去重?
JDBC details
Fast power template for inverse element, the role of inverse element and example [the 20th summer competition of Shanghai University Programming League] permutation counting
随机推荐
Mysql Information Schema 学习(二)--Innodb表
Synchronous development of business and application: strategic suggestions for application modernization
谷粒商城--分布式高级篇P129~P339(完结)
Mysql Information Schema 學習(一)--通用錶
Unbalance balance (dynamic programming, DP)
史上超级详细,想找工作的你还不看这份资料就晚了
Detailed idea and code implementation of infix expression to suffix expression
Use of deg2rad and rad2deg functions in MATLAB
C # - realize serialization with Marshall class
Swiftui game source code Encyclopedia of Snake game based on geometryreader and preference
如何自定义动漫头像?这6个免费精品在线卡通头像生成器,看一眼就怦然心动!
MySQL information schema learning (I) -- general table
主从搭建报错:The slave I/O thread stops because master and slave have equal MySQL serv
三面蚂蚁金服成功拿到offer,Android开发社招面试经验
LeetCode_ Double pointer_ Medium_ 61. rotating linked list
[translation] a GPU approach to particle physics
保证接口数据安全的10种方案
【翻译】供应链安全项目in-toto移至CNCF孵化器
MRO industrial products enterprise procurement system: how to refine procurement collaborative management? Industrial products enterprises that want to upgrade must see!
MySql必知必会学习