Best Time to Buy and Sell Stock III 解答
Question
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).
Solution
This problem can be solved by "divide and conquer". We can use left[i] array to track maximum profit for transactions before i (including i), and right[i + 1] to track maximum profit for transcations after i.
Prices: 1 4 5 7 6 3 2 9
left = [0, 3, 4, 6, 6, 6, 6, 8]
right= [8, 7, 7, 7, 7, 7, 7, 0]
Time complexity O(n), space cost O(n).
public class Solution {
public int maxProfit(int[] prices) {
if (prices == null || prices.length < 2)
return 0;
int length = prices.length, min = prices[0], max = prices[length - 1], tmpProfit = 0;
int[] leftProfits = new int[length];
leftProfits[0] = 0;
int[] rightProfits = new int[length];
rightProfits[length - 1] = 0;
// Calculat left side profits
for (int i = 1; i < length; i++) {
if (prices[i] > min)
tmpProfit = Math.max(tmpProfit, prices[i] - min);
else
min = prices[i];
leftProfits[i] = tmpProfit;
}
// Calculate right side profits
tmpProfit = 0;
for (int j = length - 2; j >= 0; j--) {
if (prices[j] < max)
tmpProfit = Math.max(tmpProfit, max - prices[j]);
else
max = prices[j];
rightProfits[j] = tmpProfit;
}
// Sum up
int result = Integer.MIN_VALUE;
for (int i = 0; i < length - 1; i++)
result = Math.max(result, leftProfits[i] + rightProfits[i + 1]);
result = Math.max(result, leftProfits[length - 1]);
return result;
}
}
Best Time to Buy and Sell Stock III 解答的更多相关文章
- 27. Best Time to Buy and Sell Stock && Best Time to Buy and Sell Stock II && Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock (onlineJudge: https://oj.leetcode.com/problems/best-time-to-buy-and- ...
- 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 ...
- Best Time to Buy and Sell Stock | & || & III
Best Time to Buy and Sell Stock I Say you have an array for which the ith element is the price of a ...
- 【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 ...
- LeerCode 123 Best Time to Buy and Sell Stock III之O(n)解法
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 【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]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 ...
- LN : leetcode 123 Best Time to Buy and Sell Stock III
lc 123 Best Time to Buy and Sell Stock III 123 Best Time to Buy and Sell Stock III Say you have an a ...
随机推荐
- Wiggle Sort 解答
Question Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= num ...
- JavaScript - 基于原型的面向对象
JavaScript - 基于原型的面向对象 1. 引言 JavaScript 是一种基于原型的面向对象语言,而不是基于类的!!! 基于类的面向对象语言,比如 Java,是构建在两个不同实体的概念之上 ...
- [置顶] vi、akw和sed总结
- 单源最短路径—Bellman-Ford和Dijkstra算法
Bellman-Ford算法:通过对边进行松弛操作来渐近地降低从源结点s到每个结点v的最短路径的估计值v.d,直到该估计值与实际的最短路径权重相同时为止.该算法主要是基于下面的定理: 设G=(V,E) ...
- JUnit三分钟教程 ---- 快速起步
JUnit三分钟教程 ---- 快速起步 摘自http://lavasoft.blog.51cto.com/62575/65625/ JUnit是个好东西,做大点的项目离不开这东西,实际中用的时候也因 ...
- pyqt sender()学习
#!/usr/bin/python # -*- coding: utf-8 -*- # sender.py import sys from PyQt4 import QtGui, QtCore cla ...
- PC--CSS优化
优化你的CSS 所谓高效的CSS就是让浏览器在查找style匹配的元素的时候尽量进行少的查找,下面列出一些我们常见的写CSS犯一些低效错误: 1.不要在ID选择器前使用标签名一般写法:DIV#divB ...
- php创建带logo的二维码
<?php /** php使用二维码 **/ class MyQrcode{ const SIZE = 150; const LEVEL = "L"; const MARGI ...
- Android——编译odex保护
编译过android源代码的可能试验过改动编译类型.android的初始化编译配置可參考Android--编译系统初始化设置 一.TARGET_BUILD_VARIANT=user 当选择的编译类型为 ...
- 学习Android之SharedPreferences使用
效果图例如以下: 当我们想让自己的属性设置保存下来,这时就须要SharedPreferences. 上面这个小程序,音乐状态是保存下来的.使用的上一次退出的状态. 进入DDMS,data文件下的dat ...