classSolution: defmaxScore(self, s: str) -> int: num = 0 for x in s: if x=='1': num += 1 res = 0 l = 0 for x in s[:-1]: if x == '0': l += 1 res = max(res, l+num) else : num -= 1 res = max(res, l+ num) return res
classSolution: defmaxScore(self, cardPoints: List[int], k: int) -> int: num = sum(cardPoints[:k]) res = num for i in range(1,k+1): num = num - cardPoints[k-i] + cardPoints[-i] res = max(num, res) return res
from collections import defaultdict classSolution: deffindDiagonalOrder(self, nums: List[List[int]]) -> List[int]: mm = defaultdict(list) for i in range(len(nums)): for j in range(len(nums[i])): mm[i+j].append(nums[i][j]) n = max(mm.keys()) res = [] for i in range(n+1): if len(mm[i])==0: continue for j in range(len(mm[i])-1 ,-1,-1): res.append(mm[i][j]) return res
classSolution: defconstrainedSubsetSum(self, nums: List[int], k: int) -> int: res = nums[0] dp = nums[:] dp[0] = nums[0] s = [0] for i in range(1, len(nums)): if i-s[0]>k: s.pop(0) dp[i] = nums[i] dp[i] = max(dp[i], dp[s[0]] + nums[i]) while s and dp[i] >= dp[s[-1]]: s.pop() s.append(i) res = max(res, dp[i]) return res