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;
}
};
此方法的核心是对于每个数据,我都以相同的处理方法(动态规划),在此,把问题解决转化为手头的剩余的钱的变化, 每一步的处理都是为了手上留下的钱更多。
Leetcode 详解(股票交易日)(动态规划DP)的更多相关文章
- 由Leetcode详解算法 之 动态规划(DP)
因为最近一段时间接触了一些Leetcode上的题目,发现许多题目的解题思路相似,从中其实可以了解某类算法的一些应用场景. 这个随笔系列就是我尝试的分析总结,希望也能给大家一些启发. 动态规划的基本概念 ...
- Leetcode详解Maximum Sum Subarray
Question: Find the contiguous subarray within an array (containing at least one number) that has the ...
- Leetcode 详解(ReverseWords)
Leetcode里面关于字符串的一些问题,描述如下: Given an input string, reverse the string word by word. For example,Given ...
- Leetcode 详解(Substing without repeats character)
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- Leetcode 详解(Valid Number)
Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...
- Leetcode 详解(Implement strstr)
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- Leetcode 详解(valid plindrome)
Question: Given a string, determine if it is a palindrome, considering only alphanumeric characters ...
- The Skyline Problem leetcode 详解
class Solution { public: vector<pair<int, int>> getSkyline(vector<vector<int>&g ...
- (转)dp动态规划分类详解
dp动态规划分类详解 转自:http://blog.csdn.NET/cc_again/article/details/25866971 动态规划一直是ACM竞赛中的重点,同时又是难点,因为该算法时间 ...
随机推荐
- mkforsela
-- #!/bin/bash #sela.gao # #History: # .根据每行查找出来的结果push进去手机 #result: # :没有编译生成文件 # :没有设置编译环境 echoMsg ...
- Thinkphp各大支付平台在线支付集成源码
用Thinkphp给客户开发网站的时候需要用到各大平台付款功能,下面就免费分享给大家,此类是个成熟类,网上down下来的,经过修改测试了(可以直接拿来使用,附带使用方法,有需要的朋友请拿走.),如果有 ...
- Spark HA实战
Spark HA需要安装zookeeper,推荐稳定版3.4.6. 1.下载zookeeper3.4.6,2.配置环境变量3.创建data logs4.vi conf/zoo.cfg5 data目录中 ...
- 解决android:background背景图片被拉伸问题
ImageView中XML属性src和background的区别: background会根据ImageView组件给定的长宽进行拉伸,而src就存放的是原图的大小,不会进行拉伸.src是图片内容(前 ...
- CocoaPods使用 主要带图。转载。
原文地址 快速更新,不需要查找更新库 命令:pod update --verbose --no-repo-update 虽然网上关于CocoaPods安装教程多不胜数,但是我在安装的过程中还是出现了 ...
- CodeUI Test:创建第一个CodeUI Test
CodeUI Test是微软自动化测试的一个比较好的项目,它的原理是获取到Windows窗口上的控件,然后针对控件的部分属性进行获取和对比,模拟对控件进行点击.双击.右键点击等事件.这样可以录制用户测 ...
- jquery总结06-动画事件04-自定义动画
.animate(params,[speed],[easing],[fn]) params,[speed],[easing],[fn]Options,Number/String,String,Func ...
- 零配置文件搭建SpringMVC实践纪录
本篇记录使用纯java代码搭建SpringMVC工程的实践,只是一个demo.再开始之前先热身下,给出SpringMVC调用流程图,讲解的是一个http request请求到达SpringMVC框架后 ...
- 如何编写高质量的Javascript代码
1.避免全局变量,因为全局变量容易发生名称上的冲突,可维护性不好. a,使用命名空间 b,使用闭包 c,在函数内部使用var声明 2.编写可维护的代码 a.可读性 b.连续性 c.预见性 d.看起来是 ...
- Linux 系统时间和硬件时间
linux 的系统时间有时跟硬件时间是不同步的 Linux时钟分为系统时钟(System Clock)和硬件(Real Time Clock,简称RTC)时钟.系统时钟是指当前Linux Kernel ...