[LeetCode]题解(python):123-Best Time to Buy and Sell Stock III
题目来源:
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/
题意分析:
和上题类似,array[i]代表第i天物品价格,如果只能交易2次。问最大利润。
题目思路:
这是一个动态规划问题。不难想到把整个数组拆成两部分。那么用两个数组First,second分别array[:i]和array[i:]的最大利润。那么答案就等于max(First[i] + second[i + 1])。
代码(python):
class Solution(object):
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
size = len(prices)
if size == 0: return 0
first,second = [0 for i in range(size)],[0 for i in range(size+1)]
minp = prices[0]
for i in range(size):
if prices[i] <= minp:
minp = prices[i];first[i] = first[i - 1]
first[i] = max(prices[i] - minp,first[i-1])
maxp = prices[size-1]
j = size - 1
while j >= 0:
if prices[j] >= maxp:
maxp = prices[j]
second[j] = max(second[j+1],maxp - prices[j])
j -= 1
ans = 0
for i in range(size):
ans = max(ans,first[i]+second[i+1])
return ans
[LeetCode]题解(python):123-Best Time to Buy and Sell Stock III的更多相关文章
- 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 ...
- 【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]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 ...
- 【刷题-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 ...
- 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 买卖股票的最佳时间 III
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- LeetCode OJ 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 ...
- 123. Best Time to Buy and Sell Stock III ——LeetCode
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 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 a ...
- 123. Best Time to Buy and Sell Stock III (Array; DP)
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
随机推荐
- UVa 10491 Cows and Cars (概率&广义三门问题 )
10491 - Cows and Cars Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onli ...
- css3 动画(animation)-简单入门
css3之动画(animation) css3中我们可以使用动画,由于取代以前的gif图片,flash动画,以及部分javascript代码(相信有很多同学都用过jquery中的animate方法来做 ...
- SSLSocket实现服务端和客户端双向认证的例子
首先创建服务器端私有密钥和公共密钥1, keytool -genkey -alias serverkey -keystore kserver.ks 密码: serverpass2, keytoo ...
- zoj 1025Wooden Sticks(贪心)
递增子序列的最小组数.可以直接贪心,扫一遍 #include<iostream> #include<cstring> #include<cstdio> #inclu ...
- js判断年龄是否在0-100之间
//判断年龄 $('#info_age').change(function(){ var _val = $(this).val(); -]+$/.test( _val ))||_val< || ...
- js 下拉框效果
<script type="text/javascript"> window.onload = function () { ]; ]; var aLi = oSub.g ...
- 防止自己的网站被别人frame引用造成钓鱼
自己负责的某一网站,最近被不法份子通过<frame>的方式引入,用户点击对方的域名后,看到的内容跟自己网站一模一样.但是右击查看源码就会发现其中的原理: <!DOCTYPE HTML ...
- Windows窗口消息大全(转)
Windows窗口消息大全,全不全自己看 ////////////////////////////////////////////////////////////////////////// #inc ...
- window.external.JavaScriptCallCpp
方案2: 1.编写html <html> <head> </head> <body> <script language="javascr ...
- 手机端 UI一些插件
手机弹出框 http://yun.baidu.com/share/link?shareid=3523128425&uk=2685891615