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

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

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. eclipse的ssh框架详解

    1.创建项目 2.导包 1.1:导入Struts2可能用到的包: 先从网站下载 再这里找出,打开它把WEB-INF/lib/下的所有包导入项目   1.2:导入spring可能用到的包: 先从网站下载 ...

  2. Selenium2+python自动化30-引入unittest框架

    from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.com ...

  3. git常用命令1

    git clone   #克隆远程仓库的项目到本地 git config --global user.name ""    #查看或设置贡献者的名字 git config --gl ...

  4. Linux2

    linux开源软件 :apache软件 nginx支持更高的并发访问 MySQL PHP samba mongoDB python 应用领域: 一:服务器  二:嵌入式

  5. PHP通过反射方法调用执行类中的私有方法

    PHP 5 具有完整的反射 API,添加了对类.接口.函数.方法和扩展进行反向工程的能力. 下面我们演示一下如何通过反射,来调用执行一个类中的私有方法: <?php //MyClass这个类中包 ...

  6. java中throw和throws的区别

    throw和throws的区别: throws 用在方法声明后面,跟的是异常类名 可以跟多个异常类名,用逗号隔开 表示抛出异常,由该方法的调用者来处理 throws表示出现异常的一种可能性,并不一定会 ...

  7. 校验两次密码一致的js代码

    function checkpasswd(){ var passwd = document.getElementByIdx_x_x_xx_x('passwd').value; var repasswd ...

  8. Github心得体会

    Github是一个代码托管的网站,以前端的代码为主,还有很多互动.​ 在我的理解看来,github并不仅仅是一个代码库,你可以自由注册,推送自己一些感兴趣编写的源代码.它不是单纯的保存代码,更多的是让 ...

  9. 推送XML

    推送的连接地址如:www.baidu.com /// <summary> /// 提交数据 /// </summary> /// <param name="ms ...

  10. linux里添加locate命令

    在linux里使用和find一样的功能 例如 find -name xx 可以yum install mlocate 然后 updatedb 再使用locate xx  来查找xx文件