题目:

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.size()==) return ;
int min_price = prices[], max_profit = ;
for ( size_t i = ; i < prices.size(); ++i )
{
min_price = std::min(min_price, prices[i]);
max_profit = std::max(max_profit, prices[i]-min_price);
}
return max_profit;
}
};

tips:

Greedy

核心在于维护两个变量:

min_price: 算上当前元素的最小值

max_profit:算上当前元素的最大利润

走完所有元素,可以获得max_profit

================================================

第二次过这道题,犹豫了一下才想起来思路。

class Solution {
public:
int maxProfit(vector<int>& prices) {
int globalProfit = ;
if ( prices.empty() ) return globalProfit;
int minPrice = prices[];
for ( int i=; i<prices.size(); ++i )
{
minPrice = min(minPrice, prices[i]);
globalProfit = max(globalProfit, prices[i]-minPrice);
}
return globalProfit;
}
};

【Best Time to Buy and Sell Stock】cpp的更多相关文章

  1. leetcode 【 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 w ...

  2. 【LeetCode-面试算法经典-Java实现】【121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)】

    [121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Say you have ...

  3. leetcode 【 Best Time to Buy and Sell Stock II 】python 实现

    题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...

  4. 【Best Time to Buy and Sell Stock III 】cpp

    题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...

  5. 【Best Time to Buy and Sell Stock II】cpp

    题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...

  6. leetcode 【 Best Time to Buy and Sell Stock III 】python 实现

    题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...

  7. 【一天一道LeetCode】#122. Best Time to Buy and Sell Stock II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say you ...

  8. 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 ...

  9. 【leetcode】Best Time to Buy and Sell Stock III

    Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the price of ...

随机推荐

  1. Invoke 和 BeginInvoke 的区别(转发)

    在Invoke或者BeginInvoke的使用中无一例外地使用了委托Delegate. 一.为什么Control类提供了Invoke和BeginInvoke机制? 关于这个问题的最主要的原因已经是do ...

  2. 工作中碰到的css问题解决方法

    好久都没来这写东西了,都长草了.刚解决的两个小问题,先记下来 textarea横向没有滚动条加上 wrap="off"这个属性 英文单词不断行加上这个 word-break:bre ...

  3. Codeforces Round #323 (Div. 2) C GCD Table 582A (贪心)

    对角线上的元素就是a[i],而且在所在行和列中最大, 首先可以确定的是最大的元素一定是a[i]之一,这让人想到到了排序. 经过排序后,每次选最大的数字,如果不是之前更大数字的gcd,那么只能是a[i] ...

  4. 【洛谷】CYJian的水题大赛 解题报告

    点此进入比赛 \(T1\):八百标兵奔北坡 这应该是一道较水的送分题吧. 理论上来说,正解应该是DP.但是,.前缀和优化暴力就能过. 放上我比赛时打的暴力代码吧(\(hl666\)大佬说这种做法的均摊 ...

  5. SEO人士一定要了解的搜索引擎惩罚原则

       SEO人士一定要了解的搜索引擎惩罚原则 SEO 的人一般都知道SEO分为白帽,黑帽,甚至还有灰帽.简单说,如果你熟读过谷歌网站质量指南,就可以了解,符合搜索引擎质量规范并且符合用户体验的SEO ...

  6. 2018.5.19 Oracle数据操作和管理表的综合练习

    --作业一.使用自己的用户登录,完成如下操作,并且创建5条测试数据 -- 创建学生表(stu),字段如下: -- 学号(stuID) -- 姓名(stuName) -- 性别(stuSex) -- 入 ...

  7. 2018. 2.4 Java中集合嵌套集合的练习

    创建学生类有姓名学校和年龄 覆盖toString() 1.创建三个学生对象,放到集合ArrayList 2.输出第2名学生的信息 3.删除第1个学生对象 4.在第2个位置插入1个新学生信息 5.判断刘 ...

  8. WARNING: The TCP backlog setting of 511.解决

    redis启动警告问题:WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/so ...

  9. Oracle Undo 和 Redo

    1. REDO(重做信息) Redo log file(重做日志文件),是数据库的事务日志. Oracle维护着两类重做日志文件:在线(online)重做日志文件和归档(archived)重做日志文件 ...

  10. Linux更改文件权限(一)

    更改文件权限(一)============================== (参考于千锋教育教学笔记) 设置权限 1.更改文件的属主.属组chown (change owner)[root@ami ...