huawei0506 3
import collections n = int(input().strip()) k = int(input().strip()) k_list = list(map(int,input().strip().split())) k_list = [(k_list[i],k_list[i+1]) for i in range(len(k_list))if i %2 == 0] girl_boy = list(map(int,input().strip().split())) girl = girl_boy[:2] boy = girl_boy[2:] map_ = [] for i in range(n): line = list(input().strip().split()) map_.append(line) q = collections.deque() # 添加时间维度,判断第几层,适用于bfs中需要判断层次的题目 q.append((boy[0],boy[1],0)) flag = True while len(q) != 0: if flag == False: break tmpx,tmpy,time = q.popleft() for pulsx,plusy in [0,0],[0,1],[1,0],[-1,0],[0,-1]: x = tmpx+pulsx y = tmpy+plusy if x >=n or y >=n or x < 0 or y < 0: continue elif map_[x][y][(time+1)%3] == '1' or (x,y) in k_list: continue else: if [x,y] == girl: flag =False break else: q.append((x,y,time+1)) print(time+1)
leecode 121
# 交易一次,套用交易k次的板子 class Solution: def maxProfit(self, prices): # k笔交易 # 2*k个变量 k = 1 l = prices buy_list = [-l[0]]*k sell_list = [0]*k if len(l) == 1: return 0 else: for i in range(1,len(l)): for j in range(k): if j == 0: buy_list[j] = max(buy_list[j],-l[i]) sell_list[j] = max(sell_list[j],buy_list[j]+l[i]) else: buy_list[j] = max(buy_list[j],sell_list[j-1]-l[i]) sell_list[j] = max(sell_list[j],buy_list[j]+l[i]) return max(sell_list) if __name__ == "__main__": prices = [1,4,2] solution = Solution() ans = solution.maxProfit(prices) print(ans)
leecode122
# 任意交易,连个变量,类似于k次交易的板子,只不过对第一次交易不用特殊处理,因为 # 这个板子用两个变量存储了多次交易的收益,没有数组就不存在溢出的风险 #dp1 dp0分别代表买了股票和卖了股票 class Solution: def maxProfit(self, prices: List[int]) -> int: l = prices dp0 = -l[0] dp1 = 0 if len(l) == 1: return 0 for i in range(1,len(l)): dp0,dp1= max(dp0,dp1-l[i]),max(dp0+l[i],dp1) return dp1
leecode123
# 两笔交易 # 3 3 5 0 0 3 1 4 # 6 # 四个转移变量 # 套用k次交易的板子 class Solution: def maxProfit(self, prices): # k笔交易 # 2*k个变量 k = 2 l = prices buy_list = [-l[0]]*k sell_list = [0]*k if len(l) == 1: return 0 else: for i in range(1,len(l)): for j in range(k): if j == 0: buy_list[j] = max(buy_list[j],-l[i]) sell_list[j] = max(sell_list[j],buy_list[j]+l[i]) else: buy_list[j] = max(buy_list[j],sell_list[j-1]-l[i]) sell_list[j] = max(sell_list[j],buy_list[j]+l[i]) return max(sell_list)
leecode188
# k笔交易 # 2*k个变量 # 注意临界值的处理 j == 0(第一次交易),用了数组来存储, # k次交易的收益,存在溢出风险 class Solution: def maxProfit(self, k: int, prices: List[int]) -> int: # k笔交易 # 2*k个变量 l = prices buy_list = [-l[0]]*k sell_list = [0]*k if len(l) == 1: return 0 else: for i in range(1,len(l)): for j in range(k): if j == 0: buy_list[j] = max(buy_list[j],-l[i]) sell_list[j] = max(sell_list[j],buy_list[j]+l[i]) else: buy_list[j] = max(buy_list[j],sell_list[j-1]-l[i]) sell_list[j] = max(sell_list[j],buy_list[j]+l[i]) return max(sell_list)
leecode309
# 任意交易且包含冷冻期,套用任意交易的板子 # dp0 dp1分别代表买了股票和卖了股票的收入,,dp2存储t-2时刻的卖出股票的收入 class Solution: def maxProfit(self, prices): l = prices dp0 = -l[0] dp1 = 0 dp2 = 0 if len(l) == 1: return 0 for i in range(1,len(l)): dp0 = max(dp0,-l[i] +(dp2 if i>1 else 0)) # 要注意dp2的位置 dp2 = dp1 dp1 = max(dp1,dp0+l[i]) return dp1
leecode 741
#和任意次数交易一样,只需要减去手续费 class Solution: def maxProfit(self, prices, fee) -> int: l = prices dp0 = -l[0] dp1 = 0 if len(l) == 1: return 0 for i in range(1,len(l)): dp0,dp1= max(dp0,dp1-l[i]),max(dp0+l[i]-fee,dp1) return dp1
快排求top-k
import random class Solution: def findKthLargest(self, nums, k): l = 0 r = len(nums) - 1 target = len(nums) - k while True: piviotIndex = self.partion(l,r,nums) if piviotIndex == target: return nums[piviotIndex] elif piviotIndex > target: r = piviotIndex - 1 else: l = piviotIndex + 1 def partion(self,l,r,nums): randomindex = random.randint(l,r) nums[l],nums[randomindex] = nums[randomindex],nums[l] piviot = nums[l] piviot_index = l # 不能丢,否则容易超时 l = l + 1 while True: while l<=r and nums[l] < piviot: l += 1 while l<=r and nums[r] > piviot: r -= 1 if l >= r: break nums[l],nums[r] = nums[r],nums[l] # 不能丢,否则容易超时,特别是没有走上面两个循环的时候 l += 1 r -= 1 nums[r],nums[piviot_index] = nums[piviot_index],nums[r] return r if __name__ == "__main__": solution = Solution() nums = [3,2,1,5,6,4] k = 2 ans = solution.findKthLargest(nums,k) print(ans)