当前位置:网站首页>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;
}
};
边栏推荐
- LeetCode-1279. Traffic light intersection
- Solution of commercial supply chain management platform for packaging industry: layout smart supply system and digitally integrate the supply chain of packaging industry
- English topic assignment (25)
- ModuleNotFoundError: No module named ‘PIL‘解决方法
- 【翻译】供应链安全项目in-toto移至CNCF孵化器
- usb host 驱动 - UVC 掉包
- Mysql Information Schema 學習(一)--通用錶
- 【翻译】数字内幕。KubeCon + CloudNativeCon在2022年欧洲的选择过程
- Spark foundation -scala
- Druid database connection pool details
猜你喜欢
Pytorch common loss function
接雨水问题解析
三面蚂蚁金服成功拿到offer,Android开发社招面试经验
零基础入门PolarDB-X:搭建高可用系统并联动数据大屏
An error occurs when installing MySQL: could not create or access the registry key needed for the
[玩转Linux] [Docker] MySQL安装和配置
A full set of teaching materials, real questions of Android interview of 7 major manufacturers including Alibaba Kwai pinduoduo
CCNP Part 11 BGP (III) (essence)
【计算情与思】扫地僧、打字员、信息恐慌与奥本海默
JDBC详解
随机推荐
Actf 2022 came to a successful conclusion, and 0ops team won the second consecutive championship!!
An error occurs when installing MySQL: could not create or access the registry key needed for the
[translation] linkerd's adoption rate in Europe and North America exceeded istio, with an increase of 118% in 2021.
史上超级详细,想找工作的你还不看这份资料就晚了
倒计时2天|腾讯云消息队列数据接入平台(Data Import Platform)直播预告
快速幂模板求逆元,逆元的作用以及例题【第20届上海大学程序设计联赛夏季赛】排列计数
Documents to be used in IC design process
Tensorflow and torch code verify whether CUDA is successfully installed
Swiftui game source code Encyclopedia of Snake game based on geometryreader and preference
黑馬--Redis篇
Unbalance balance (dynamic programming, DP)
How can my Haskell program or library find its version number- How can my Haskell program or library find its version number?
Mathematical knowledge -- code implementation of Gaussian elimination (elementary line transformation to solve equations)
测试用里hi
CF960G - Bandit Blues(第一类斯特林数+OGF)
Leetcode 30. 串联所有单词的子串
tensorflow和torch代码验证cuda是否安装成功
LeetCode-1279. Traffic light intersection
[translation] a GPU approach to particle physics
R language ggplot2 visualization: use the ggdotplot function of ggpubr package to visualize dot plot, set the palette parameter, and set the colors of data points and box graphs of dot plots at differ