问题描述:

在股市的交易日中,假设最多可进行两次买卖(即买和卖的次数均小于等于2),规则是必须一笔成交后进行另一笔(即买-卖-买-卖的顺序进行)。给出一天中的股票变化序列,请写一个程序计算一天可以获得的最大收益。请采用实践复杂度低的方法实现。

给定价格序列prices及它的长度n,请返回最大收益。保证长度小于等于500。

class Solution {
public:
int maxProfit(vector<int>& prices) {
//It's wrong if you choose the minimum price for buy2 , but you can maximize the left money.
//
int buy1 = INT_MIN, sale1 = 0, buy2 = INT_MIN, sale2 = 0;
for(int i=0; i<prices.size(); i++){ //the more money left, the happier you will be
buy1 = max(buy1, -prices[i]); //left money after buy1
sale1 = max(sale1, prices[i] + buy1); //left money after sale1
buy2 = max(buy2, sale1 - prices[i]); //left money after buy2
sale2 = max(sale2, prices[i] + buy2); //left money after sale2
}
return sale2; }
};

此方法的核心是对于每个数据,我都以相同的处理方法(动态规划),在此,把问题解决转化为手头的剩余的钱的变化, 每一步的处理都是为了手上留下的钱更多。

Leetcode 详解(股票交易日)(动态规划DP)的更多相关文章

  1. 由Leetcode详解算法 之 动态规划(DP)

    因为最近一段时间接触了一些Leetcode上的题目,发现许多题目的解题思路相似,从中其实可以了解某类算法的一些应用场景. 这个随笔系列就是我尝试的分析总结,希望也能给大家一些启发. 动态规划的基本概念 ...

  2. Leetcode详解Maximum Sum Subarray

    Question: Find the contiguous subarray within an array (containing at least one number) that has the ...

  3. Leetcode 详解(ReverseWords)

    Leetcode里面关于字符串的一些问题,描述如下: Given an input string, reverse the string word by word. For example,Given ...

  4. Leetcode 详解(Substing without repeats character)

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  5. Leetcode 详解(Valid Number)

    Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...

  6. Leetcode 详解(Implement strstr)

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  7. Leetcode 详解(valid plindrome)

    Question: Given a string, determine if it is a palindrome, considering only alphanumeric characters ...

  8. The Skyline Problem leetcode 详解

    class Solution { public: vector<pair<int, int>> getSkyline(vector<vector<int>&g ...

  9. (转)dp动态规划分类详解

    dp动态规划分类详解 转自:http://blog.csdn.NET/cc_again/article/details/25866971 动态规划一直是ACM竞赛中的重点,同时又是难点,因为该算法时间 ...

随机推荐

  1. Apache开启状态查看页面(原创贴-转载请注明出处)

    =================写在前面的话================== 场景描述:有时候我们需要查看apache的运行状态,只需要开启apache的status功能就可以实现,但是stat ...

  2. Mac环境下JDK安装方法

    1.版本 MAC OS 10.10.3 JDK:jdk1.8.0_40 jdk下载mac版本的:http://www.oracle.com/technetwork/java/javase/downlo ...

  3. js 、jsdoc生成33

    ============== js 点击事件后没方法名,调用有方法名 document.getElementById('lind').onclick=abc;//传统的id选择器 中没有# 哦 fun ...

  4. 分享自己的超轻量级高性能ORM数据访问框架Deft

    Deft 简介 Deft是一个超轻量级高性能O/R mapping数据访问框架,简单易用,几分钟即可上手. Deft包含如下但不限于此的特点: 1.按照Transact-SQL的语法语义风格来设计,只 ...

  5. hive的使用03

    1.hive中的四种排序 1.1 order by :对全局进行排序,只能有一个reduce select * from hive.employee order by id;

  6. python datetime模块

    该模块的时间有限时限:1 - 9999 dir(datetime)  from datetime import datetime, timedelta, timezone dt = datetime. ...

  7. OpenModelica仿真

    复杂产品通常涉及机械.控制.电子.液压.气动和软件等多学科领域,其设计过程需要进行仿真,以满足对成本.质量.性能等的要求.目前各个学科和领域都已经有了比较成熟的仿真软件,但大部分仿真软件仅适用于本学科 ...

  8. InfoCube信息立方体的优化

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  9. 未能解析目标框架“.NETFramework,Version=v4.0”的 mscorlib的解决方法

    本人菜鸟一个,在编码过程中遇到的问题记录下以备忘,高手别笑.最近在做一个项目,公司的VS版本是2010,家里的VS版本是2012.把公司的项目用2012打开后再用2010打开就出现 未能解析目标框架“ ...

  10. 《简明python教程》笔记二

    面向对象的编程: 类和对象是面向对象编程的两个主要方面.类创建一个新类型,而对象是这个类的实例.对象可以使用普通的属于对象的变量存储数据.属于一个对象或类的变量被称为域.对象也可以使用属于类的函数来具 ...