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竞赛中的重点,同时又是难点,因为该算法时间 ...
随机推荐
- sql中文日期格式转换(xxxx年x月x日)
) set @dd='2014年10月1日' select replace(replace( replace(@dd,'日',''),'月','-'),'年','-') 别人的方法 )='2012年1 ...
- 20161127-adt bundle
1.adt.exe 路径:E:\software\adt-bundle-windows-x86-20131030\sdk\platform-tools\adt.exe 配置环境变量 命令: adb d ...
- synergy 两台Windows电脑配置过程
Synergy 介绍 软件作用 Synergy 两台独立电脑,共享一套鼠标和键盘的工具, 软件原理(我自己想的) 保证两台电脑在一个局域网内,可以相互Ping通的电脑(这样才能直接通过TCP连接) 将 ...
- Android Studio 基本使用
一 . 目录结构: 目录结构本身代表了一个workspace空间 Android stuido是单工程的开发模式 Android stuido里面project表示工作空间,相当于eclipse里面的 ...
- 我为开源做贡献,网页正文提取——Html2Article
为什么要做正文提取 一般做舆情分析,都会涉及到网页正文内容提取.对于分析而言,有价值的信息是正文部分,大多数情况下,为了便于分析,需要将网页中和正文不相干的部分给剔除.可以说正文提取的好坏,直接影响了 ...
- centos7 docker redis
docker run --name=redistmp -ti centos /bin/bash yum -y install gcc tcl make cd /home wget http://dow ...
- xcode相关配置
Xcode将全部供应配置文件(包括用户手动下载安装的和Xcode自动创建的Team Provisioning Profile)放在目录~/Library/MobileDevice/Provisioni ...
- Servlet开发详解
一.Servlet简介 Servlet是sun公司提供的一门用于动态web资源的技术 sun公司在其API中提供了一个Servlet接口,用户若想开发一个动态web资源,需要完成两个步骤: 编写一个J ...
- DuiLib 源码分析之解析xml类CMarkup & CMarkupNode 头文件
xml使用的还是比较多的,duilib界面也是通过xml配置实现的 duilib提供了CMarkkup和CMarkupNode类解析xml,使用起来也是比较方便的,比较好奇它是怎么实现的,如果自己来写 ...
- web前端网站收藏
参考 w3school:html,css,js等各种参考 W3schools:较之w3school界面更华丽 webplatform:学习最新的web技术 MDN:mozilla developer ...