当前位置:网站首页>121. 买卖股票的最佳时机
121. 买卖股票的最佳时机
2022-07-06 11:36:00 【yitahutu79】
给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。
你只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票。设计一个算法来计算你所能获取的最大利润。
返回你可以从这笔交易中获取的最大利润。如果你不能获取任何利润,返回 0 。
示例 1:
输入:[7,1,5,3,6,4]
输出:5
解释:在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。
注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格;同时,你不能在买入前卖出股票。
示例 2:
输入:prices = [7,6,4,3,1]
输出:0
解释:在这种情况下, 没有交易完成, 所以最大利润为 0。
提示:
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;
}
};
边栏推荐
- 打家劫舍III[后序遍历与回溯+动态规划]
- Xingnuochi technology's IPO was terminated: it was planned to raise 350million yuan, with an annual revenue of 367million yuan
- MySQL information Schema Learning (i) - - General table
- Benefit a lot, Android interview questions
- Tensorflow2.0 自定义训练的方式求解函数系数
- Take a look at how cabloyjs workflow engine implements activiti boundary events
- Solution of commercial supply chain management platform for packaging industry: layout smart supply system and digitally integrate the supply chain of packaging industry
- 反射及在运用过程中出现的IllegalAccessException异常
- 【基础架构】Flink/Flink-CDC的部署和配置(MySQL / ES)
- Documents to be used in IC design process
猜你喜欢

LeetCode_双指针_中等_61. 旋转链表

Problems encountered in using RT thread component fish

Don't miss this underestimated movie because of controversy!

Mathematical knowledge -- code implementation of Gaussian elimination (elementary line transformation to solve equations)

RT-Thread 组件 FinSH 使用时遇到的问题
Benefit a lot, Android interview questions

Cereals Mall - Distributed Advanced p129~p339 (end)

全套教学资料,阿里快手拼多多等7家大厂Android面试真题

Mysql Information Schema 学习(一)--通用表

Php+redis realizes the function of canceling orders over time
随机推荐
Black Horse - - Redis Chapter
How to do smoke test
Countdown 2 days | live broadcast preview of Tencent cloud message queue data import platform
Detailed idea and code implementation of infix expression to suffix expression
学习探索-无缝轮播图
10 schemes to ensure interface data security
The dplyr package of R language performs data grouping aggregation statistical transformations and calculates the grouping mean of dataframe data
php+redis实现超时取消订单功能
tensorflow和torch代码验证cuda是否安装成功
PMP practice once a day | don't get lost in the exam -7.6
Mysql Information Schema 學習(一)--通用錶
如何自定义动漫头像?这6个免费精品在线卡通头像生成器,看一眼就怦然心动!
Mind map + source code + Notes + project, ByteDance + JD +360+ Netease interview question sorting
Lick the dog until the last one has nothing (simple DP)
安装Mysql报错:Could not create or access the registry key needed for the...
English topic assignment (25)
Modulenotfounderror: no module named 'PIL' solution
【pytorch】yolov5 训练自己的数据集
Mathematical knowledge -- code implementation of Gaussian elimination (elementary line transformation to solve equations)
Mysql Information Schema 学习(二)--Innodb表