classSolution: defmaxAscendingSum(self, nums: List[int]) -> int: curCount = 0 pre = 0 ans = 0 for n in nums: if n > pre: curCount += n else: curCount = n pre = n ans = max(curCount, ans) return ans
classSolution: defgetNumberOfBacklogOrders(self, orders: List[List[int]]) -> int: MOD = 1000000007 buy = [] sell = [] buypm = {} sellpm = {} for p, m, t in orders: if t == 0: while m > 0andlen(sell) > 0and sell[0] <= p: if sellpm[sell[0]] > m: sellpm[sell[0]] -= m m = 0 else: m -= sellpm[sell[0]] del sellpm[sell.pop(0)] if m > 0: if p notin buy: insort(buy, p) buypm[p] = m else: buypm[p] += m else: while m > 0andlen(buy) > 0and buy[-1] >= p: if buypm[buy[-1]] > m: buypm[buy[-1]] -= m m = 0 else: m -= buypm[buy[-1]] del buypm[buy.pop()] if m > 0: if p notin sell: insort(sell, p) sellpm[p] = m else: sellpm[p] += m return (sum(sellpm.values()) + sum(buypm.values())) % MOD
classSolution: defgetNumberOfBacklogOrders(self, orders: List[List[int]]) -> int: MOD = 1000000007 buy = [] # a MAX priority queue sell = [] # a Min pririty queue for p, m, t in orders: if t == 0: while m > 0andlen(sell) > 0and sell[0][0] <= p: if sell[0][1] > m: sell[0][1] -= m m = 0 else: m -= sell[0][1] heappop(sell) if m > 0: heappush(buy, [-p, m]) else: while m >0andlen(buy) > 0andabs(buy[0][0]) >= p: if buy[0][1] > m: buy[0][1] -= m m = 0 else: m -= buy[0][1] heappop(buy) if m > 0: heappush(sell, [p, m]) ans = sum([i[1] for i in buy ]) % MOD + sum(i[1] for i in sell) % MOD return ans % MOD
# 10 # [2,3,2,1,0,0,0] # find the maximum value that fits in # [1,2,1] classSolution: defmaxValue(self, n: int, index: int, maxSum: int) -> int: maxSum -= n if maxSum == 0: return1 defcountM(i, p, l): if p == 0: return0 left = min(i, p-1) right = min(l - i, p) return (max(p-left,0 ) + max(p-1,0))* left /2 + (p + max(p - (right-1), 0))* right / 2 l = 0 r = maxSum + n # upper exclusive bound !!! # find the min make g(m) valid # find the max countM(index, m, n) <= maxSum <=> find the min countM(index, m, n) > maxSum , index -= 1 while l < r: m = l + (r-l)//2 if countM(index, m, n) > maxSum: r = m #[l, m) else: l = m + 1#[m+1, r) countM(index, m, n) <= maxSum return l
classSolution: # Time 19.52% # Space 19.86% # Date 20210406 defcountDifferentSubsequenceGCDs(self, nums: List[int]) -> int: lru_cache(None) defdivisors(n): for i inrange(2, int(sqrt(n) + 1)): if n % i == 0: s = divisors(n//i) returnlist(set(s + [i * q for q in s])) returnlist(set([1,n])) dic = defaultdict(list) nums = set(nums) for num in nums: divs = divisors(num) for div in divs: dic[div].append(num) ans = 0 for div in dic: num_list = dic[div] s = num_list[0] for num in num_list: s = gcd(s, num) if s == div: break if div == s: ans += 1 return ans
#from collections import defualtdict classSolution: # Time 97.60% m + m/2 + m/3 + ... = O(mlogm) # Space 66.44% o(1) # Date 20210406 defcountDifferentSubsequenceGCDs(self, nums: List[int]) -> int: T = max(nums) + 1 nums = set(nums) ans = 0 for div inrange(1, T): g = 0 for num inrange(div, T, div): if num in nums: g = gcd(g, num) if g == div: break if g == div: ans += 1 return ans
classSolution: defgetXORSum(self, arr1: List[int], arr2: List[int]) -> int: a1 = arr1[0] for a in arr1[1:]: a1 ^= a a2 = arr2[0] for a in arr2[1:]: a2 ^= a return a1 & a2
244
Rank
Score
Finish Time
Date
Count
5191 / 12835
7
0:47:50(1 bugs in 1)
20210530
0
1880. Check if Word Equals Summation of Two Words
1 2 3 4 5 6 7 8 9 10
classSolution: defisSumEqual(self, firstWord: str, secondWord: str, targetWord: str) -> bool: deftoNum(word): ans = 0 for w in word: #print(ord(w) - ord('a')) ans = ans * 10 + (ord(w) - ord('a')) return ans #print(toNum(firstWord), toNum(secondWord), toNum(targetWord)) return toNum(firstWord) + toNum(secondWord) == toNum(targetWord)
# cr:DBabichev classSolution: defminDifference(self, nums: List[int], queries: List[List[int]]) -> List[int]: N, ans, dp = max(nums), [], [[0] * (max(nums) + 1)] for num in nums: t = dp[-1][:] t[num] += 1 dp.append(t) # zip 用法 # >>> a = [1, 2, 3] # >>> b = [4, 5, 6] # >>> list(zip(a,b,range(3))) # [(1, 4, 0), (2, 5, 1), (3, 6, 2)] # >>> [] or [-1] # [-1] for a, b in queries: cur_nums = [i for x, y, i inzip(dp[b+1], dp[a], range(N+1)) if y != x] ans.append(min([b - a for a, b inzip(cur_nums, cur_nums[1:])] or [-1] )) return ans