由于题意太长,请自己翻译,很容易懂的。

做法:从前向后遍历数组,记录当前出现过的最低价格,作为买入价格,并计算以当天价格出售的收益,作为可能的最大收益,整个遍历过程中,出现过的最大收益就是所求。动态规划的思路下次有空写个专题

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

Leetcode 121 Best Time to Buy and Sell Stock 动态规划的更多相关文章

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

  2. 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 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...

  3. [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 ...

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

  5. Java for 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 ...

  6. leetcode 121. Best Time to Buy and Sell Stock ----- java

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  7. Python [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 ...

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

  9. Leetcode 121. Best Time to Buy and Sell Stock 最佳股票售卖时(动态规划,数组,模拟)

    题目描述 已知一个数组,第i个元素表示第i天股票的价格,你只能进行一次交易(买卖各一次),设计算法找出最大收益 测试样例 Input: [7, 1, 5, 3, 6, 4] Output: 5 最大收 ...

随机推荐

  1. 友盟分享SDK集成步骤

    1.官方注册appID. 2.menifest添加和声明umeng相关的activity以及appKey. 3. // 首先声明一个controller变量,由友盟服务工厂类直接取得友盟社交服务. m ...

  2. 技术英文单词贴--E

    E element 元素,成分,要素 expire 到期,终止,期满

  3. css颜色表示

    CSS1&CSS2的颜色方式 Color Name方式 用颜色关键字表示对应的颜色. 例如:red(红色).blue(蓝色).pink(粉色) 优点:方便快捷而且特定颜色比较准确 缺点:英文记 ...

  4. 『BASH』——文件权限批量恢复脚本——「Permission Revovery」

    一.恢复指定程序包所有文件的权限: #!/bin/bash #Assume that you have mounted a correct orignal-system on /mnt read -p ...

  5. mac下java 开发环境搭建

    mac配置java开发环境: jdk1.7 +sdk1.7+maven +tomcat   1.先安装jdk ,才能安装sdk . 2 mac中jdk1.7的默认位置:/Library/Java/Ja ...

  6. servlet 生命周期

    Ò编写一个HelloWordServlet类

  7. 有关pascal的填充语句小技巧

    背景 今天打代码,用了一次fillchar(a,sizeof(a),1); 结果a数组(of longint)所赋的值却不是1 探索 ···pascal program fillchartest; v ...

  8. 理解浏览器的重绘与回流(repaint&&reflow)

    今天在做练习的时候,遇到了重绘与回流这个词,表示连个毛都没有听过.遂查之,首先将网上的(http://blog.sina.com.cn/s/blog_8dace7290102wezv.html)关于这 ...

  9. 存储构造题(Print Check)

    连接:Print Check 题意:n行m列的矩阵,有k次涂色,每一涂一行或者一列,求最后的涂色结果. 从数据的大小看,暴力肯定要TLE: 问题是如何存储数据. 首先:我们只要最后的涂色结果. 其次: ...

  10. hdu 4107

    Gangster Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...