Leetcode 详解(股票交易日)(动态规划DP)

问题描述:

在股市的交易日中,假设最多可进行两次买卖(即买和卖的次数均小于等于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;

    }
};

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

时间: 2024-10-10 14:21:25

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

Leetcode 详解(ReverseWords)

Leetcode里面关于字符串的一些问题,描述如下: Given an input string, reverse the string word by word. For example,Given s = "the sky is blue",return "blue is sky the". Update (2015-02-12):For C programmers: Try to solve it inO(1) space. click to show cla

The Skyline Problem leetcode 详解

class Solution { public: vector<pair<int, int>> getSkyline(vector<vector<int>>& buildings) { vector<pair<int, int> > h, res; multiset<int> m; int pre = 0, cur = 0; for (auto &a : buildings) { h.push_back({

Leetcode 详解(Valid Number)

Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => true 解决方案: public class Solution { public boolean isNumber(String s) { in

Leetcode 详解(valid plindrome)

Question: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, "A man, a plan, a canal: Panama" is a palindrome. "race a car" is not a palindrome.Example Questions C

Leetcode 详解(Substing without repeats character)

Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b", with the length of 1.

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

dp动态规划分类详解 转自:http://blog.csdn.NET/cc_again/article/details/25866971 动态规划一直是ACM竞赛中的重点,同时又是难点,因为该算法时间效率高,代码量少,多元性强,主要考察思维能力.建模抽象能力.灵活度. ****************************************************************************************** 动态规划(英语:Dynamic programm

HDU 1024 Max Sum Plus Plus【动态规划求最大M子段和详解 】

Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 29942    Accepted Submission(s): 10516 Problem Description Now I think you have got an AC in Ignatius.L's "Max Sum" problem

HUD5282 Senior&#39;s String 详解(使用DP解决组合数学)

题意:假设两个字符串的最长公共子序列长度为L,求第一个字符串中有多少个长度为L的子序列是第二个字符串的子序列.显然找出一个字符串的所有长度为L的子序列是组合数学问题,如果枚举所有子串的时间复杂度是n! 级的.这里就需要用动态规划来解决.首先用dp[i][j]和num[i][j]分别记录x的前I个字母和y的前j 个字母的最长公共子序列的长度和个数.先求出dp, 然后求num:.求num[i][j]分为两种情况,子序列不选x[i]和选x[i]: 1. 不选x[i]: 如果dp[i][j] == dp

【leetcode】Edit Distance 详解

下图为TI C6xx DSP Nyquist总线拓扑图,总线连接了master与slave,提供了高速的数据传输.有很多种速率不同的总线,如图中的红色方框,最高速总线为CPU/2 TeraNet SCR(即VBUSM SCR),带宽为256bit,其他低速总线为CPU/3,CPU/6,带宽参考图中所示.总线之间用Bridge(桥)连接,作用包括转换总线的速率,使之与所流向总线的速率相同等. 在具体应用中,各种速率的总线完全可以满足复杂的数据传输,而数据传输的瓶颈往往在于连接总线之间的Bridge