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

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

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. vim--golang代码补全

    我想说,我折腾了很久编辑器,试了九种办法 最后我只成功了一种 但我依然想就我混乱的逻辑做下整理 一.一开始,我试图入手ipad编码软件,大概9美金吧,叫Textastic.我试图用它的近亲来试验Tex ...

  2. C++队列中应该注意的一些问题

    第一次在C++中写类,新手,见笑 #include<iostream.h>#include<iostream>template<typename T> class ...

  3. andorid SQLite数据库的增删改查 和事务操作

    .xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android ...

  4. Trie树:POJ2001

    这是一道最简单的trie树的题 有趣的是这道题的测试用例无法在VS上调试,只能在框框里不断提交测试了,哈哈 最基本的Trie树,插入和查找操作没什么好说的 注意节点添加一个count变量作为附加条件, ...

  5. canvas 画六边形

    <section class="m1-c"> <div class="m1-t clearfix"> <ul> <li ...

  6. Android NDK构建资料

    Cmake http://blog.csdn.net/u012527560/article/details/51752070  http://wenku.baidu.com/link?url=ENJF ...

  7. centos下 redis安装配置及简单测试

    1:安装redis(使用的的环境是centos6.7 redis-2.6.14) 将redis-2.6.14.tar.gz文件拷贝到/usr/local/src 目录下 解压文件  tar zxvf ...

  8. Okhttp https

    1. 绕过CA证书,不建议使用 private void ingoreCA() throws NoSuchAlgorithmException, KeyManagementException { SS ...

  9. 1201新课程TSQL语句

    1.创建数据库 create datebase 表名称 2.删除数据库 drop datebase 表名称 3.创建表 -create table test(表名称)( code(列名称) varch ...

  10. C++ 优先队列

    C++ 优先队列 #include <queue> priority_queue<Type, Container, Functional>:Type为数据的类型,Contain ...