当前位置:网站首页>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;
}
};
边栏推荐
猜你喜欢
Leetcode 30. 串联所有单词的子串
中缀表达式转后缀表达式详细思路及代码实现
Systematic and detailed explanation of redis operation hash type data (with source code analysis and test results)
Problems encountered in using RT thread component fish
A popular explanation will help you get started
Interpretation of Dagan paper
Actf 2022 came to a successful conclusion, and 0ops team won the second consecutive championship!!
学习探索-无缝轮播图
It's super detailed in history. It's too late for you to read this information if you want to find a job
IC设计流程中需要使用到的文件
随机推荐
Mysql Information Schema 學習(一)--通用錶
Zero foundation entry polardb-x: build a highly available system and link the big data screen
黑馬--Redis篇
系统性详解Redis操作Hash类型数据(带源码分析及测试结果)
C # - realize serialization with Marshall class
Problems encountered in using RT thread component fish
Unbalance balance (dynamic programming, DP)
Countdown 2 days | live broadcast preview of Tencent cloud message queue data import platform
Solution of commercial supply chain management platform for packaging industry: layout smart supply system and digitally integrate the supply chain of packaging industry
Application of clock wheel in RPC
Druid database connection pool details
ZABBIX proxy server and ZABBIX SNMP monitoring
A method of removing text blur based on pixel repair
[translation] micro survey of cloud native observation ability. Prometheus leads the trend, but there are still obstacles to understanding the health of the system
利用 clip-path 绘制不规则的图形
A full set of teaching materials, real questions of Android interview of 7 major manufacturers including Alibaba Kwai pinduoduo
Mysql Information Schema 学习(一)--通用表
Synchronous development of business and application: strategic suggestions for application modernization
GCC【7】- 编译检查的是函数的声明,链接检查的是函数的定义bug
蓝桥杯 微生物增殖 C语言