题目:

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. 使用FusionCharts创建可更新数据的JavaScript图表

    先创建一个简单的图表,然后改变它的数据(请参见下面的代码).图表最初据显示8月份的销售数据,当用户点击按钮时改为显示9月份的销售数据.每个月都有单独的XML文件,代码如下: <html> ...

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

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

  3. spring boot 下 spring security 自定义登录配置与form-login属性详解

    package zhet.sprintBoot; import org.springframework.beans.factory.annotation.Autowired;import org.sp ...

  4. maven+jenkins发布环境

    安装java省略,下面是环境变量 export JAVA_HOME=/usr/java/jdk1.8.0_65/ export PATH=$JAVA_HOME/bin:$PATH export CLA ...

  5. CSS第二节

    div做页面布局的建议 把整个网页从上到下分成若干块(一般分三块:头,中间,尾部),每一块都按下面的思路 先写第一层,可以设置背景色,或者高度和垂直居中(line-height保证内容不超出高度),不 ...

  6. 在一个css文件中引入其他css文件

    @import "./main.css";@import "./color-dark.css";@import "./reset.css";

  7. ZOJ 2112 Dynamic Rankings(二分,树套树)

    动态区间询问kth,单点修改. 区间用线段树分解,线段树上每条线段存一颗平衡树. 不能直接得到kth,但是利用val和比val小的个数之间的单调性,二分值.log^3N. 修改则是一次logN*log ...

  8. Android(java)学习笔记86:Android提供打开各种文件的API接口:setDataAndType

    1. Android 打开各种文件(setDataAndType) private void openFile(File file){ Intent intent = new Intent(); in ...

  9. 【洛谷4252】[NOI2006] 聪明的导游(提答题)

    点此看题面 大致题意: 给你一张\(n\)个点.\(m\)条边的无向图,让你找出图上的一条不经过重复节点的最长路(提答+\(spj\)). 随机化乱搞 针对这种提答题,我们就要用随机化乱搞(Cptra ...

  10. MyISAM 和 InnoDB 的区别与优化

    MyISAM 和 InnoDB 的基本区别 1.InnoDB不支持FULLTEXT类型的索引. 2.InnoDB 中不保存表的具体行数,也就是说,执行select count(*) from tabl ...