【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.

代码如下:

 1 public class Solution {
 2     public int maxProfit(int[] prices) {
 3         if(prices == null || prices.length == 0)
 4             return 0;
 5
 6         int[] LeftToRight = new int[prices.length];
 7         int[] RightToLeft = new int[prices.length];
 8
 9         int minimal = prices[0];
10         LeftToRight[0] = 0;
11         for(int i = 1;i < prices.length;i++){
12             minimal = Math.min(minimal, prices[i]);
13             LeftToRight[i] = Math.max(LeftToRight[i-1], prices[i]-minimal);
14         }
15
16         int profit = 0;
17         //From Right to left
18         RightToLeft[prices.length-1] = 0;
19         int maximal = prices[prices.length-1];
20         for(int i = prices.length-2;i >= 0;i--){
21             maximal = Math.max(prices[i], maximal);
22             RightToLeft[i]= Math.max(RightToLeft[i+1], maximal-prices[i]);
23             profit = Math.max(profit, LeftToRight[i] + RightToLeft[i] );
24         }
25
26         return Math.max(profit, LeftToRight[0]+RightToLeft[0]);
27     }
28 }

【leetcode刷题笔记】Best Time to Buy and Sell Stock III

时间: 2024-10-20 18:41:21

【leetcode刷题笔记】Best Time to Buy and Sell Stock III的相关文章

LeetCode练题——122. Best Time to Buy and Sell Stock II

1.题目 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你可以尽可能地完成更多的交易(多次买卖一支股票). 注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票). 示例 1: 输入: [7,1,5,3,6,4]输出: 7解释: 在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 .  随后,在第 4 天(股票价格 = 3)的时候买入,在第 5

【leetcode刷题笔记】Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3 T

【leetcode刷题笔记】Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run in

【leetcode刷题笔记】Remove Duplicates from Sorted Array II

Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3]. 题解: 设置两个变量:右边kepler和前向游标forward.如果当前kepeler所指的元素和

【leetcode刷题笔记】Restore IP Addresses

Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example:Given "25525511135", return ["255.255.11.135", "255.255.111.35"]. (Order does not matter) 题解:深度优先搜索.用resul

【leetcode刷题笔记】Path Sum

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 return true, as t

【leetcode刷题笔记】Insertion Sort List

Sort a linked list using insertion sort. 题解:实现链表的插入排序. 要注意的地方就是,处理链表插入的时候尽量往当前游标的后面插入,而不要往前面插入,后者非常麻烦.所以每次利用kepeler.next.val和head.val比较大小,而不是kepeler.val和head.val比较大小,因为如果用后者,要把head指向的节点插入到kepeler指向的节点的前面,如果kepeler指向的节点是头结点,就更麻烦了. 代码如下: 1 /** 2 * Defi

【leetcode刷题笔记】Max Points on a Line

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 题解: 思路比较简单,每条直线都可以表示为y=kx+b,所以对于任意三点,如果它们共线,那么它们中任意两点的斜率都相等. 所以就遍历points数组,对其中的每一个元素计算它和位于它后面的数组元素的斜率并保存在一个hashmap中. 这个hashmap的键就是两点构成直线的斜率,值就是和当前元素po

【leetcode刷题笔记】Spiral Matrix II

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 题解:以前做过的Spiral Matrix是给一个矩阵螺旋式的输出,这道题是给一个n,螺旋式的