Leetcode_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 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:
//I
int maxProfit(vector<int> &prices) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int len = prices.size();
if(len < ) return ;
int min = prices[];
int res = ;
for(int i = ; i< prices.size(); ++i)
{
if(prices[i] < min){
min = prices[i] ;
continue;
}
res = res > prices[i] - min ? res : prices[i] - min ;
}
return res;
}
};
Leetcode_Best Time to Buy and Sell Stock的更多相关文章
- LeetCode_Best Time to Buy and Sell Stock III
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 【leetcode】121-Best Time to Buy and Sell Stock
problem 121. Best Time to Buy and Sell Stock code class Solution { public: int maxProfit(vector<i ...
- [LeetCode] Best Time to Buy and Sell Stock with Cooldown 买股票的最佳时间含冷冻期
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] Best Time to Buy and Sell Stock III 买股票的最佳时间之三
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] Best Time to Buy and Sell Stock II 买股票的最佳时间之二
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] 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 ...
- [LintCode] Best Time to Buy and Sell Stock II 买股票的最佳时间之二
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LintCode] 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 ...
随机推荐
- alias, bg, bind, break, builtin, caller, cd, command,
bash, :, ., [, alias, bg, bind, break, builtin, caller, cd, command, compgen, complete, com ...
- M0、M1、M2、M3都是用来反映货币供应量的重要指标
m2-反映货币供应量的重要指标编辑词条m2广义货币是一个经济学概念,和狭义货币相对应,货币供给的一种形式或口径,以M2来表示,其计算方法是交易货币(M1,即社会流通货币总量加上活期存款)以及定期存款与 ...
- POJ-1028(字符串模拟)
Web Navigation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 31906 Accepted: 14242 ...
- 高级私人定制西服品牌:XUAN PRIVE 为定制而生_乐活_onlylady女人志
高级私人定制西服品牌:XUAN PRIVE 为定制而生_乐活_onlylady女人志 高级私人定制西服品牌:XUAN PRIVE 为定制而生
- JBossESB教程(二)——将JBossESB部署到JBossAS中
前言 上篇讲了JBossESB的环境搭建,但是细心的同学会发现,我们在添加JBoss AS的时候,实际上添加的是jbossesb-server,而这个里面是没有EJB的支持的.如果我们想要使开发环境能 ...
- Shell中变量的使用
1.变量的声明 name="blacksonny" 注意://变量定义时不加$,变量与等号之间不能有空格 变量命名规则: 首个字符必须为字母(a-z,A-Z). 中间不能有空格,可 ...
- 关于数据表命名为mysql保留的时候的操作
今天操作数据表的时候,发现order数据表无法进行操作,必须加上反单引号才能进行操作,查了一下原因: 反引号是用来区别mysql关键字的,比如,如果你有一个表名叫select,你就必须写成`selec ...
- phpstorm + xdebug 配置
PHPSTORM版本 : 8.0.1 PHP版本 : 5.6.2 把php-xdebug.dll复制到xamapp/php/ext目录下,打开php.ini配置如下参数 [xdebug] zend_e ...
- 苹果的HomeKit协议
苹果的HomeKit协议非常底层,其作用仅限于让iOS平台和家居设备能够相互“握手”,但“认识”之后,想要继续控制灯.空调等设备,仍然需要家电厂商在HomeKit的基础上进行二次开发.
- Python字符串格式符号含义
====== #字符串格式化符号含义 #%C 格式化字符串及其ASCLL码 >>> '%c' %97 'a' >>> '%c' % 97 'a' >>& ...