Best Time to Buy and Sell Stock
Say you have an array for which the ith element is the price of a given stock on day i.
If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.
Note that you cannot sell a stock before you buy one. [#121]
Examples: Input: [7,1,5,3,6,4] Output: 5 Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5. Not 7-1 = 6, as selling price needs to be larger than buying price. Input: [7,6,4,3,1] Output: 0 Explanation: In this case, no transaction is done, i.e. max profit = 0.
题意:股票一次买卖的最大利润(必须先买后卖),相当于求列表中两元素差的最大值但要求索引值大者减去小者。
本题又能用“子序列推导式”来解决:
>>> sublst = lambda s:[d[-1]-d[0] for d in [s[i:j] for i in range(len(s)) for j in range(i+2,len(s)+1)]] >>> maxProfit = lambda s:max(sublst(s)) if any([i>0 for i in sublst(s)]) else 0 >>> >>> data1 = [7,1,5,3,6,4] >>> maxProfit(data1) 5 >>> >>> data2 = [7,6,4,3,1] >>> maxProfit(data2) 0 >>>
或者:
>>> sublst = lambda s:[d[-1]-d[0] for d in [s[i:j] for i in range(len(s)) for j in range(i+2,len(s)+1)] if d[-1]>d[0]] >>> maxProfit = lambda s:max(sublst(s)) if len(sublst(s))>0 else 0 >>> >>> data1 = [7,1,5,3,6,4] >>> maxProfit(data1) 5 >>> >>> data2 = [7,6,4,3,1] >>> maxProfit(data2) 0 >>>
解法二:动态规划 Dynamic Programming
>>> def maxProfit(prices): profit,min = 0,prices[0] for i in range(1,len(prices)): t = prices[i]-min if t > profit: profit = t if t < 0: min = prices[i] return profit >>> data1 = [7,1,5,3,6,4] >>> maxProfit(data1) 5 >>> data2 = [7,6,4,3,1] >>> maxProfit(data2) 0 >>>
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 algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times). [#122]
Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock
before you buy again).
Examples: Input: [7,1,5,3,6,4] Output: 7 Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4. Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3. Input: [1,2,3,4,5] Output: 4 Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4. Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are engaging multiple transactions at the same time. You must sell before buying again. Input: [7,6,4,3,1] Output: 0 Explanation: In this case, no transaction is done, i.e. max profit = 0.
题意:股票多次买卖的最大利润(必须先买后卖,不能多次买入后卖出)
本题是上一题的加强版:
>>> def maxProfit(prices): profit = 0 for i in range(len(prices)-1): t = prices[i+1]-prices[i] if t>0: profit += t return profit >>> data1 = [7,1,5,3,6,4] >>> maxProfit(data1) 7 >>> data2 = [1,2,3,4,5] >>> maxProfit(data2) 4 >>> data3 = [7,6,4,3,1] >>> maxProfit(data3) 0 >>>
相关单词
stock
英 [stɒk] 美 [stɑːk]
n.股票;(商店的)现货,存货,库存;储备物;备用物;供应物;股本;资本
vt.存货;贮备,贮存(食物、书籍等)
adj.老一套的;陈腐的;(商店里)常备的,通常有的
permit
英 [pəˈmɪt , ˈpɜːmɪt] 美 [pərˈmɪt , ˈpɜːrmɪt]
n.许可证;特许证(尤指限期的)
v.允许;准许;使有可能
前缀per- 自始至终
词根-mit- 送
complete
英 [kəmˈpliːt] 美 [kəmˈpliːt]
vt.完成;结束;填写(表格);使完整;使完美
adj.完整的;(用以强调)完全的,彻底的;全部的;整个的;包括,含有(额外部分或特征)
前缀com- 加强
词根-plet- 满的;填满
transaction
英 [trænˈzækʃn] 美 [trænˈzækʃn]
n.交易;处理;业务;买卖;办理
前缀trans- 变换
词根-act- 行动
algorithm
英 [ˈælɡərɪðəm] 美 [ˈælɡərɪðəm]
n.算法;计算程序
maximum
英 [ˈmæksɪməm] 美 [ˈmæksɪməm]
n.最大限度;最大量;最高限度
adj.最高的;最多的;最大极限的
profit
英 [ˈprɒfɪt] 美 [ˈprɑːfɪt]
n.利润;收益;赢利;好处;利益;裨益
v.获益;得到好处;对…有用(或有益)
前缀pro- 在前
词根-fit- (=fict)做, 制作
engage
英 [ɪnˈɡeɪdʒ] 美 [ɪnˈɡeɪdʒ]
v.从事;聘用;吸引住(注意力、兴趣);雇用;与…建立密切关系;尽力理解
前缀en- 加强; 处于状态; 使...进入
词根-gag- 约定, 担保
multiple
英 [ˈmʌltɪpl] 美 [ˈmʌltɪpl]
n.倍数
adj.数量多的;多种多样的
前缀multi- 多的