class Solution(object): def colorBorder(self, grid, r0, c0, color): """ :type grid: List[List[int]] :type r0: int :type c0: int :type color: int :rtype: List[List[int]] """ old_c = grid[r0][c0] if old_c==color:return grid m = len(grid) n = len(grid[0]) dp=[[0]*n for i in range(m)] def is_b(x,y): if x<0 or y<0 or x>=m or y>=n: return 1 elif grid[x][y]!=old_c and dp[x][y]==0: return 1 return 0 def dfs(x,y): if x<0 or y<0 or x>=m or y>=n: return elif grid[x][y]!=old_c: return elif dp[x][y]==1: return if is_b(x-1,y) or is_b(x+1,y) or is_b(x,y-1) or is_b(x,y+1): grid[x][y] = color dp[x][y] =1 dfs(x-1,y) dfs(x+1,y) dfs(x,y-1) dfs(x,y+1) dfs(r0,c0) return grid
股票利润:
1、只做一次交易:
class Solution(object): def maxProfit(self, prices): """ :type prices: List[int] :rtype: int """ if len(prices)==0: return 0 min1 = prices[0] n = len(prices) for i in range(n): min1 = min(min1,prices[i]) prices[i] = prices[i]-min1 return max(prices)
2、多次交易:
122. 买卖股票的最佳时机 II
class Solution(object): def maxProfit(self, prices): """ :type prices: List[int] :rtype: int """ min1 =10e4 dp = 0 for i in range(1,len(prices)): dp +=max(0,prices[i]-prices[i-1]) return dp
3、只做2次交易:
class Solution(object): def maxProfit(self, prices): """ :type prices: List[int] :rtype: int """ n = len(prices) if n==0:return 0 dp1=[0]*n dp2=[0]*n min1 = prices[0] for i in range(1,n): min1 =min(min1,prices[i]) dp1[i] = prices[i]-min(min1,prices[i]) max1 = prices[-1] for j in range(n-2,-1,-1): dp2[j] = max(max(prices[j],max1)-prices[j],dp2[j+1]) max1 = max(prices[j],max1) max1 = dp1[-1] # print(dp1,dp2) for i in range(n-1): max1= max(max1,dp1[i]+dp2[i+1]) return max1
含手续费:
714. 买卖股票的最佳时机含手续费
class Solution(object): def maxProfit(self, prices, fee): """ :type prices: List[int] :type fee: int :rtype: int """ sell = 0 buy = -5e6 for i in range(len(prices)): buy = max(sell-prices[i],buy) sell = max(buy+prices[i]-fee,sell) return sell
309. 最佳买卖股票时机含冷冻期
需要一个sell 记录前两天完成交易的利润,sell1表示前一天的;
class Solution(object): def maxProfit(self, prices): """ :type prices: List[int] :type fee: int :rtype: int """ sell = 0 buy = -5e6 if len(prices)<=1:return 0 buy = max(sell-prices[0],buy) sell = max(buy+prices[0],sell) sell1 = max(buy+prices[1],sell) buy1 = max(-prices[1],buy) for i in range(2,len(prices)): tmp = sell1 sell1 = max(buy1+prices[i],sell1) buy1 = max(sell-prices[i],buy1) sell = tmp return sell1
特例:k>数组的一半长度 md
188. 买卖股票的最佳时机 IV
class Solution(object): def maxProfit(self, k, prices): """ :type k: int :type prices: List[int] :rtype: int """ n = len(prices) if n<=1:return 0 if k>=n: max1 = prices[0] d = 0 for i in range(1,len(prices)): if prices[i]>prices[i-1]: d+=prices[i]-prices[i-1] return d dp=[[[0]*2 for j in range(k+1)] for i in range(n+1)] dp[0][0][1] = -prices[0] for i in range(n+1):#jiaoyi 1ci dp[i][0][0] = 0#max(dp[i-1][0][1]+prices[i],dp[i-1][0][0]) dp[i][0][1] = -5e6#max(dp[i-1][0][0]-prices[i],dp[i-1][0][1]) for j in range(k+1): dp[0][j][0] = 0 dp[0][j][1] = -5e6 for i in range(1,n+1): for j in range(1,k+1): dp[i][j][0] = max(dp[i-1][j][0],dp[i-1][j][1]+prices[i-1]) dp[i][j][1] = max(dp[i-1][j][1],dp[i-1][j-1][0]-prices[i-1]) return dp[-1][k][0]