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

时间:2021-11-19 07:24:27

问题描述:

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

    QQ:231469242 欢迎喜欢nltk朋友交流   http://baike.baidu.com/link?url=YFVbJFMkZO9A5CAvtCoKbI609HxXXSFd8flFG_Lg ...

  2. 类或方法名后加&lt&semi;&gt&semi;

    声明类型为泛型. 说明传递的参数为泛型,而不仅仅是一种类型. public void aa<T>(T a) { Console.WriteLine(a); } 调用时可写: this.aa ...

  3. &lbrack;转&rsqb;理解WSRF之一 使用WS-ResourceProperties &lpar;整理自IBM网站&rpar;

    理解 WSRF第1部分-使用 WS-ResourceProperties   本 教程是一个由 4 部分组成的系列文章中的第 1 部分,该系列介绍 WSRF(Web Services Resource ...

  4. ogre sample分析(一)

    ogre自带了一些例子,逐个过一遍并自己动手做一些调整 1 Sample_BezierPatch:这个例子直接用数值来构造顶点缓存并创建entity,这种方法一般只能创建简单对象,本人以为复杂对象顶点 ...

  5. html中滚动栏的样式

    DIV滚动栏设置 (CSS)2008/09/26 03:07div 中滚动栏的控制2008年01月06日 星期日 01:181)隐藏滚动栏<body style="overflow-x ...

  6. 基于Qt语音识别功能

    基于在最近的研究Qt语音识别平台下.在此记录12 首先,语音识别做三件事 1.记录用户的语音文件到本地 2.将用户语音编码 使用flac或者speex进行编码 3.使用第三方语音识别API或者SDK进 ...

  7. java基础之类与对象1

    从学习java到现在估计都有一年了,然而在这一年里基本处于三天打鱼五天晒网,感觉自己再不做点改变就是个废人了..T - T. 趁着重新复习java的时间,也顺便用博客来记录学习的过程.好了,废话不说了 ...

  8. 更符合面向对象的数据库操作方式-OrmLite

    1. jar包下载 下载地址:http://ormlite.com/releases/,一般用core和android包即可. 如果使用的是android studio,也可以直接通过module s ...

  9. Android Hybrid App自动化测试实战讲解(基于python)

    1.Hybrid App自动化测试概要 什么是Hybrid App? Hybrid App(混合模式移动应用)是指介于web-app.native-app这两者之间的app,兼具“Native App ...

  10. java用流的方式加载图片

    前端: <img ng-show="photo" class="vertical_b" id="photoImg" src=&quot ...