【leetcode刷题笔记】Best Time to Buy and Sell Stock III
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete at most two transactions.
Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
题解:DP
从前往后扫描一遍数组,用LeftToRight[i]记录(0,i)得到的最大利润;
从后往前扫描一遍数组,用RightToLeft[i]记录(i,n-1)得到的最大利润;
最终的最大利润是max(LeftToRight[i]+RightToLeft[i])。
举个例子,如果prices = {1,2,3,2,5,7},对应的
LeftToRight = {0,1,2,2,4,6}
RightToLeft = {6,5,5,5,2,0}
最终的最大利润就是2+5=7。表示在0~2(或0~3)这个区间取得利润2,并且在2~5(或者3~5)这个区间取得利润5,最终得到利润7.
代码如下:
- public class Solution {
- public int maxProfit(int[] prices) {
- if(prices == null || prices.length == 0)
- return 0;
- int[] LeftToRight = new int[prices.length];
- int[] RightToLeft = new int[prices.length];
- int minimal = prices[0];
- LeftToRight[0] = 0;
- for(int i = 1;i < prices.length;i++){
- minimal = Math.min(minimal, prices[i]);
- LeftToRight[i] = Math.max(LeftToRight[i-1], prices[i]-minimal);
- }
- int profit = 0;
- //From Right to left
- RightToLeft[prices.length-1] = 0;
- int maximal = prices[prices.length-1];
- for(int i = prices.length-2;i >= 0;i--){
- maximal = Math.max(prices[i], maximal);
- RightToLeft[i]= Math.max(RightToLeft[i+1], maximal-prices[i]);
- profit = Math.max(profit, LeftToRight[i] + RightToLeft[i] );
- }
- return Math.max(profit, LeftToRight[0]+RightToLeft[0]);
- }
- }
【leetcode刷题笔记】Best Time to Buy and Sell Stock III的更多相关文章
- LeetCode(123) Best Time to Buy and Sell Stock III
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
- 【刷题-LeetCode】123 Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the price of ...
- LeetCode 笔记23 Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the price of ...
- LeetCode之“动态规划”:Best Time to Buy and Sell Stock I && II && III && IV
Best Time to Buy and Sell Stock I 题目链接 题目要求: Say you have an array for which the ith element is the ...
- 【leetcode】123. Best Time to Buy and Sell Stock III
@requires_authorization @author johnsondu @create_time 2015.7.22 19:04 @url [Best Time to Buy and Se ...
- LeetCode: Best Time to Buy and Sell Stock III 解题报告
Best Time to Buy and Sell Stock IIIQuestion SolutionSay you have an array for which the ith element ...
- LeetCode(122) Best Time to Buy and Sell Stock II
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
- Leetcode之动态规划(DP)专题-123. 买卖股票的最佳时机 III(Best Time to Buy and Sell Stock III)
Leetcode之动态规划(DP)专题-123. 买卖股票的最佳时机 III(Best Time to Buy and Sell Stock III) 股票问题: 121. 买卖股票的最佳时机 122 ...
- 【leetcode】Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the price of ...
- [leetcode]123. Best Time to Buy and Sell Stock III 最佳炒股时机之三
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
随机推荐
- parse arguments in bash
There are lots of ways to parse arguments in sh. Getopt is good. Here's a simple script that parses ...
- python mysql orm
Python中操作mysql的pymysql模块详解:https://www.cnblogs.com/wt11/p/6141225.html Python 12 - Mysql & ORM:h ...
- 图解堆算法、链表、栈与队列(Mark)
原文地址: 图解堆算法.链表.栈与队列(多图预警) 堆(heap),是一类特殊的数据结构的统称.它通常被看作一棵树的数组对象.在队列中,调度程序反复提取队列中的第一个作业并运行,因为实际情况中某些时间 ...
- git reset和git revert
1 git reset commit-id 直接回到某次提交,该次commit-id之后的提交都会被删除. --hard,将index和本地都恢复到指定的commit版本. 2 git revert ...
- Meeting-in-the-Middle (LA 2965)
Meeting-in-the-Middle,又称“中途相遇法”.准确地说,它只是一种策略. 顺便说一下,这个算法真的很冷门! 结合这道题来讨论一下吧:LA 2965.ε(┬┬﹏┬┬)3 因为博主的英文 ...
- cocos2d-x3.6 生成带类图的离线文档
我的博客:http://blog.csdn.net/dawn_moon cocos2d-x的官网有点慢,并且最新3.6的在线API文档居然没有了类图,不知道什么原因,之前2.2.6都是有的. 只是能够 ...
- Django之表单验证
对于前端的表单进行验证的方法,从最简单的js到基于XML传输的Ajax,再到cookie的免认证,现在Django为我们提供了自带的表单验证方法. views.py: from django impo ...
- LeetCode:区域和检索【303】
LeetCode:区域和检索[303] 题目描述 给定一个整数数组 nums,求出数组从索引 i 到 j (i ≤ j) 范围内元素的总和,包含 i, j 两点. 示例: 给定 nums = [ ...
- iOS 代码延迟执行
1. [NSTread sleepForTimeInterval:0.8f] 这个方法 实际效果 好比打断点 等你再恢复断点执行 2. [self performSelector:@selector ...
- spring-boot4
1.1.1. Starter pom 除了官方也有其他第三方提供的starter Websocket是服务端推数据到客户端.长连接. 1.1.1.Xml 配置文件 有些时候必须使用xml配置. 1.1 ...