当前位置:网站首页>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;
}
};
边栏推荐
- Mind map + source code + Notes + project, ByteDance + JD +360+ Netease interview question sorting
- Unbalance balance (dynamic programming, DP)
- 零基础入门PolarDB-X:搭建高可用系统并联动数据大屏
- 通俗的讲解,带你入门协程
- 黑馬--Redis篇
- Fast power template for inverse element, the role of inverse element and example [the 20th summer competition of Shanghai University Programming League] permutation counting
- 利用 clip-path 绘制不规则的图形
- LeetCode-1279. Traffic light intersection
- Carte de réflexion + code source + notes + projet, saut d'octets + jd + 360 + tri des questions d'entrevue Netease
- 终于可以一行代码也不用改了!ShardingSphere 原生驱动问世
猜你喜欢
反射及在运用过程中出现的IllegalAccessException异常
思維導圖+源代碼+筆記+項目,字節跳動+京東+360+網易面試題整理
系统性详解Redis操作Hash类型数据(带源码分析及测试结果)
Interview assault 63: how to remove duplication in MySQL?
Help improve the professional quality of safety talents | the first stage of personal ability certification and assessment has been successfully completed!
在解决了 2961 个用户反馈后,我做出了这样的改变...
凤凰架构3——事务处理
[玩转Linux] [Docker] MySQL安装和配置
Intelligent supply chain management system solution for hardware and electromechanical industry: digital intelligent supply chain "creates new blood" for traditional industries
【翻译】Linkerd在欧洲和北美的采用率超过了Istio,2021年增长118%。
随机推荐
安装Mysql报错:Could not create or access the registry key needed for the...
RT-Thread 组件 FinSH 使用时遇到的问题
Use of map (the data of the list is assigned to the form, and the JSON comma separated display assignment)
Leetcode topic [array] - 119 Yang Hui triangle II
潇洒郎: AttributeError: partially initialized module ‘cv2‘ has no attribute ‘gapi_wip_gst_GStreamerPipe
Yyds dry goods inventory leetcode question set 751 - 760
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
Elastic search indexes are often deleted [closed] - elastic search indexes gets deleted frequently [closed]
The dplyr package of R language performs data grouping aggregation statistical transformations and calculates the grouping mean of dataframe data
[translation] linkerd's adoption rate in Europe and North America exceeded istio, with an increase of 118% in 2021.
short i =1; i=i+1与short i=1; i+=1的区别
Php+redis realizes the function of canceling orders over time
A full set of teaching materials, real questions of Android interview of 7 major manufacturers including Alibaba Kwai pinduoduo
MATLAB中deg2rad和rad2deg函数的使用
spark基础-scala
数学知识——高斯消元(初等行变换解方程组)代码实现
Solution of intelligent management platform for suppliers in hardware and electromechanical industry: optimize supply chain management and drive enterprise performance growth
zabbix 代理服务器 与 zabbix-snmp 监控
凤凰架构3——事务处理
【翻译】数字内幕。KubeCon + CloudNativeCon在2022年欧洲的选择过程