121. Best Time to Buy and Sell Stock (Array;DP)
Say you have an array for which the ith element is the price of a given stock on day i.
If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.
思路:注意不能简单地将最大值-最小值。卖必须发生在买之后,最小值得在最大值之前。记录下到当前为止最小值。
class Solution {
public:
int maxProfit(vector<int> &prices) {
if(prices.empty()) return ;
int maxProfit = ;
int min = INT_MAX;
int profit; for(vector<int>::iterator it = prices.begin(); it < prices.end(); it ++)
{
if ((*it)<min)
{
min = *it;
}
else
{
profit = *it - min;
if(profit > maxProfit)
{
maxProfit = profit;
}
}
}
return maxProfit;
}
};
121. Best Time to Buy and Sell Stock (Array;DP)的更多相关文章
- 121. Best Time to Buy and Sell Stock (一) leetcode解题笔记
121. Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of ...
- 30. leetcode 121. Best Time to Buy and Sell Stock
121. Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of ...
- leetcode 121. Best Time to Buy and Sell Stock 、122.Best Time to Buy and Sell Stock II 、309. Best Time to Buy and Sell Stock with Cooldown
121. Best Time to Buy and Sell Stock 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...
- 121. Best Time to Buy and Sell Stock【easy】
121. Best Time to Buy and Sell Stock[easy] Say you have an array for which the ith element is the pr ...
- 121. Best Time to Buy and Sell Stock@python
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- [LeetCode] 121. Best Time to Buy and Sell Stock 买卖股票的最佳时间
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- 【刷题-LeetCode】121 Best Time to Buy and Sell Stock
Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a gi ...
- 121. Best Time to Buy and Sell Stock
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- (Array)121. Best Time to Buy and Sell Stock
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
随机推荐
- 杂项:flex (adobe flex)
ylbtech-杂项:Flex (Adobe Flex) Flex指Adobe Flex,基于其专有的Macromedia Flash平台,它是涵盖了支持RIA(Rich Internet Appli ...
- 廖雪峰Java1-1Java入门-java简介
Java特点: 一种面向对象的跨平台变成语言 以字节码方式运行在虚拟机上 自带功能齐全的类库 非常活跃的开源社区支持 Java优点: 简单.健壮.安全 跨平台,一次编写,到处运行 高度优化的虚拟机 J ...
- 1038 Recover the Smallest Number (30 分)
1038 Recover the Smallest Number (30 分) Given a collection of number segments, you are supposed to r ...
- 1077 Kuchiguse (20 分)
1077 Kuchiguse (20 分) The Japanese language is notorious for its sentence ending particles. Personal ...
- PLSQL导出表结构
1:进行plsql后选怎Tools--------->Exports User Ojbects------------->选中需要导出的table,squence,view,type,fu ...
- sklearn.externals import joblib模块保存和下载使用模型的用法实例
#加载模块 from sklearn import datasets from sklearn.externals import joblib from sklearn.linear_model im ...
- Java 运算符-=,+=混合计算详解
+=与-=运算符混合计算解析: int x = 3; x += x -= x -= x += x -= x; 详解:算数运算按运算符优先级运算,从右至左计算. 1. x=x-x; 实际为 3 - 3 ...
- 基于标准库实现string和wstring的转换
// convert string to wstring std::wstring to_wstring(const std::string& str, const std::locale&a ...
- 管理oracle 11g RAC 常用命令
1).检查集群状态: [grid@rac02 ~]$ crsctl check cluster CRS-4537: Cluster Ready Services is online CRS-4529: ...
- 深度学习中的Normalization模型
Batch Normalization(简称 BN)自从提出之后,因为效果特别好,很快被作为深度学习的标准工具应用在了各种场合.BN 大法虽然好,但是也存在一些局限和问题,诸如当 BatchSize ...