diff --git a/Python3/801.py b/Python3/801.py index 433d2ad..6eb8185 100644 --- a/Python3/801.py +++ b/Python3/801.py @@ -1,5 +1,39 @@ __________________________________________________________________________________________________ +sample 88 ms submission +class Solution: + def minSwap(self, A: List[int], B: List[int]) -> int: + n = len(A) + swap, noswap = [n] * n, [n] * n + swap[0], noswap[0] = 1, 0 + for i in range(1, len(A)): + if A[i] > A[i-1] and B[i] > B[i-1] and A[i] > B[i-1] and B[i] > A[i-1]: + noswap[i] = min(noswap[i-1], swap[i-1]) + swap[i] = min(noswap[i-1], swap[i-1]) + 1 + elif A[i] > A[i-1] and B[i] > B[i-1]: + noswap[i] = noswap[i-1] + swap[i] = swap[i-1] + 1 + elif A[i] > B[i-1] and B[i] > A[i-1]: + noswap[i] = swap[i-1] + swap[i] = noswap[i-1] + 1 + return min(swap[-1], noswap[-1]) __________________________________________________________________________________________________ - +sample 96 ms submission +class Solution: + def minSwap(self, A: List[int], B: List[int]) -> int: + + minSwap, minNotSwap= 1, 0 + for i in range(1, len(A)): + swap, notSwap = len(A), len(A) + + if A[i] > B[i-1] and B[i] > A[i-1]: + swap = 1 + minNotSwap + notSwap = minSwap + if A[i] > A[i-1] and B[i] > B[i-1]: + swap = min(swap,1 + minSwap) + notSwap = min(notSwap, minNotSwap) + + minSwap, minNotSwap = swap, notSwap + + return min(minSwap, minNotSwap) __________________________________________________________________________________________________ diff --git a/Python3/802.py b/Python3/802.py index 433d2ad..ae0d374 100644 --- a/Python3/802.py +++ b/Python3/802.py @@ -1,5 +1,42 @@ __________________________________________________________________________________________________ - +sample 712 ms submission +class Solution: + def eventualSafeNodes(self, graph): + # return t if i will be in a cycle, f if it will not. + + def explore(i): + visited[i] = 0 + for v in graph[i]: + # if neighbor has been visited before or after exploration, it is in a cycle + # -1 => unvisited; 0 => visited; 1 => no cycle? + if visited[v] == 0 or (visited[v] == -1 and explore(v)): return True + visited[i] = 1 + res.append(i) + return False + visited, res = [-1] * len(graph), [] + for i in range(len(graph)): + if visited[i] == -1: explore(i) + return sorted(res) __________________________________________________________________________________________________ - +sample 724 ms submission +class Solution: + def eventualSafeNodes(self, graph: List[List[int]]) -> List[int]: + #color[i], 0 means not visited. 1 means safe. 2 means unsafe. + color=[0] * len(graph) + res=[] + for i in range(len(graph)): + if self.dfs(graph,i,color): + res.append(i) + res.sort() + return res + + def dfs(self,graph,i,color): + if color[i] != 0: + return color[i] == 1 + color[i] = 2 + for e in graph[i]: + if not self.dfs(graph,e,color): + return False + color[i] = 1 + return True __________________________________________________________________________________________________ diff --git a/Python3/803.py b/Python3/803.py index 433d2ad..bdbc0f2 100644 --- a/Python3/803.py +++ b/Python3/803.py @@ -1,5 +1,115 @@ __________________________________________________________________________________________________ - +sample 604 ms submission +class Solution: + def hitBricks(self, grid: List[List[int]], hits: List[List[int]]) -> List[int]: + if not grid or not grid[0]: + return [] + + m, n = len(grid), len(grid[0]) + EMPTY = 0 + BRICK = 1 + DROP = 2 + WAIT = 3 + + def count(x, y): + cnt = 1 + grid[x][y] = DROP + + if x > 0 and grid[x - 1][y] == BRICK: + cnt += count(x - 1, y) + if y > 0 and grid[x][y - 1] == BRICK: + cnt += count(x, y - 1) + if x + 1 < m and grid[x + 1][y] == BRICK: + cnt += count(x + 1, y) + if y + 1 < n and grid[x][y + 1] == BRICK: + cnt += count(x, y + 1) + + return cnt + + for x, y in hits: + if grid[x][y] == BRICK: + grid[x][y] = WAIT + + for y in range(n): + if grid[0][y] == BRICK: + count(0, y) + + ans = [] + + for x, y in hits[::-1]: + if grid[x][y] == WAIT: + grid[x][y] = BRICK + + if ( + x == 0 or + (x > 0 and grid[x - 1][y] == DROP) or + (y > 0 and grid[x][y - 1] == DROP) or + (x + 1 < m and grid[x + 1][y] == DROP) or + (y + 1 < n and grid[x][y + 1] == DROP) + ): + ans.append(count(x, y) - 1) + else: + ans.append(0) + else: + ans.append(0) + + return ans[::-1] __________________________________________________________________________________________________ - +sample 608 ms submission +class Solution: + """ + T: O(m * n) + S: O(m * n) + """ + def hitBricks(self, grid: List[List[int]], hits: List[List[int]]) -> List[int]: + if not grid or not grid[0] or not hits: + return [] + + m, n, k = len(grid), len(grid[0]), len(hits) + EMPTY = 0 + BRICK = 1 + ERASURE = 2 + DROPED = 3 + + def count(x, y): + grid[x][y] = DROPED + cnt = 1 + + if x > 0 and grid[x - 1][y] == BRICK: + cnt += count(x - 1, y) + if y > 0 and grid[x][y - 1] == BRICK: + cnt += count(x, y - 1) + if x + 1 < m and grid[x + 1][y] == BRICK: + cnt += count(x + 1, y) + if y + 1 < n and grid[x][y + 1] == BRICK: + cnt += count(x, y + 1) + + return cnt + + for x, y in hits: + if grid[x][y] == BRICK: + grid[x][y] = ERASURE + + for y in range(n): + if grid[0][y] == BRICK: + count(0, y) + + ans = [0] * k + + for i in range(k - 1, -1, -1): + x, y = hits[i] + + if grid[x][y] == ERASURE: + grid[x][y] = BRICK + + if ( + (x == 0) or + (x > 0 and grid[x - 1][y] == DROPED) or + (y > 0 and grid[x][y - 1] == DROPED) or + (x + 1 < m and grid[x + 1][y] == DROPED) or + (y + 1 < n and grid[x][y + 1] == DROPED) + ): + ans[i] = count(x, y) - 1 + + return ans __________________________________________________________________________________________________ diff --git a/Python3/804.py b/Python3/804.py index 433d2ad..a4715cc 100644 --- a/Python3/804.py +++ b/Python3/804.py @@ -1,5 +1,35 @@ __________________________________________________________________________________________________ - +sample 24 ms submission +class Solution: + def uniqueMorseRepresentations(self, words: List[str]) -> int: + morse_codes = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."] + alphabet = {k: v for v, k in enumerate([letter for letter in "abcdefghijklmnopqrstuvwxyz"])} + transformations = [] + + for word in words: + transformation = "" + + for char in word: + transformation += morse_codes[alphabet[char]] + + transformations.append(transformation) + + return len(list(set(transformations))) __________________________________________________________________________________________________ +sample 12936 kb submission +#!/user/bin/env python3 + + +class Solution: + table = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."] + + def encode_c(self, c): + return self.table[ord(c) - ord('a')] + + def encode(self, word): + return ''.join(map(self.encode_c, word)) + def uniqueMorseRepresentations(self, words: List[str]) -> int: + results = set(self.encode(word) for word in words) + return len(results) __________________________________________________________________________________________________ diff --git a/Python3/805.py b/Python3/805.py index 433d2ad..297026b 100644 --- a/Python3/805.py +++ b/Python3/805.py @@ -1,5 +1,84 @@ __________________________________________________________________________________________________ +sample 40 ms submission +class Solution: + def splitArraySameAverage(self, A: List[int]) -> bool: + L, total = len(A), sum(A) + A.sort() + + '''个数不会超过数组总个数的一半 + 1''' + for i in range(1, L // 2 + 1): + ''' + i个数 x sum(A) == i个数之和 x len(A) + 所以 sum(A) x i ➗ len(A) == i个数之和 必然是整数 + ''' + if total * i % L: + continue + + + ''' 从数组A中,找到k个数之和等于target的可能性''' + k, target, index = i, total * i // L, 0 + + if self.dfs(A, k, target, index): + return True + + return False + + + ''' + 从数组A中,找到k个数之和等于target的可能性 + combination sum II 或者 k sum那两道题的简易版 + ''' + def dfs(self, A, k, target, index): + if not k and not target: + return True + + if not k or target < 0: + return False + for i in range(index, len(A)): + if i > index and A[i] == A[i -1]: + continue + + if target < A[i] * k or target > A[-1] * k: + break + + if self.dfs(A, k - 1, target - A[i], i + 1): + return True + + return False __________________________________________________________________________________________________ - +sample 44 ms submission +class Solution: + def splitArraySameAverage(self, A: List[int]) -> bool: + A.sort() + + L, total = len(A), sum(A) + + for k in range(1, L // 2 + 1): + if total * k % L: + continue + + start_index, target = 0, total * k // L + if self.dfs(A, k, start_index, target): + return True + + return False + + + def dfs(self, A, k, start_index, target): + if k == 0 and target == 0: + return True + + if k == 0 or target <= 0: + return False + + if start_index < len(A) and target < k * A[start_index] or target > k * A[-1]: + return False + + for i in range(start_index, len(A)): + if i > start_index and A[i] == A[i - 1]: + continue + + if self.dfs(A, k - 1, i + 1, target - A[i]): + return True __________________________________________________________________________________________________ diff --git a/Python3/806.py b/Python3/806.py index 433d2ad..ef0743e 100644 --- a/Python3/806.py +++ b/Python3/806.py @@ -1,5 +1,35 @@ __________________________________________________________________________________________________ - +sample 24 ms submission +class Solution: + def numberOfLines(self, widths: List[int], S: str) -> List[int]: + + letters = 'abcdefghijklmnopqrstuvwxyz' + keys = list(letters) + print(keys) + + letter_width = dict(zip(keys, widths)) + + ptr = 0 + lines = 1 + for i in range(len(S)): + if ptr + letter_width[S[i]] > 100: + lines += 1 + ptr = letter_width[S[i]] + else: + ptr += letter_width[S[i]] + + return [lines, ptr] __________________________________________________________________________________________________ +sample 13000 kb submission +class Solution: + def numberOfLines(self, widths, S): + lines, width = 1, 0 + for c in S: + w = widths[ord(c) - ord('a')] + width += w + if width > 100: + lines += 1 + width = w + return lines, width __________________________________________________________________________________________________ diff --git a/Python3/807.py b/Python3/807.py index 433d2ad..2c5ce0c 100644 --- a/Python3/807.py +++ b/Python3/807.py @@ -1,5 +1,17 @@ __________________________________________________________________________________________________ +sample 68 ms submission +class Solution: + def maxIncreaseKeepingSkyline(self, grid: List[List[int]]) -> int: + row_maxes = [max(row) for row in grid] + col_maxes = [max(col) for col in zip(*grid)] + return sum(min(row_maxes[r], col_maxes[c]) - val + for r, row in enumerate(grid) + for c, val in enumerate(row)) __________________________________________________________________________________________________ - +sample 13112 kb submission +class Solution: + def maxIncreaseKeepingSkyline(self, grid: List[List[int]]) -> int: + row, col = list(map(max, grid)), list(map(max, zip(*grid))) + return sum(min(i, j) for i in row for j in col) - sum(map(sum, grid)) __________________________________________________________________________________________________ diff --git a/Python3/808.py b/Python3/808.py index 433d2ad..3645a72 100644 --- a/Python3/808.py +++ b/Python3/808.py @@ -1,5 +1,52 @@ __________________________________________________________________________________________________ - +sample 36 ms submission +class Solution: + def soupServings(self, N: int) -> float: + if N>4800: + return 1.0 + soups=[[100,0],[75,25],[50,50],[25,75]] + memo=collections.defaultdict(float) + def dfs(A,B): + if (A,B) in memo: + return memo[(A,B)] + if A<=0: + if B<=0: + return 0.5 + return 1.0 + if B<=0: + return 0.0 + res=0.0 + for soup in soups: + res+=dfs(A-soup[0], B-soup[1]) + res/=len(soups) + memo[(A,B)]=res + return res + return dfs(N, N) __________________________________________________________________________________________________ +sample 40 ms submission +class Solution: + def soupServings(self, N: int) -> float: + if N >= 4000: + return 1 + + self.ans, self.dp = 0, {} + self.operations = [(100, 0), (75, 25), (50, 50), (25, 75)] + return self.__soupServings(N, N) + + def __soupServings(self, A, B): + if A <= 0 and B <= 0: + return 0.5 + if A <= 0: + return 1.0 + if B <= 0: + return 0.0 + + if (A, B) not in self.dp: + self.dp[(A, B)] = 0.25 * sum([ + self.__soupServings(A - self.operations[op][0], + B - self.operations[op][1]) + for op in range(4) + ]) + return self.dp[(A, B)] __________________________________________________________________________________________________ diff --git a/Python3/809.py b/Python3/809.py index 433d2ad..44980fd 100644 --- a/Python3/809.py +++ b/Python3/809.py @@ -1,5 +1,60 @@ __________________________________________________________________________________________________ - +sample 44 ms submission +class Solution: + def expressiveWords(self, S: str, words: List[str]) -> int: + return sum([1 for w in words if self.check(S, w)]) + + + def check(self, S:str, w:str) -> bool: + j = 0 + for i in range(len(S)): + if j < len(w) and S[i] == w[j]: + j += 1 + elif i > 1 and S[i - 2] == S[i - 1] and S[i - 1] == S[i]: + continue + elif i > 0 and i < len(S) - 1 and S[i - 1] == S[i] and S[i] == S[i + 1]: + continue + else: + return False + return j == len(w) __________________________________________________________________________________________________ - +sample 48 ms submission +class Solution: + def expressiveWords(self, S: str, words: List[str]) -> int: + s_target, target_count = self.squash(S) + count = 0 + for word in words: + s_word, word_count = self.squash(word) + if s_word == s_target and self.is_count_equal(word_count, target_count): + count += 1 + return count + + def squash(self, word): + # return squash word, count_array + if not word: + return '', [] + buff = [] + curr_c = word[0] + count = 0 + count_array = [] + for c in word: + if c == curr_c: + count += 1 + else: + buff.append(curr_c) + count_array.append(count) + count = 1 + curr_c = c + buff.append(curr_c) + count_array.append(count) + return ''.join(buff), count_array + + + def is_count_equal(self, count1, count2): + # count2 is longer string + for x, y in zip(count1, count2): + if x == y or (x < y and y >= 3): + continue + return False + return True __________________________________________________________________________________________________ diff --git a/Python3/810.py b/Python3/810.py index 433d2ad..647934c 100644 --- a/Python3/810.py +++ b/Python3/810.py @@ -1,5 +1,18 @@ __________________________________________________________________________________________________ - +sample 84 ms submission +class Solution: + def xorGame(self, nums: List[int]) -> bool: + if len(nums) % 2 == 0: + return True + else: + res = 0 + for num in nums: + res ^= num + return res == 0 __________________________________________________________________________________________________ - +sample 88 ms submission +import functools +class Solution: + def xorGame(self, nums: List[int]) -> bool: + return not len(nums)%2 or not functools.reduce(lambda x, y: x^y, nums) __________________________________________________________________________________________________ diff --git a/Python3/811.py b/Python3/811.py index 433d2ad..6e5859b 100644 --- a/Python3/811.py +++ b/Python3/811.py @@ -1,5 +1,66 @@ __________________________________________________________________________________________________ - +sample 44 ms submission +class Solution: + def subdomainVisits(self, cpdomains: List[str]) -> List[str]: + tempDict = {} + for i in cpdomains: + count,domain = i.split(' ') + if domain not in tempDict: + tempDict[domain] = int(count) + else: + tempDict[domain] += int(count) + + subdomain = domain.split('.')[1::] + temp = '' + for j in range(len(subdomain)-1, -1, -1): + if temp == '': + temp = subdomain[j] + else: + temp = subdomain[j] + '.' + temp + if temp not in tempDict: + tempDict[temp] = int(count) + else: + tempDict[temp] += int(count) + result = [] + for k,v in tempDict.items(): + result.append(str(v) + ' ' + k) + return result __________________________________________________________________________________________________ +sample 13036 kb submission +class Solution: + def subdomainVisits(self, cpdomains: List[str]) -> List[str]: + + dic={} + for i in cpdomains: + temp = i.split(" ") + + # temp[0] is the count + # temp[1] is the domain name + + subdomain = [] + subdomain.append(temp[1]) + a= temp[1] + b=temp[1] + for k in range(0,a.count(".")): + index = b.find(".") + string = b[index+1:] + subdomain.append(string) + b = string + + count= int(temp[0]) + for j in subdomain: + if j in dic: + dic[j] = dic[j]+count + else: + dic[j] = count + + + result = [] + for i in range(0,len(dic)): + key, value = dic.popitem() + string = str(value)+" " + key + result.append(string) + + return result __________________________________________________________________________________________________ diff --git a/Python3/812.py b/Python3/812.py index 433d2ad..c27f9f1 100644 --- a/Python3/812.py +++ b/Python3/812.py @@ -1,5 +1,43 @@ __________________________________________________________________________________________________ - +sample 48 ms submission +class Solution: + def largestTriangleArea(self, points: List[List[int]]) -> float: + points = self.boundary(points) + m = 0 + for p1,p2,p3 in itertools.permutations(points, 3): + a = self.calc_area(p1,p2,p3) + if a > m: + m = a + return m + + def boundary(self, points): + points = sorted(points, key=lambda x: (x[0], x[1])) + def cross(o,a,b): + return (o[0] - a[0]) * (o[1] - b[1]) - (o[1] - a[1]) * (o[0] - b[0]) + lower, upper = [],[] + for p in points: + while len(lower) >=2 and cross(lower[-2], lower[-1], p)<0: + lower.pop() + lower.append(tuple(p)) + + for p in reversed(points): + while len(upper) >=2 and cross(upper[-2], upper[-1], p)<0: + upper.pop() + upper.append(tuple(p)) + return list(set(lower+upper)) + + def calc_area(self, a,b,c): + return abs(a[0] * (b[1] - c[1]) + b[0] * (c[1] - a[1]) + c[0] * (a[1] - b[1])) / 2 __________________________________________________________________________________________________ - +sample 13120 kb submission +class Solution: + def largestTriangleArea(self, points: List[List[int]]) -> float: + res = 0 + N = len(points) + for i in range(N - 2): + for j in range(i + 1, N - 1): + for k in range(i + 2, N): + (x1, y1), (x2, y2), (x3, y3) = points[i], points[j], points[k] + res = max(res, 0.5 * abs(x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2))) + return res __________________________________________________________________________________________________ diff --git a/Python3/813.py b/Python3/813.py index 433d2ad..fb2b46a 100644 --- a/Python3/813.py +++ b/Python3/813.py @@ -1,5 +1,45 @@ __________________________________________________________________________________________________ - +sample 116 ms submission +import functools, itertools +memoise = functools.lru_cache(maxsize=None) +class Solution: + def largestSumOfAverages(self, A: List[int], K: int) -> float: + P = [0, *itertools.accumulate(A)] + + @memoise + def avg(i, j): + return (P[j] - P[i]) / (j - i) + + @memoise + def dp(n, k): + if 1 <= k <= n: + return max(dp(i, k-1) + avg(i, n) for i in range(k-1, n)) + elif k == n == 0: + return 0 + else: + return float('-inf') + #print(dp.cache_info()) + return dp(len(A), K) __________________________________________________________________________________________________ - +sample 13584 kb submission +class Solution: + def largestSumOfAverages(self, A: List[int], K: int) -> float: + # A = [1,2,3] + # K = 3 + def avg(i, j): + return sum(A[i: j + 1]) / (j - i + 1) + + dp = [[0 for k in range(K + 1)] for i in range(len(A))] + + for i in range(len(A)): + dp[i][1] = avg(0, i) + + for k in range(2, K + 1): + for i in range(k - 1, len(A)): + ans = 0 + for j in range(k - 1, i + 1): + ans = max(ans, dp[j - 1][k - 1] + avg(j, i)) + dp[i][k] = ans + print(dp) + return dp[-1][K] __________________________________________________________________________________________________ diff --git a/Python3/814.py b/Python3/814.py index 433d2ad..6159eac 100644 --- a/Python3/814.py +++ b/Python3/814.py @@ -1,5 +1,43 @@ __________________________________________________________________________________________________ +sample 24 ms submission +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None +class Solution: + def pruneTree(self, root: TreeNode) -> TreeNode: + def f(tree): + if tree: + if tree.left: tree.left = f(tree.left) + if tree.right: tree.right = f(tree.right) + if tree.val != 0 or tree.left or tree.right: + return tree + return f(root) __________________________________________________________________________________________________ +sample 13056 kb submission +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None +class Solution: + def pruneTree(self, root: TreeNode) -> TreeNode: + if root is None: + return None + + if root.val == 0 and root.left is None and root.right is None: + return None + + root.left = self.pruneTree(root.left) + root.right = self.pruneTree(root.right) + + if root.val == 0 and root.left is None and root.right is None: + return None + + return root __________________________________________________________________________________________________ diff --git a/Python3/815.py b/Python3/815.py index 433d2ad..17ee4c2 100644 --- a/Python3/815.py +++ b/Python3/815.py @@ -1,5 +1,72 @@ __________________________________________________________________________________________________ - +sample 400 ms submission +class Solution: + def numBusesToDestination(self, routes: List[List[int]], S: int, T: int) -> int: + #BFS: traverse all bus stop that are reachable by all the bus route that go via the Start + if S == T: + return 0 + # need to record all the routes you can take at each stop so that you can find all + # the stops you can reach when you take one time of bus + stop_routes = {} + for route, stops in enumerate(routes): + for stop in stops: + if stop not in stop_routes: + stop_routes[stop] = [route] + else: + stop_routes[stop].append(route) + # record all the stops you can reach when you take one time bus route + queue = collections.deque([S]) + # record route has been taken before + visited = set() + countRoute = 0 + while queue: + countRoute += 1 + # all stops you can reach last time + prev_stops = len(queue) + for _ in range(prev_stops): + curStop = queue.popleft() + for route in stop_routes[curStop]: + if route not in visited: + visited.add(route) + for stop in routes[route]: + if stop == T: + return countRoute + queue.append(stop) + return -1 __________________________________________________________________________________________________ +sample 408 ms submission +class Solution: + def numBusesToDestination(self, routes: List[List[int]], S: int, T: int) -> int: + M = max(map(max, routes)) + coroute = [[] for _ in range(M+1)] + for k, r in enumerate(routes): + for ri in r: + coroute[ri].append(k) + + old_routes = set() + print('arrows ok') + + n_steps = 0 + old = set() + at = set([S]) + while T not in at: + print(f'step {n_steps}') + old.update(at) + + to = set() + for i in at: + for rt in coroute[i]: + if rt in old_routes: + continue + to.update(routes[rt]) + old_routes.add(rt) + new = to - old + #print("new",new) + + if len(new) == 0: + return -1 + n_steps += 1 + at = new + return n_steps __________________________________________________________________________________________________ diff --git a/Python3/816.py b/Python3/816.py index 433d2ad..4ea87d3 100644 --- a/Python3/816.py +++ b/Python3/816.py @@ -1,5 +1,94 @@ __________________________________________________________________________________________________ - +sample 44 ms submission +class Solution(object): + def ambiguousCoordinates(self, s): + ''' + For each place to put the comma, we separate the string into two fragments. For example, with a string like "1234", we could separate it into fragments "1" and "234", "12" and "34", or "123" and "4". +Then, for each fragment, we have a choice of where to put the period, to create a list make(...) of choices. For example, "123" could be made into "1.23", "12.3", or "123". +Because of extranneous zeroes, we should ignore possibilities where the part of the fragment to the left of the decimal starts with "0" (unless it is exactly "0"), and ignore possibilities where the part of the fragment to the right of the decimal ends with "0", as these are not allowed. +Note that this process could result in an empty answer, such as for the case S = "(000)". + ''' + ''' + Example 2: +Input: "(00011)" +Output: ["(0.001, 1)", "(0, 0.011)"] +Explanation: +0.0, 00, 0001 or 00.01 are not allowed. + ''' + '''def make(frag): + N = len(frag) + for d in range(1, N+1): + left = frag[:d] + right = frag[d:] + if ((not left.startswith('0') or left == '0') + and (not right.endswith('0'))): + yield left + ('.' if d != N else '') + right + S = S[1:-1] + return ["({}, {})".format(*cand) + for i in range(1, len(S)) + for cand in itertools.product(make(S[:i]), make(S[i:]))]''' + def make(frag): + fragLen = len(frag) + for idx in range(1, fragLen+1): + left = frag[:idx] + right = frag[idx:] + #if (left == '0' or not left.startswith('0')) and (not right.endswith('0')): + if ((left == '0' or not left.startswith('0')) + and not right.endswith('0')): + yield left + ('.' if idx != fragLen else '') + right + s = s[1:-1] + return ['({}, {})'.format(*cand) for i in range(1, len(s)) for cand in itertools.product(make(s[:i]), make(s[i:]))] + '''result = [] + s = s[1:-1] + for i in range(1, len(s)): + for x in self.get_coordinate(s[:i]): + for y in self.get_coordinate(s[i:]): + result.append('({}, {})'.format(x, y)) + return list(map(str, result)) + def get_coordinate(self, x): + if len(x) == 1: + return x + if x[0] == '0' and x.endswith('0'): + return [] + if x[0] == '0': + return [x[0] + '.' + x[1:]] + if x.endswith('0'): + return [x] + return [x[:i] + '.' + x[i:] for i in range(1, len(x))] + [x]''' + '''res = [] + s = s[1:-1] + for i in range(1, len(s)): + for x in self.getCoord(s[:i]): + for y in self.getCoord(s[i:]): + res.append('({}, {})'.format(x,y)) + return res + def getCoord(self,x): + if len(x) == 1: + return x + if x[0]=='0' and x.endswith('0'): + return [] + if x[0] == '0': + return [x[0]+'.'+x[1:]] + if x.endswith('0'): + return [x] + return [x[:i]+'.'+x[i:] for i in range(1, len(x))]+[x]''' __________________________________________________________________________________________________ - +sample 48 ms submission +class Solution: + def ambiguousCoordinates(self, S: str) -> List[str]: + ''' + if S == "": return [] + if S == "0": return [S] + if S == "0XXX0": return [] + if S == "0XXX": return ["0.XXX"] + if S == "XXX0": return [S] + return [S, "X.XXX", "XX.XX", "XXX.X"...] + ''' + S = S[1:-1] + def f(S): + if not S or len(S) > 1 and S[0] == S[-1] == '0': return [] + if S[-1] == '0': return [S] + if S[0] == '0': return [S[0] + '.' + S[1:]] + return [S] + [S[:i] + '.' + S[i:] for i in range(1, len(S))] + return ['(%s, %s)' % (a, b) for i in range(len(S)) for a, b in itertools.product(f(S[:i]), f(S[i:]))] __________________________________________________________________________________________________ diff --git a/Python3/817.py b/Python3/817.py index 433d2ad..53e11c1 100644 --- a/Python3/817.py +++ b/Python3/817.py @@ -1,5 +1,42 @@ __________________________________________________________________________________________________ +sample 104 ms submission +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, x): +# self.val = x +# self.next = None +class Solution: + def numComponents(self, head: ListNode, G: List[int]) -> int: + G = set(G) + c = 0 + mxc = 0 + while head: + if head.val in G: + c = 1 + else: + mxc += c + c=0 + head = head.next + mxc+=c + return mxc __________________________________________________________________________________________________ +sample 108 ms submission +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, x): +# self.val = x +# self.next = None +class Solution: + def numComponents(self, head: ListNode, G: List[int]) -> int: + s = set(G) + cnt = 0 + # only need first one + while head: + if head.val in s and (not head.next or head.next.val not in s): + cnt += 1 + head = head.next + + return cnt __________________________________________________________________________________________________ diff --git a/Python3/818.py b/Python3/818.py index 433d2ad..16298ce 100644 --- a/Python3/818.py +++ b/Python3/818.py @@ -1,5 +1,120 @@ __________________________________________________________________________________________________ - +sample 44 ms submission +from functools import lru_cache +class Solution: + cache = {0:0} + @lru_cache(None) + def racecar(self, target: int) -> int: + #这里用dp,记录下来从0到任意一个点的距离,因为很多点不会经过,所以不用循环用递归 + #Dijkstra? + if target in self.cache: + return self.cache[target] + + k = target.bit_length() + + if target == (1< int: + #bfs + dp + res = 0 + q=[(0, 1), (None, None)] + + k = target.bit_length() + 1 + + while q: + pos, spd = q.pop(0) + if pos is None: + res += 1 + q.append((None, None)) + else: + if pos == target: + return res + else: + q.append((pos+spd, spd*2)) + if pos < target: + if spd > 0: + if pos + spd > target: + q.append((pos, -1)) + else: + q.append((pos, 1)) + else: + if spd < 0: + if pos+spd < target: + q.append((pos, 1)) + else: + q.append((pos, -1)) + return res __________________________________________________________________________________________________ +sample 48 ms submission +class Solution: + def racecar(self, target: int) -> int: + # Idea: DP with optimal substructure: + # memo(target) = min of: + # n + 1 + memo(2 ^ n - 1 - target) + # (n - 1) + 1 + memo(m) + memo(target - (2 ^ (n - 1) - 1 - m)) + # + # where: + # 2 ^ (n - 1) - 1 <= target <= 2 ^ n - r + # + # 1. Driving distance of 'A' * n = 2 ^ n - 1 + # 2. Try to let the car accelerate as much as possible + # 3. To reach target with shortest instruction, there're 2 ways defined: + # 1. drive with 'A' * n passing target and then turn back to target + # 2. drive with 'A' * (n - 1) arriving before target, + # then turn back with 'R' and drive 'A' * m, + # finally turn around with 'R' and drive 'A' * k to reach target + # + # Time: + # + # Space: + + # Define the max number of consecutive 'A's + n = target.bit_length() + # The car never pass through this position + barrier = 1 << n + memo = [None] * (barrier + 1) + # Base case where '' to reach target position 0 + memo = {0:0} + self.dp(target, memo) + + return memo[target] + + def dp(self, t, memo): + if t in memo: + return memo[t] + + n = t.bit_length() + accelerateDist = (1 << n) - 1 + if accelerateDist == t: + memo[t] = n + + return n + + # Keep accelerating (with 'A' * n) until reaching or passing target, then turn around + minInst = n + 1 + self.dp(accelerateDist - t, memo) + + # Keep accelerating until close to target (with 'A' * (n - 1)), + # then turn around (with 'R') and drive back for m distance, + # finally turn around (with 'R') and drive to target + accelerateDist = (1 << (n - 1)) - 1 + for m in range(n - 1): + backDist = (1 << m) - 1 + minInst = min(minInst, n + m + 1 + self.dp(t - (accelerateDist - backDist), memo)) + + memo[t] = minInst + return minInst __________________________________________________________________________________________________ diff --git a/Python3/819.py b/Python3/819.py index 433d2ad..b03d393 100644 --- a/Python3/819.py +++ b/Python3/819.py @@ -1,5 +1,66 @@ __________________________________________________________________________________________________ - +sample 24 ms submission +class Solution: + def mostCommonWord(self, paragraph: str, banned: List[str]) -> str: + for c in "!?',;.": + paragraph = paragraph.replace(c, " ") + words = paragraph.split() + for i in range(len(words)): + # word = words[i] + # word_ = "" + # for j in word: + # if j.isalpha(): + # word_ += j + words[i] = words[i].lower() + d = dict() + for word in words: + if word not in d: + d[word] = 0 + d[word] += 1 + tupples = [] + for word in d: + tupples.append((d[word], word)) + tupples.sort(reverse=True) + s = set(banned) + + ret = "" + for i in range(len(tupples)): + word = tupples[i][1] + if word not in s: + ret = word + break + + # print(words) + # print(tupples) + return ret __________________________________________________________________________________________________ - +sample 12944 kb submission +from collections import Counter +class Solution: + def mostCommonWord(self, paragraph: str, banned: List[str]) -> str: + paragraph=paragraph.lower() + #print(paragraph) + c=Counter() + paragraph2=[] + for j in range(len(paragraph)): + if paragraph[j] in ["!","?","'",",",";",".", ""]: + paragraph2.append(" ") + else: + paragraph2.append(paragraph[j]) + + maxkey="" + maxvalue=0 + paragraph=''.join(paragraph2) + sl=paragraph.split(" ") + for i in range(len(sl)): + c[sl[i]]+=1 + for key, value in c.items(): + if key not in banned: + if value > maxvalue and key !="": + maxkey=key + maxvalue=value + #print (paragraph2) + #print (c) + #print (maxkey) + return maxkey __________________________________________________________________________________________________ diff --git a/Python3/820.py b/Python3/820.py index 433d2ad..a021678 100644 --- a/Python3/820.py +++ b/Python3/820.py @@ -1,5 +1,41 @@ __________________________________________________________________________________________________ - +sample 100 ms submission +class Solution: + def minimumLengthEncoding(self, words: List[str]) -> int: + d = {} + for word in words: + n = len(word) + if n in d: + d[n].add(word) + else: + d[n] = set([word]) + keys = list(d.keys()) + keys.sort() + n = len(keys) + res = 0 + for i in range(n-1, -1, -1): + if i == n-1: + s = d[keys[i]] + L = len(d[keys[i]]) + res += L * keys[i] + L + else: + s = set([item[-keys[i]:] for item in s]) + L1 = len(s) + s = s | d[keys[i]] + L3 = len(s) + res += (L3 - L1) * keys[i] + (L3 - L1) + return res __________________________________________________________________________________________________ - +sample 15540 kb submission +from functools import reduce +class Solution: + def minimumLengthEncoding(self, words: List[str]) -> int: + T = lambda: collections.defaultdict(T) + root = T() + end = [] + for w in set(words): + end.append(reduce(lambda x,y: x[y], w[::-1], root) ) + end[-1]['#'] = len(w)+1 + + return sum(w['#'] for w in end if len(w)==1) __________________________________________________________________________________________________ diff --git a/Python3/821.py b/Python3/821.py index 433d2ad..82bd34e 100644 --- a/Python3/821.py +++ b/Python3/821.py @@ -1,5 +1,48 @@ __________________________________________________________________________________________________ - +sample 36 ms submission +class Solution: + def shortestToChar(self, S: str, C: str) -> List[int]: + + cs = [-len(S)] + [i for i in range(len(S)) if S[i]==C] + [2*len(S)] + + idx = 0 + res = [] + + for i in range(len(S)): + val = min(i-cs[idx], cs[idx+1]-i) + if val==0: + idx+=1 + res.append(val) + + return res __________________________________________________________________________________________________ +sample 13028 kb submission +class Solution: + def shortestToChar(self, S: str, C: str) -> List[int]: + ePosition = [] + + result = [] + length = len(S) + counter = 0 + + for i in range(length): + if S[i] == C: + ePosition.append(i) + + eCounts = len(ePosition) + + for i in range(length): + + if counter == eCounts: + result.append(abs(ePosition[counter - 1] - i)) + else: + left = abs(ePosition[counter- 1] - i) + right = abs(ePosition[counter] - i) + result.append(min(left, right)) + if i >= ePosition[counter]: + counter += 1 + + + return result __________________________________________________________________________________________________ diff --git a/Python3/822.py b/Python3/822.py index 433d2ad..75fa946 100644 --- a/Python3/822.py +++ b/Python3/822.py @@ -1,5 +1,31 @@ __________________________________________________________________________________________________ - +sample 104 ms submission +class Solution: + def flipgame(self, fronts: List[int], backs: List[int]) -> int: + #print(fronts + backs) + same = {x for i,x in enumerate(fronts) if x == backs[i]} + + ans = float('inf') + for x in fronts + backs: + if x not in same: + ans = min(ans, x) + + return ans if ans != float('inf') else 0 + + # for x in itertools.chain(fronts, backs): __________________________________________________________________________________________________ - +sample 112 ms submission +class Solution: + def flipgame(self, fronts: List[int], backs: List[int]) -> int: + ans = 1 << 32 + nums = set() + ex = set() + for a, b in zip(fronts, backs): + if a == b: + ex.add(a) + else: + nums.add(a) + nums.add(b) + diff = nums.difference(ex) + return min(diff) if diff else 0 __________________________________________________________________________________________________ diff --git a/Python3/823.py b/Python3/823.py index 433d2ad..84bec50 100644 --- a/Python3/823.py +++ b/Python3/823.py @@ -1,5 +1,57 @@ __________________________________________________________________________________________________ - +sample 148 ms submission +class Solution: + def numFactoredBinaryTrees(self, A: List[int]) -> int: + local_counts = {} + result = 0 + for a in sorted(A): + local_counts[a] = self.internalNumFactoredBinaryTrees(local_counts, a) + result += local_counts[a] + return result % (10**9 + 7) + + def internalNumFactoredBinaryTrees(self, local_counts, a): + result = 1 + for k, v in local_counts.items(): + if k * k > a: + break + if a % k != 0: + continue + b = int(a / k) + if b in local_counts: + result += (v * local_counts[b]) * (2 if b != k else 1) + return result __________________________________________________________________________________________________ - +sample 13048 kb submission +class Solution: + def numFactoredBinaryTrees(self, A: List[int]) -> int: + # init + n = len(A) + A.sort() + + # build dp + dp = [1] * n + + for i in range(1,n): + target = A[i] + left = 0 + right = i-1 + + while left <= right: + prod = A[left] * A[right] + + if prod == target: + if left != right: + dp[i] += dp[left] * dp[right] * 2 + else: + dp[i] += dp[left] * dp[right] + + left += 1 + right -= 1 + elif prod < target: + left += 1 + else: + right -= 1 + + # return sum + return sum(dp) % (10**9+7) __________________________________________________________________________________________________ diff --git a/Python3/824.py b/Python3/824.py index 433d2ad..c922ce3 100644 --- a/Python3/824.py +++ b/Python3/824.py @@ -1,5 +1,31 @@ __________________________________________________________________________________________________ - +sample 24 ms submission +class Solution: + def toGoatLatin(self, S: str) -> str: + words = S.split() + new_words = [] + for i, w in enumerate(words): + if w[0].lower() in {'a','e','i','o','u'}: + new = w + 'ma' + else: + new = w[1:] + w[0] + 'ma' + new_words.append(new+'a'*(i+1)) + return ' '.join(new_words) __________________________________________________________________________________________________ - +sample 13076 kb submission +class Solution: + def toGoatLatin(self, S: str) -> str: + words = S.split(" ") + vowels = set(["a","e","i","o","u","A","E","I","O","U"]) + + for i in range(len(words)): + if words[i][0] in vowels: + words[i] += ("ma") + else: + words[i] = words[i][1:] + words[i][0] + "ma" + + words[i] += "a"*(i+1) + + res = " ".join(words) + return res __________________________________________________________________________________________________ diff --git a/Python3/825.py b/Python3/825.py index 433d2ad..4c1bf16 100644 --- a/Python3/825.py +++ b/Python3/825.py @@ -1,5 +1,36 @@ __________________________________________________________________________________________________ - +sample 276 ms submission +class Solution: + def numFriendRequests(self, ages: List[int]) -> int: + res=0 + nums=[0]*121 + sums=[0]*121 + for i in ages: + nums[i]+=1 + for i in range(1,121): + sums[i]=sums[i-1]+nums[i] + for i in range(15,121): + if nums[i]==0: + continue + count=sums[i]-sums[i//2+7] + res+=(count-1)*nums[i] + return res __________________________________________________________________________________________________ - +sample 13524 kb submission +class Solution: + def numFriendRequests(self, ages: List[int]) -> int: + count = [0 for i in range(121)] + for a in ages: + count[a] += 1 + res = 0 + + for x in range(121): + for y in range(121): + if y <= 0.5*x + 7 or y > x or y > 100 > x: + continue + res += count[x]*count[y] + if x == y: + res -= count[x] + + return res __________________________________________________________________________________________________ diff --git a/Python3/826.py b/Python3/826.py index 433d2ad..d2cf2e8 100644 --- a/Python3/826.py +++ b/Python3/826.py @@ -1,5 +1,37 @@ __________________________________________________________________________________________________ - +sample 392 ms submission +class Solution: + def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: + output = 0 + len_task = len(difficulty) + all_task = list(sorted(zip(difficulty, profit))) + track = 0 + best = 0 + for able in sorted(worker): + while track < len_task: + diff, price = all_task[track] + if diff <= able: + if best <= price: + best = price + track += 1 + else: + break + output += best + return output __________________________________________________________________________________________________ - +sample 14952 kb submission +class Solution: + def maxProfitAssignment(self, difficulty, profit, worker) -> int: + rank = sorted(list(range(len(difficulty))), key=lambda i: difficulty[i]) + max_profit = 0 + p = 0 + w = sorted(worker) + i = 0 + for r in range(len(difficulty)+1): + while i < len(worker) and (r == len(difficulty) or w[i] < difficulty[rank[r]]): + p += max_profit + i += 1 + if r < len(difficulty): + max_profit = max(max_profit, profit[rank[r]]) + return p __________________________________________________________________________________________________ diff --git a/Python3/827.py b/Python3/827.py index 433d2ad..c15c701 100644 --- a/Python3/827.py +++ b/Python3/827.py @@ -1,5 +1,67 @@ __________________________________________________________________________________________________ - +sample 120 ms submission +class Solution: + def largestIsland(self, grid: List[List[int]]) -> int: + n = len(grid) + m = n and len(grid[0]) + if not m or not n: + return 0 + dic = collections.Counter() + + def dfs(i,j): + for I, J in [(i+1,j),(i-1,j),(i,j-1),(i,j+1)]: + if I>=0 and J >=0 and I< n and J int: + if not grid: return 0 + id = 2 + area = {0: 0} + for i in range(len(grid)): + for j in range(len(grid[0])): + if grid[i][j] != 1: continue + area[id] = self.visit(grid, i, j, id) + id += 1 + max_area = 0 + for i in range(len(grid)): + for j in range(len(grid[0])): + if grid[i][j] != 0: continue + tmp = set() + if i > 0: tmp.add(grid[i - 1][j]) + if j > 0: tmp.add(grid[i][j - 1]) + if i + 1 < len(grid): tmp.add(grid[i + 1][j]) + if j + 1 < len(grid[0]): tmp.add(grid[i][j + 1]) + max_area = max(max_area, 1 + sum([area[x] for x in tmp])) + return max_area if max_area else len(grid) * len(grid[0]) + def visit(self, grid, i, j, id) -> int: + ret, grid[i][j] = 1, id + if i > 0 and grid[i - 1][j] == 1: ret += self.visit(grid, i - 1, j, id) + if j > 0 and grid[i][j - 1] == 1: ret += self.visit(grid, i, j - 1, id) + if i + 1 < len(grid) and grid[i + 1][j] == 1: ret += self.visit(grid, i + 1, j, id) + if j + 1 < len(grid[0]) and grid[i][j + 1] == 1: ret += self.visit(grid, i, j + 1, id) + return ret __________________________________________________________________________________________________ diff --git a/Python3/828.py b/Python3/828.py index 433d2ad..be3eee9 100644 --- a/Python3/828.py +++ b/Python3/828.py @@ -1,5 +1,64 @@ __________________________________________________________________________________________________ +sample 60 ms submission +import string -__________________________________________________________________________________________________ +class Solution: + """O(n) solution + + Think about string "XAXAXXAX" and focus on making the second "A" a unique + character. + We can take "XA(XAXX)AX" and between "()" is our substring. To make the + second "A" counted as a uniq character, we need to: + + 1. insert "(" somewhere between the first and second A + 2. insert ")" somewhere between the second and third A + + For step 1 we have "A(XA" and "AX(A", 2 possibility. + For step 2 we have "A)XXA", "AX)XA" and "AXX)A", 3 possibilities. + + So there are in total 2 * 3 = 6 ways to make the second A a unique character + in a substring. + In other words, there are only 6 substring, in which this A contribute 1 + point as unique string. + + Instead of counting all unique characters and struggling with all possible + substrings, we can count for every char in S, how many ways to be found as a + unique char. We count and sum, and it will be the answer. + """ + + MODULO_BASE = 10 ** 9 + 7 + + def uniqueLetterString(self, s: str) -> int: + # index[c] stores the last two indexes of appearance of c. + index = {c: [-1, -1] for c in string.ascii_uppercase} + total = 0 + for i, c in enumerate(s): + k, j = index[c] + total += (i - j) * (j - k) + index[c] = [j, i] + # Calculate the last occurrence for each letter as well. + for c in index: + k, j = index[c] + total += (len(s) - j) * (j - k) + return total % self.MODULO_BASE +__________________________________________________________________________________________________ +sample 68 ms submission +class Solution: + def uniqueLetterString(self, S: str) -> int: + # https://leetcode.com/problems/unique-letter-string/discuss/128952/One-pass-O(N)-Straight-Forward + # https://www.icode9.com/content-4-25511.html + res = 0 + kMod = 1e9 + 7 + char_pos = collections.defaultdict(lambda : [-1, -1]) + for i, c in enumerate(S): # process all unique but last one + k, j = char_pos[c] + res += (j - k) * (i - j) + char_pos[c] = [j, i] + n = len(S) + for c in char_pos: # ATTN only process char_pos.keys(if you process S again,you will duplicate if some appear more than once) only process process last unique + k, j = char_pos[c] + res += (n - j) * (j - k) + + return int(res % kMod) __________________________________________________________________________________________________ diff --git a/Python3/829.py b/Python3/829.py index 433d2ad..319d5c5 100644 --- a/Python3/829.py +++ b/Python3/829.py @@ -1,5 +1,50 @@ __________________________________________________________________________________________________ +sample 56 ms submission +import collections +import itertools -__________________________________________________________________________________________________ +def prime_factors(n): + while n % 2 == 0: + n //= 2 + yield 2 + + p = 3 + while n >= p * p: + while n % p == 0: + n //= p + yield p + p += 2 + + if n > 1: + yield n + +class Solution: + def consecutiveNumbersSum(self, N: int) -> int: + while N % 2 == 0: + N //= 2 + + factors = collections.Counter(p for p in prime_factors(N)) + ret = 1 + for c in factors.values(): + ret *= c + 1 + return ret +__________________________________________________________________________________________________ +sample 64 ms submission +class Solution: + def consecutiveNumbersSum(self, N: int) -> int: + if N == 1: + return 1 + res = 1 + for i in range(2, int(N ** .5 + 1)): + if N % i == 0: + if i % 2 == 1: # If i is odd, then we can form a sum of length i + res += 1 + j = (N // i) # Check the corresponding N // i + if i != j and j % 2 == 1: + res += 1 + if N % 2 == 1: # If N is odd(2k + 1). Then N = k + k + 1, not included above + res += 1 + + return res __________________________________________________________________________________________________ diff --git a/Python3/830.py b/Python3/830.py index 433d2ad..9c0e791 100644 --- a/Python3/830.py +++ b/Python3/830.py @@ -1,5 +1,33 @@ __________________________________________________________________________________________________ - +sample 32 ms submission +class Solution(object): + def largeGroupPositions(self, S): + # do itertools.groupby in python + ans = [] + + i, j = 0, 0 + + while j < len(S): + if j == len(S) - 1 or S[j] != S[j+1]: + if j - i + 1 >= 3: + ans.append([i, j]) + i = j + 1 + j += 1 + + return ans __________________________________________________________________________________________________ - +sample 13208 kb submission +class Solution: + def largeGroupPositions(self, S: str) -> List[List[int]]: + result = [] + S = list(S) + S.insert(0,0) + S.append(0) + for i in range(1,len(S)-1): + if len(set(S[i:i+3])) == 1 and S[i-1] != S[i]: + start = i-1 + if len(set(S[i-2:i+1])) == 1 and S[i+1] != S[i]: + end = i-1 + result.append([start,end]) + return result __________________________________________________________________________________________________ diff --git a/Python3/831.py b/Python3/831.py index 433d2ad..7e0b7a9 100644 --- a/Python3/831.py +++ b/Python3/831.py @@ -1,5 +1,47 @@ __________________________________________________________________________________________________ - +sample 24 ms submission +class Solution: + def maskPII(self, S: str) -> str: + at=S.find('@') + if at>0: + return (S[0]+'*****'+S[at-1:]).lower() + S=''.join([i for i in S if i.isdigit()]) + return ["", "+*-", "+**-", "+***-"][len(S) - 10] + "***-***-" + S[-4:] __________________________________________________________________________________________________ - +sample 28 ms submission +import re +class Solution: + def maskPII(self, S): + if '@' in S: + S = S.lower() + res = S.split('@') + return res[0][0] + '*****' + res[0][-1] + '@' + res[1] + else: + S = S.replace('(', '').replace(')', '').replace('-', '').replace('+', '').replace('-', '').replace(' ', '') + if len(S) == 10: + return '***-***-' + S[6:] + else: + return '+' + (len(S) - 10) * '*' + '-***-***-' + S[-4:] + def maskPII1(self, S: str) -> str: + if len(S) >= 2 and re.match(r'[a-zA-Z]+\@[a-zA-Z]+\.[a-zA-Z]+', S): + S = S.lower() + first = S[0] + second = S[S.find('@') - 1] + rest = S[S.find('@'):] + return f'{first}*****{second}{rest}' + else: + S = S.replace('(', '').replace(')', '').replace('-', '').replace('+', '').replace('-', '').replace(' ', '') + if len(S) == 10: + ans = ['***', '***', S[6:]] + return '-'.join(ans) + elif len(S) == 11: + ans = ['+*', '***', '***', S[-4:]] + return '-'.join(ans) + elif len(S) == 12: + ans = ['+**', '***', '***', S[-4:]] + return '-'.join(ans) + elif len(S) == 13: + ans = ['+***', '***', '***', S[-4:]] + return '-'.join(ans) + return S __________________________________________________________________________________________________ diff --git a/Python3/832.py b/Python3/832.py index 433d2ad..b5dea88 100644 --- a/Python3/832.py +++ b/Python3/832.py @@ -1,5 +1,16 @@ __________________________________________________________________________________________________ - +sample 40 ms submission +class Solution: + # def flipAndInvertImage(self, A: List[List[int]]) -> List[List[int]]: + + def flipAndInvertImage(self, A): + return [[1 ^ i for i in row[::-1]] for row in A] __________________________________________________________________________________________________ - +sample 12924 kb submission +class Solution: + def flipAndInvertImage(self, A: List[List[int]]) -> List[List[int]]: + for row in (A): + for i in range((len(row)+1)//2): + row[i], row[~i] = row[~i]^1, row[i]^1 + return A __________________________________________________________________________________________________ diff --git a/Python3/833.py b/Python3/833.py index 433d2ad..8881244 100644 --- a/Python3/833.py +++ b/Python3/833.py @@ -1,5 +1,25 @@ __________________________________________________________________________________________________ - +sample 32 ms submission +class Solution: + def findReplaceString(self, S, indexes, sources, targets): + S = list(S) + for i, src, tgt in sorted(zip(indexes, sources, targets), reverse=True): + if S[i:i+len(src)] == list(src): + S[i:i+len(src)] = list(tgt) + return "".join(S) __________________________________________________________________________________________________ - +sample 36 ms submission +class Solution: + def findReplaceString(self, S: str, indexes: List[int], sources: List[str], targets: List[str]) -> str: + """ + [a, b, ffff, ""] + """ + arr = [c for c in S] + + for i, src, tgt in zip(indexes, sources, targets): + if S[i:i+len(src)] == src: + arr[i] = tgt + for j in range(i+1, i+len(src)): + arr[j] = "" + return "".join(arr) __________________________________________________________________________________________________ diff --git a/Python3/834.py b/Python3/834.py index 433d2ad..c136787 100644 --- a/Python3/834.py +++ b/Python3/834.py @@ -1,5 +1,81 @@ __________________________________________________________________________________________________ - +sample 352 ms submission +class Solution: + def sumOfDistancesInTree(self, N: int, edges: List[List[int]]) -> List[int]: + if not edges: + return [0] + + result = [0] * N + nodes = [set() for _ in range(N)] + counts = [1] * N + + for i in range(len(edges)): + nodes[edges[i][0]].add(edges[i][1]) + nodes[edges[i][1]].add(edges[i][0]) + + result[0] = self._get_counts_and_distances(nodes, 0, counts, None) + self._get_result(nodes, 0, counts, result, None) + + return result + + def _get_counts_and_distances(self, nodes, index, counts, parent): + dis = 0 + count = 0 + for node in nodes[index]: + if node == parent: + continue + + dis += self._get_counts_and_distances(nodes, node, counts, index) + dis += counts[node] + counts[index] += counts[node] + + return dis + + def _get_result(self, nodes, index, counts, ans, parent): + if parent is not None: + ans[index] = ans[parent] - counts[index] + counts[0] - counts[index] + + for node in nodes[index]: + if node == parent: + continue + self._get_result(nodes, node, counts, ans, index) __________________________________________________________________________________________________ - +sample 356 ms submission +class Solution: + def sumOfDistancesInTree(self, N: int, edges: List[List[int]]) -> List[int]: + if not edges: + return [0] + + result = [0] * N + nodes = [set() for _ in range(N)] + counts = [1] * N + + for i in range(len(edges)): + nodes[edges[i][0]].add(edges[i][1]) + nodes[edges[i][1]].add(edges[i][0]) + + result[0] = self._get_counts_and_distances(nodes, 0, counts, None) + self._get_result(nodes, 0, counts, result, None) + + return result + + def _get_counts_and_distances(self, nodes, index, counts, parent): + dis = 0 + for node in nodes[index]: + if node == parent: + continue + dis += self._get_counts_and_distances(nodes, node, counts, index) + dis += counts[node] + counts[index] += counts[node] + + return dis + + def _get_result(self, nodes, index, counts, ans, parent): + if parent is not None: + ans[index] = ans[parent] - counts[index] + counts[0] - counts[index] + + for node in nodes[index]: + if node == parent: + continue + self._get_result(nodes, node, counts, ans, index) __________________________________________________________________________________________________ diff --git a/Python3/835.py b/Python3/835.py index 433d2ad..96c5520 100644 --- a/Python3/835.py +++ b/Python3/835.py @@ -1,5 +1,45 @@ __________________________________________________________________________________________________ - +sample 212 ms submission +import collections +class Solution: + def largestOverlap(self, A: List[List[int]], B: List[List[int]]) -> int: + ''' + 1. map the two dimension coordinates to one dimension coordinate + 2. the same diff between two one-dimension coordinates(one from A, the other from B), means a point with 1 in A can be slided to another point with 1 in B: + (i1, j1), (i2, j2) -> 100i1 + j1, 100i2 + i2 + -> slide(n, m) + (i1 + n, j1 + m), (i2 + n, j2 + m) -> 100i1 + j1 + 100n +m, 100i2 + j2 + 100n +m + diff 100n + m , 100n + m + ''' + n = len(A) + AA, BB = [], [] + for row in range(n): + for col in range(n): + if A[row][col]: + AA.append(100 * row + col) + if B[row][col]: + BB.append(100 * row + col) + cnt = collections.Counter([a - b for a in AA for b in BB] ) + return max(cnt.values() or [0]) __________________________________________________________________________________________________ - +sample 13704 kb submission +class Solution: + def largestOverlap(self, A: List[List[int]], B: List[List[int]]) -> int: + a_ind = [] + b_ind = [] + for i in range(len(A)): + for j in range(len(A[0])): + if A[i][j] == 1: + a_ind.append((i, j)) + if B[i][j] == 1: + b_ind.append((i, j)) + + max_count = 0 + count = collections.defaultdict(int) + for a in a_ind: + for b in b_ind: + vector = (b[0] - a[0], b[1] - a[1]) + count[vector] += 1 + max_count = max(max_count, count[vector]) + return max_count __________________________________________________________________________________________________ diff --git a/Python3/836.py b/Python3/836.py index 433d2ad..6240e26 100644 --- a/Python3/836.py +++ b/Python3/836.py @@ -1,5 +1,30 @@ __________________________________________________________________________________________________ - +sample 20 ms submission +class Solution: + def isRectangleOverlap(self, rec1: List[int], rec2: List[int]) -> bool: + x1=rec1[0] + x2=rec2[0] + y1=rec1[-1] + y2=rec2[-1] + l1=rec1[-1]-rec1[1] + l2=rec2[-1]-rec2[1] + L1=rec1[2]-rec1[0] + L2=rec2[2]-rec2[0] + if (y2>=y1+l2) or (y1>=y2+l1): + return False + elif (x2>=x1+L1) or (x1>=x2+L2): + return False + else: + return True __________________________________________________________________________________________________ - +sample 12884 kb submission +class Solution: + def isRectangleOverlap(self, rec1: List[int], rec2: List[int]) -> bool: + x1,y1,x2,y2 = 0,1,2,3 + return ( + rec1[x1] < rec2[x2] and # rec1 is to left of rec2 + rec1[y1] < rec2[y2] and # rec1 is above rec2 + rec2[x1] < rec1[x2] and # rec2 is to the left of rec1 + rec2[y1] < rec1[y2] # rec2 is above rec1 + ) __________________________________________________________________________________________________ diff --git a/Python3/837.py b/Python3/837.py index 433d2ad..84fb72d 100644 --- a/Python3/837.py +++ b/Python3/837.py @@ -1,5 +1,33 @@ __________________________________________________________________________________________________ - +sample 68 ms submission +class Solution: + def new21Game(self, N, K, W): + """ + :type N: int + :type K: int + :type W: int + :rtype: float + """ + d=[0]*(K+max(W,N)+10) + for i in range(K,N+1): + d[i]=1 + ms=N-K+1 + for i in range(K-1,-1,-1): + d[i]=ms/W + ms+=d[i]-d[i+W] + return d[0] __________________________________________________________________________________________________ - +sample 72 ms submission +class Solution: + def new21Game(self, N: int, K: int, W: int) -> float: + # dp[x] = the answer when Alice has x points + dp = [0]*(N+1+W) + for i in range(K, N+1): + dp[i] = 1.0 + + S = min(N-K+1, W) + for i in range(K-1, -1, -1): + dp[i] = S/W + S += dp[i] - dp[i+W] + return dp[0] __________________________________________________________________________________________________ diff --git a/Python3/838.py b/Python3/838.py index 433d2ad..f6d1faa 100644 --- a/Python3/838.py +++ b/Python3/838.py @@ -1,5 +1,56 @@ __________________________________________________________________________________________________ - +sample 84 ms submission +class Solution: + def pushDominoes(self, dominoes: str) -> str: + res = '' + count = 0 + for d in dominoes: + if d == '.': + count += 1 + elif not res or res[-1] == 'L': + if d == 'L': + res += 'L' * (count + 1) + else: + res += '.' * count + 'R' + count = 0 + else: + if d == 'R': + res += 'R' * (count + 1) + else: + res += ('R' * (count // 2) + + ('.' if count & 1 else '') + + 'L' * (count // 2 + 1)) + count = 0 + if count > 0: + if not res or res[-1] == 'L': + res += '.' * count + elif res[-1] == 'R': + res += 'R' * count + return res __________________________________________________________________________________________________ - +sample 88 ms submission +class Solution: + def pushDominoes(self, dominoes: str) -> str: + ans, cnt, toRight = '', 0, False + for c in dominoes: + if c == 'L': + if toRight: + ans += 'R'*(cnt//2) + '.'*(cnt-cnt//2*2) + 'L'*(cnt//2+1) + else: + ans += 'L'*(cnt+1) + cnt, toRight = 0, False + elif c == 'R': + if toRight: + ans += 'R'*(cnt+1) + else: + ans += '.'*cnt + 'R' + cnt, toRight = 0, True + else: + cnt += 1 + if cnt > 0: + if toRight: + ans += 'R'*cnt + else: + ans += '.'*cnt + return ans __________________________________________________________________________________________________ diff --git a/Python3/839.py b/Python3/839.py index 433d2ad..d9f2fc6 100644 --- a/Python3/839.py +++ b/Python3/839.py @@ -1,5 +1,89 @@ __________________________________________________________________________________________________ - +sample 1596 ms submission +class Solution: + def numSimilarGroups(self, A: 'List[str]') -> int: + A = list(set(A)) +# la = len(A) +# label = [i for i in range(la)] +# dic = {i:[i] for i in range(la)} + label = {a:i for i, a in enumerate(A)} + dic = {i:[a] for i, a in enumerate(A)} + def union(nowi, nowj): # take labels (found!) as input + for p in dic[nowj]: label[p] = nowi + dic[nowi].extend(dic[nowj]) + dic.pop(nowj) + def similar(s, t): + hasDif = False + for cs, ct in zip(s, t): # check if similar + if cs==ct: continue + if hasDif: + if temp and temp == (cs, ct): + temp = None + else: return False + else: + hasDif = True + temp = (ct, cs) + return True + + if len(A[0])**2 > len(A): # few words (sparse) + for i, s in enumerate(A): + nowi = label[s] + for t in A[i+1:]: + nowj = label[t] + if nowj == nowi: continue + if similar(s, t): + union(nowi, nowj) + if len(dic) == 1: return 1 + else: # short word (dense) + lw = len(A[0]) + for a in A: + nowa = label[a] + for j in range(1, lw): + for i in range(j): + nbhd = f'{a[:i]}{a[j]}{a[i+1:j]}{a[i]}{a[j+1:]}' +# print(nbhd) + if nbhd in label and nowa != label[nbhd]: + union(nowa, label[nbhd]) + return len(dic) __________________________________________________________________________________________________ - +sample 45776 kb submission +global it +it=2 +import time +time.clock() +def tic(): + print(time.clock()) + +class Solution: + def numSimilarGroups(self, A): + """ + :type A: List[str] + :rtype: int + """ + global it + it-=1 + simlen=len(A[0])-2 + # O(A*L) + Adic = collections.defaultdict(list) + sim = [[0]*len(A) for _ in range(len(A) )] + for i in range(len(A) ): + for c in enumerate(A[i]): + for j in Adic[c]: + sim[i][j]+=1 + Adic[c].append(i) + for i in range(len(A)): + for j in range(i): + sim[j][i]=sim[i][j] + # O(L^2) + DFS = set(range(len(A))) + count=0 + while(DFS): + count+=1 + group = [DFS.pop()] + for i in group: + for j in [n for n in DFS if sim[i][n]>=simlen]: + DFS.remove(j) + group.append(j) + if not it:print(group) + return count __________________________________________________________________________________________________ diff --git a/Python3/840.py b/Python3/840.py index 433d2ad..d8c80c2 100644 --- a/Python3/840.py +++ b/Python3/840.py @@ -1,5 +1,38 @@ __________________________________________________________________________________________________ - +sample 28 ms submission +class Solution: + def numMagicSquaresInside(self, grid: List[List[int]]) -> int: + r, c = len(grid), len(grid[0]) + ret = 0 + if r < 3 or c < 3: + return ret + for i in range(1, r - 1): + for j in range(1, c - 1): + if grid[i][j] != 5: + continue + if self.magic(grid[i - 1][j - 1], grid[i - 1][j], grid[i - 1][j + 1], + grid[i][j - 1], grid[i][j], grid[i][j + 1], + grid[i + 1][j - 1], grid[i + 1][j], grid[i + 1][j + 1]): + ret += 1 + return ret + + def magic(self, a, b, c, d, e, f, g, h, i): + return (set([a, b, c, d, e, f, g, h, i]) == set(range(1, 10)) and + (a + b + c == g + h + i == a + d + g == c + f + i == 15) and + (a + i == c + g == d + f == b + h == 10)) __________________________________________________________________________________________________ - +sample 12912 kb submission +class Solution: + def numMagicSquaresInside(self, grid: List[List[int]]) -> int: + def helper(x,y): + memo = set([grid[x][y],grid[x + 1][y],grid[x + 2][y],grid[x][y + 1],grid[x + 1][y + 1],grid[x + 2][y + 1],grid[x][y + 2],grid[x + 1][y + 2],grid[x + 2][y + 2]]) + for i in range(1,10): + if i not in memo: + return False + return grid[x][y] + grid[x][y + 1] + grid[x][y + 2] == grid[x + 1][y] + grid[x + 1][y + 1] + grid[x + 1][y + 2] == grid[x + 2][y] + grid[x + 2][y + 1] + grid[x + 2][y + 2] and grid[x][y] + grid[x + 1][y] + grid[x + 2][y] == grid[x][y + 1] + grid[x + 1][y + 1] + grid[x + 2][y + 1] == grid[x][y + 2] + grid[x + 1][y + 2] + grid[x + 2][y + 2] and grid[x][y] + grid[x + 2][y + 2] == grid[x + 2][y] + grid[x][y + 2] + ans = 0 + for i in range(len(grid) - 2): + for j in range(len(grid[0]) - 2): + ans += helper(i,j) + return ans __________________________________________________________________________________________________ diff --git a/Python3/841.py b/Python3/841.py index 433d2ad..2dd0927 100644 --- a/Python3/841.py +++ b/Python3/841.py @@ -1,5 +1,25 @@ __________________________________________________________________________________________________ - +sample 60 ms submission +class Solution: + def canVisitAllRooms(self, rooms: List[List[int]]) -> bool: + q, seen = [0], {0} + for node in q: + for nei in rooms[node]: + if nei not in seen: + q.append(nei) + seen.add(nei) + return len(seen) == len(rooms) __________________________________________________________________________________________________ - +sample 13296 kb submission +class Solution: + def canVisitAllRooms(self, rooms: List[List[int]]) -> bool: + st = [0] + entered = set([0]) + while st: + c = st.pop(-1) + for k in rooms[c]: + if k not in entered: + st.append(k) + entered.add(k) + return len(entered) == len(rooms) __________________________________________________________________________________________________ diff --git a/Python3/842.py b/Python3/842.py index 433d2ad..11252da 100644 --- a/Python3/842.py +++ b/Python3/842.py @@ -1,5 +1,61 @@ __________________________________________________________________________________________________ - +sample 28 ms submission +class Solution: + def splitIntoFibonacci(self, S: str) -> List[int]: + for i in range(min(10,len(S))): + x=S[:i+1] + if x!= '0' and x.startswith('0'): + break + a=int(x) + for j in range(i+1,min(i+10,len(S))): + y=S[i+1:j+1] + if y != '0' and y.startswith('0'): + break + b=int(y) + fib=[a,b] + k=j+1 + while k=3: + return fib + return [] __________________________________________________________________________________________________ - +sample 13156 kb submission +class Solution: + def splitIntoFibonacci(self, S: str) -> List[int]: + """ + We choose the starting two numbers (n^2), then iteratively continue the + pattern (n). + Time: O(n^3) + """ + def _fib_seq(num1, num2, remainder): + fib = [num1, num2] + while remainder: + sums = num1 + num2 + cut = len(str(sums)) + num3, remainder = int(remainder[:cut]), remainder[cut:] + if sums != num3 or sums >= 2**31: + return [] + fib.append(num3) + num1, num2 = num2, num3 + return fib + for i in range(2, len(S) - 1): + nums, remainder = S[:i], S[i:] + for cut in range(1, len(nums)): + num1, num2 = nums[:cut], nums[cut:] + if (len(num1) > 1 and num1.startswith('0')) or \ + (len(num2) > 1 and num2.startswith('0')): + continue + num1, num2 = int(num1), int(num2) + fib = _fib_seq(num1, num2, remainder) + if fib: + return fib + return [] __________________________________________________________________________________________________ diff --git a/Python3/843.py b/Python3/843.py index 433d2ad..799aefa 100644 --- a/Python3/843.py +++ b/Python3/843.py @@ -1,5 +1,90 @@ __________________________________________________________________________________________________ +sample 20 ms submission +# """ +# This is Master's API interface. +# You should not implement it, or speculate about its implementation +# """ +#class Master: +# def guess(self, word): +# """ +# :type word: str +# :rtype int +# """ +class Solution: + def findSecretWord(self, wordlist, master): + """ + :type wordlist: List[Str] + :type master: Master + :rtype: None + """ + import random + + new_wordlist = wordlist + while True: + test_word = random.choice(new_wordlist) + matches = master.guess(test_word) + if matches == 6: + return + + wordlist = new_wordlist + new_wordlist = [] + for word in wordlist: + dist = 0 + for i in range(6): + if word[i] == test_word[i]: + dist += 1 + + if matches == dist: + new_wordlist.append(word) __________________________________________________________________________________________________ +sample 13052 kb submission +# """ +# This is Master's API interface. +# You should not implement it, or speculate about its implementation +# """ +#class Master: +# def guess(self, word): +# """ +# :type word: str +# :rtype int +# """ + +class Solution: + def findSecretWord(self, wordlist, master): + """ + :type wordlist: List[Str] + :type master: Master + :rtype: None + """ + idx_q = collections.deque() + + distance = master.guess(wordlist[0]) + + def cal_distance(w1, w2: str) -> int: + result = 0 + for idx in range(len(w1)): + if w1[idx] == w2[idx]: + result += 1 + return result + + for idx in range(1, len(wordlist)): + if cal_distance(wordlist[idx], wordlist[0]) == distance: + idx_q.append(idx) + while True: + if len(idx_q) == 0: + return + word = wordlist[idx_q.popleft()] + distance = master.guess(word) + if distance == 6: + return + new_q = collections.deque() + while len(idx_q) > 0: + idx = idx_q.pop() + if cal_distance(wordlist[idx], word) == distance: + new_q.append(idx) + idx_q = new_q + + return __________________________________________________________________________________________________ diff --git a/Python3/844.py b/Python3/844.py index 433d2ad..e72b31c 100644 --- a/Python3/844.py +++ b/Python3/844.py @@ -1,5 +1,50 @@ __________________________________________________________________________________________________ +sample 20 ms submission +class Solution: + def backspaceCompare(self, S: str, T: str) -> bool: + to_compare, second = "", "" + for c in S: + if c == '#': + if to_compare: + to_compare = to_compare[:-1] + else: + to_compare += c + for c in T: + if c == '#': + if second: + second = second[:-1] + else: + second += c + + return second == to_compare __________________________________________________________________________________________________ +sample 13024 kb submission +class Solution: + def backspaceCompare(self, S: str, T: str) -> bool: + w = 0 + S = list(S) + for i in range(len(S)): + if S[i] == "#": + w = w - 1 if w > 1 else 0 + else: + S[w] = S[i] + w += 1 + + while w < len(S): + S.pop() + + w = 0 + T = list(T) + for i in range(len(T)): + if T[i] == "#": + w = w - 1 if w > 1 else 0 + else: + T[w] = T[i] + w += 1 + + while w < len(T): + T.pop() + return S == T __________________________________________________________________________________________________ diff --git a/Python3/845.py b/Python3/845.py index 433d2ad..3036ee6 100644 --- a/Python3/845.py +++ b/Python3/845.py @@ -1,5 +1,95 @@ __________________________________________________________________________________________________ +sample 176 ms submission +class Solution: + def longestMountain(self, A: List[int]) -> int: + if len(A) <= 2: + return 0 + + is_asc = True + has_asc = False + has_desc = False + prev = A[0] + max_len = 0 + length = 0 + + for cur in A[1:]: + # "Downhill" + if prev > cur: + # Reached the peak, going down + if is_asc and has_asc: + has_desc = True + is_asc = False + has_asc = False + length += 1 + # Rolling down the mountain + if not is_asc: + length += 1 + + # "Uphill" + if cur > prev: + # First step in mountain + if is_asc: + has_asc = True + length += 1 + # previously downhill, encountered another uphill + else: + max_len = max(length, max_len) + length = 1 + is_asc = True + has_asc = True + + if cur == prev: + if not is_asc: + max_len = max(length, max_len) + length = 0 + is_asc = True + has_asc = False + + prev = cur + + if not has_desc: + return 0 + + return max(max_len, length) __________________________________________________________________________________________________ - +sample 180 ms submission +class Solution: + def longestMountain(self, A: List[int]) -> int: + if len(A) < 3: + return 0 + + ans = 0 + length = 1 + temp = A[0] + climbUp = temp < A[1] + print(temp, A[1], climbUp, length) + for x in A[1:]: + if climbUp: + length += 1 + if not x > temp: + climbUp = False + if x == temp: + length = 1 + elif length == 1: + if x > temp: + climbUp = True + length += 1 + else: + if x < temp: + length += 1 + else: + ans = max(length, ans) + length = 1 + if x > temp: + climbUp = True + length += 1 + + # print(temp, x, climbUp, length) + temp = x + + if length > ans and not climbUp: + ans = length + + return ans if ans > 2 else 0 __________________________________________________________________________________________________ diff --git a/Python3/846.py b/Python3/846.py index 433d2ad..0e74567 100644 --- a/Python3/846.py +++ b/Python3/846.py @@ -1,5 +1,33 @@ __________________________________________________________________________________________________ +sample 180 ms submission +class Solution: + """ + T: O() + S: O() + """ + def isNStraightHand(self, hand: List[int], k: int) -> bool: + if not k or k < 0: + return True + if k == 1: + return bool(hand) + if len(hand) % k: + return False + cnts = collections.Counter(x % k for x in hand) + return len(set(cnts.values())) == 1 and len(cnts) == k __________________________________________________________________________________________________ - +sample 14428 kb submission +class Solution: + def isNStraightHand(self, hand: List[int], W: int) -> bool: + count = collections.Counter(hand) + while count: + m = min(count) + for card in range(m, m+W): + if count[card] == 0: + return False + elif count[card] == 1: + del count[card] + else: + count[card] -= 1 + return True __________________________________________________________________________________________________ diff --git a/Python3/847.py b/Python3/847.py index 433d2ad..21f486c 100644 --- a/Python3/847.py +++ b/Python3/847.py @@ -1,5 +1,43 @@ __________________________________________________________________________________________________ +sample 104 ms submission +import heapq +class Solution: + def shortestPathLength(self, graph: List[List[int]]) -> int: + + # solution 2 + n = len(graph) + goal = (1 << n) - 1 + curs = [] + past = set() + for i in range(n): + curs.append([0, i, 1< int: + memo, final, q, steps = set(), (1 << len(graph)) - 1, [(i, 1 << i) for i in range(len(graph))], 0 + while True: + new = [] + for node, state in q: + if state == final: return steps + for v in graph[node]: + if (state | 1 << v, v) not in memo: + new.append((v, state | 1 << v)) + memo.add((state | 1 << v, v)) + q = new + steps += 1 __________________________________________________________________________________________________ diff --git a/Python3/848.py b/Python3/848.py index 433d2ad..9425958 100644 --- a/Python3/848.py +++ b/Python3/848.py @@ -1,5 +1,25 @@ __________________________________________________________________________________________________ +sample 196 ms submission +class Solution: + def shiftingLetters(self, S: str, shifts: List[int]) -> str: + ans = [] + X = sum(shifts) % 26 + for i, c in enumerate(S): + index = ord(c) - ord('a') + ans.append(chr(ord('a') + (index + X) % 26)) + X = (X - shifts[i]) % 26 + return "".join(ans) __________________________________________________________________________________________________ - +sample 15224 kb submission +class Solution: + def shiftingLetters(self, S: str, shifts: List[int]) -> str: + tot_shift, S = 0, list(S) + for i, shift, char in zip(range(len(S) - 1, -1, -1), + reversed(shifts), reversed(S)): + tot_shift += shift + char = ord(char) - ord('a') + char = (char + tot_shift) % 26 + S[i] = chr(char + ord('a')) + return "".join(S) __________________________________________________________________________________________________ diff --git a/Python3/849.py b/Python3/849.py index 433d2ad..7e4d61d 100644 --- a/Python3/849.py +++ b/Python3/849.py @@ -1,5 +1,43 @@ __________________________________________________________________________________________________ - +sample 128 ms submission +class Solution: + def maxDistToClosest(self, seats: List[int]) -> int: + right, left = 0, 0 + while seats[left]==0: + left+=1 + idx = len(seats)-1 + while seats[idx]==0: + idx -= 1 + right +=1 + + max_, zero = 0, 0 + + for i in seats: + if i == 0: + zero += 1 + if zero > max_: + max_ = zero + else: + zero = 0 + return max([left, right, int(max_/2+0.5)]) __________________________________________________________________________________________________ - +sample 13144 kb submission +class Solution: + def maxDistToClosest(self, seats: List[int]) -> int: + first, last, prev = -1, -1, -1 + ans = 0 + for i in range(len(seats)): + if seats[i] != 1: + continue + + if prev != -1: + ans = max(ans, (i - prev) // 2) + elif first == -1: + first = i + if last < i: + last = i + + prev = i + + return max(ans, max(first, len(seats) - 1 - last)) __________________________________________________________________________________________________ diff --git a/Python3/850.py b/Python3/850.py index 433d2ad..86b17a4 100644 --- a/Python3/850.py +++ b/Python3/850.py @@ -1,5 +1,81 @@ __________________________________________________________________________________________________ - +sample 40 ms submission +class Solution: + def rectangleArea(self, rectangles: List[List[int]]) -> int: + def getSum(intervals): + ''' + intervals are sorted by start, may have overlaps, find the sum of total covered area + [[1, 3],[2, 3]] = 3 + ''' + tot = 0 + end = -math.inf + for a, b in intervals: + if b<=end: + continue + + tot += b- max(end, a) + end = b + return tot + + ENTER = 0 + EXIT = 1 + + lines = [] + for x1, y1, x2, y2 in rectangles: + lines.append((x1, y1, y2, ENTER)) + lines.append((x2, y1, y2, EXIT)) + + lines.sort(key=lambda x: (x[0], -x[3])) + + tot = 0 + prevx = 0 + activeLines = [] + for line in lines: + ysum = getSum(activeLines) + tot += (line[0] - prevx) * ysum + + prevx = line[0] + + if line[3]==ENTER: + activeLines.append([line[1], line[2]]) + activeLines.sort() + + else: + activeLines.remove([line[1], line[2]]) + + return tot%(10**9 + 7) __________________________________________________________________________________________________ - +sample 48 ms submission +class Solution: + def rectangleArea(self, rectangles: List[List[int]]) -> int: + ans = 0 + + if not rectangles: + return ans + + MOD = int(1e9 + 7) + lines = sorted(x for x1, y1, x2, y2 in rectangles for x in [[x1, y1, y2, True], [x2, y1, y2, False]]) + segs = [(float('inf'),) * 2] + x1 = lines[0][0] + + for x2, y1, y2, is_start in lines: + h = lo = hi = 0 + + for h1, h2 in segs: + if h1 > hi: + h += hi - lo + lo, hi = h1, h2 + else: + hi = max(hi, h2) + + ans = (ans + (x2 - x1) * h) % MOD + + if is_start: + bisect.insort(segs, (y1, y2)) + else: + segs.remove((y1, y2)) + + x1 = x2 + + return ans __________________________________________________________________________________________________ diff --git a/Python3/851.py b/Python3/851.py index 433d2ad..94dd8e5 100644 --- a/Python3/851.py +++ b/Python3/851.py @@ -1,5 +1,61 @@ __________________________________________________________________________________________________ +sample 496 ms submission +class Solution: + def loudAndRich(self, richer: List[List[int]], quiet: List[int]) -> List[int]: + + # construct an adjacency list where out[x] = [y_1, y_2, .., y_n] means x is richer than them + out = dict() + for edge in richer: + if edge[0] not in out: + out[edge[0]] = [edge[1]] + else: + out[edge[0]].append(edge[1]) + + # starting from quietest person, to least quiet person + n = len(quiet) + + quietper = [ (quiet[i], i) for i in range(n) ] + quietper = sorted(quietper, key = lambda elem: elem[0]) + + ans = [-1 for i in range(n) ] + + for _, per in quietper: + q = [per] + + while len(q)>0: + i = q.pop() + + # if ans[i] not updated, update ans[i], and put all its neighbors into queue, + # otherwise skip this step + if ans[i] == -1: + ans[i] = per + if i in out: + for neigh in out[i]: + if ans[neigh] == -1: + q.append(neigh) + + + return ans __________________________________________________________________________________________________ - +sample 504 ms submission +class Solution: + def loudAndRich(self, richer: List[List[int]], quiet: List[int]) -> List[int]: + dic = collections.defaultdict(list) + for i in richer: + dic[i[1]].append(i[0]) + self.res=[-1 for i in range(len(quiet))] + def helper(i): + if not dic[i]: return i + cur = i + for j in dic[i]: + if self.res[j]==-1: + self.res[j] = helper(j) + if quiet[self.res[j]] < quiet[cur]: + cur=self.res[j] + return cur + + for i in range(len(self.res)): + self.res[i] = helper(i) + return self.res __________________________________________________________________________________________________ diff --git a/Python3/852.py b/Python3/852.py index 433d2ad..a2ad134 100644 --- a/Python3/852.py +++ b/Python3/852.py @@ -1,5 +1,24 @@ __________________________________________________________________________________________________ - +sample 72 ms submission +class Solution: + def peakIndexInMountainArray(self, A: List[int]) -> int: + left, right = 0, len(A) - 1 + while left < right: + mid = (left + right) // 2 + if A[mid - 1] < A[mid] and A[mid] < A[mid + 1]: + left = mid + elif A[mid - 1] > A[mid] and A[mid] > A[mid + 1]: + right = mid + else: + break + return mid __________________________________________________________________________________________________ - +sample 14000 kb submission +class Solution: + def peakIndexInMountainArray(self, A: List[int]) -> int: + i = 1 + while i < len(A): + if A[i] < A[i - 1]: + return i - 1 + i += 1 __________________________________________________________________________________________________ diff --git a/Python3/853.py b/Python3/853.py index 433d2ad..a8c44d6 100644 --- a/Python3/853.py +++ b/Python3/853.py @@ -1,5 +1,33 @@ __________________________________________________________________________________________________ - +sample 184 ms submission +class Solution: + def carFleet(self, target: int, position: List[int], speed: List[int]) -> int: + t = 0 + res = 0 + hm = {} + for i in range(len(position)): + hm[position[i]] = speed[i] + position.sort() + for i in range(len(position)-1, -1, -1): + if t == 0: + t = (target - position[i] ) / hm[position[i]] + res = 1 + continue + tt = (target - position[i] ) / hm[position[i]] + if tt > t: + t = tt + res += 1 + return res __________________________________________________________________________________________________ - +sample 188 ms submission +class Solution: + def carFleet(self, target: int, position: List[int], speed: List[int]) -> int: + cars = sorted(zip(position, speed), key=lambda x:-x[0]) + ans, slowest = 0, -1 + for p, s in cars: + t = (target - p) / s + if t > slowest: + slowest = t + ans += 1 + return ans __________________________________________________________________________________________________ diff --git a/Python3/854.py b/Python3/854.py index 433d2ad..1e83345 100644 --- a/Python3/854.py +++ b/Python3/854.py @@ -1,5 +1,61 @@ __________________________________________________________________________________________________ +sample 64 ms submission +import itertools -__________________________________________________________________________________________________ +class Solution: + def kSimilarity(self, A: str, B: str) -> int: + # check how many circles + # it is better to do a BFS + n, b = len(A), list(B) + min_step = float('inf') + + def backtracking(step, i): + nonlocal min_step + if step >= min_step: # exceed min_step, early stop + return + + while i < n and A[i] == b[i]: + i += 1 + + if i == n: + min_step = min(step, min_step) + return + pos = [] + for j in range(i + 1, n): + if b[j] == A[i]: + # should be the optimal swap, match two pairs in one swap, + # even if have multiple A[j]==b[i], we can just pick any one + if A[j] == b[i]: + pos = [j] + break + elif A[j] != b[j]: # if A[j] == b[j], this swap also remain one mis-match, exclude this situation + pos.append(j) + + for k in pos: + b[i], b[k] = b[k], b[i] + backtracking(step + 1, i) + b[i], b[k] = b[k], b[i] + + backtracking(0, 0) + return min_step +__________________________________________________________________________________________________ +sample 96 ms submission +class Solution(object): + def kSimilarity(self, A, B): + if A == B: return 0 + dq, seen, step, n = collections.deque([A]), {A}, 0, len(A) + while dq: + sz = len(dq) + for _ in range(sz): + cur, i = dq.popleft(), 0 + while i < n and cur[i] == B[i]: i += 1 + for j in range(i + 1, n): + if B[j] != cur[i] or cur[j] == B[j]: continue + nxt = cur[ : i] + cur[j] + cur[i + 1: j] + cur[i] + cur[j + 1: ] + if nxt not in seen: + seen.add(nxt) + if nxt == B: return step + 1 + dq.append(nxt) + step += 1 __________________________________________________________________________________________________ diff --git a/Python3/855.py b/Python3/855.py index 433d2ad..9f5461a 100644 --- a/Python3/855.py +++ b/Python3/855.py @@ -1,5 +1,100 @@ __________________________________________________________________________________________________ +sample 52 ms submission +class ExamRoom: + def __init__(self, N: int): + # intuition, need to track seats and pop the furthest slot up + # need to be clear about distance calculation + # [1,2,3,4,5], pick 3, distance is 2 + # [1,2,3,4], pick 2, distance is 1 + # formula is dist = (j-i)//2, pick = i+(j-i)//2 + #self.seats = [False]*N + self.slots = [(1-N, 0, N-1)] + self.total = N + + + def seat(self) -> int: + #if not self.slots: return -1 + n, beg, end = heapq.heappop(self.slots) + res =beg+ (end-beg)//2 + if beg==0: + res = beg + elif end==self.total-1: + res = end + # we don't care about the special rule? since here we only have 0/N-1 taken + # we need to care about special spacing rule when 0/N-1 leaves + if res>beg: heapq.heappush(self.slots, (-((res-1-beg)//2), beg, res-1)) + if res None: + i = 0 + beg, end = p, p + while i dist: + dist, student = d, prev + d + + # Considering the right-most seat. + d = self.N - 1 - self.students[-1] + if d > dist: + student = self.N - 1 + + # Add the student to our sorted list of positions. + bisect.insort(self.students, student) + return student + + def leave(self, p): + #if p in self.students: + self.students.remove(p) + +# Your ExamRoom object will be instantiated and called as such: +# obj = ExamRoom(N) +# param_1 = obj.seat() +# obj.leave(p) __________________________________________________________________________________________________ diff --git a/Python3/856.py b/Python3/856.py index 433d2ad..6a8c152 100644 --- a/Python3/856.py +++ b/Python3/856.py @@ -1,5 +1,31 @@ __________________________________________________________________________________________________ - +sample 20 ms submission +class Solution: + def scoreOfParentheses(self, S: str) -> int: + bal = ans = 0 + for i,x in enumerate(S): + if x == '(': + bal += 1 + else: + bal -= 1 + if S[i-1] == '(': + ans += 1 << bal + return ans __________________________________________________________________________________________________ +sample 12912 kb submission +class Solution: + def scoreOfParentheses(self, S: str) -> int: + + # from solution + ans = 0 + bal = 0 + for i, c in enumerate(S): + if c == "(": + bal += 1 + else: + bal -= 1 + if S[i-1] == '(': + ans += pow(2, bal) + return ans __________________________________________________________________________________________________ diff --git a/Python3/857.py b/Python3/857.py index 433d2ad..fc56a47 100644 --- a/Python3/857.py +++ b/Python3/857.py @@ -1,5 +1,55 @@ __________________________________________________________________________________________________ +sample 208 ms submission +from heapq import heapify, heappushpop +class Solution: + def mincostToHireWorkers( + self, + quality: List[int], + wage: List[int], + K: int + ) -> float: + workers = sorted(zip(quality, wage), key=lambda x: x[1]/x[0]) + quality_heap = [-w[0] for w in workers[:K]] + heapify(quality_heap) + + quality_sum = -sum(quality_heap) + cost = quality_sum / workers[K-1][0] * workers[K-1][1] + min_cost = cost + + for k in range(K, len(quality)): + if workers[k][0] + quality_heap[0] < 0: + quality_sum += heappushpop(quality_heap, -workers[k][0]) + workers[k][0] + cost = quality_sum / workers[k][0] * workers[k][1] + min_cost = min(cost, min_cost) + return min_cost __________________________________________________________________________________________________ +sample 15140 kb submission +import heapq +class Solution: + def mincostToHireWorkers(self, quality: List[int], wage: List[int], K: int) -> float: + n = len(quality) + rate = [] + for q, w in zip(quality, wage): + rate.append(w/q) + + rateindex = sorted(range(n), key = lambda i: rate[i]) + + # use max heap to store the min K quality person + pq = minKQuality = [] + for i in range(K): + pq.append( -quality[rateindex[i]] ) # use - to change min heap to max heap + heapq.heapify(pq) + + minQuality = sum(pq) * (-1) + minWage = rate[rateindex[K-1]] * minQuality + for i in range(K, n): + index = rateindex[i] + currate = rate[index] + outquality = - heapq.heappushpop(pq, -quality[index]) + minQuality += (quality[index] - outquality) + minWage = min(minWage, minQuality * currate ) + + return minWage __________________________________________________________________________________________________ diff --git a/Python3/858.py b/Python3/858.py index 433d2ad..09c0d61 100644 --- a/Python3/858.py +++ b/Python3/858.py @@ -1,5 +1,30 @@ __________________________________________________________________________________________________ - +sample 24 ms submission +class Solution: + def gcd(self,n1,n2): + return self.gcd(n2,n1%n2) if n2 != 0 else n1 + def mirrorReflection(self, p: int, q: int) -> int: + m = self.gcd(max(p,q),min(p,q)) + n1 = p // m + n2 = q// m + if n2 % 2 == 0: + return 0 + else: + if n1 % 2 == 0: + return 2 + else: + return 1 __________________________________________________________________________________________________ - +sample 12980 kb submission +class Solution: + def mirrorReflection(self, p: int, q: int) -> int: + + tot = q + while tot % p != 0: + tot += q + + if (tot // q) % 2 == 0: + return 2 + else: + return (tot // p) % 2 __________________________________________________________________________________________________ diff --git a/Python3/859.py b/Python3/859.py index 433d2ad..4a3d8a6 100644 --- a/Python3/859.py +++ b/Python3/859.py @@ -1,5 +1,29 @@ __________________________________________________________________________________________________ - +sample 24 ms submission +class Solution: + def buddyStrings(self, A: str, B: str) -> bool: + if len(A) != len(B): return False + if A == B and len(set(A)) < len(A): return True + diff = [(a, b) for a, b in zip(A, B) if a != b] + return len(diff) == 2 and diff[0] == diff[1][::-1] __________________________________________________________________________________________________ - +sample 13080 kb submission +class Solution(object): + def buddyStrings(self, A, B): + """ + :type A: str + :type B: str + :rtype: bool + """ + if len(A) != len(B): return False + + # 兩個字串相等, 且字串中有重複的字元(swap後字串不變) + if A == B and len(set(A)) < len(A): return True + + # ["abcd", "badc"], #false + # dif = [('a', 'b'), ('b', 'a'), ('c', 'd'), ('d', 'c')] + # 唯一一組, 且swap後吻合 -> True + dif = [(a, b) for a, b in zip(A, B) if a != b] + #print(dif) + return len(dif) == 2 and dif[0] == dif[1][::-1] __________________________________________________________________________________________________ diff --git a/Python3/860.py b/Python3/860.py index 433d2ad..26f8b28 100644 --- a/Python3/860.py +++ b/Python3/860.py @@ -1,5 +1,46 @@ __________________________________________________________________________________________________ - +sample 144 ms submission +class Solution: + def lemonadeChange(self, bills: List[int]) -> bool: + + num5 = 0 + num10 = 0 + num20 = 0 + if not bills[0]==5: + return False + for b in bills: + if b==5: + num5+=1 + elif b==10: + if num5==0: + return False + num5-=1 + num10+=1 + elif b==20: + if num5>=1 and num10>=1: + num5-=1 + num10-=1 + num20+=1 + elif num5>=3 and num10==0: + num5-=3 + num20+=1 + else: + return False + return True __________________________________________________________________________________________________ - +sample 13264 kb submission +class Solution: + def lemonadeChange(self, bills: List[int]) -> bool: + drawer = { 5: 0, 10: 0} + for bill in bills: + if bill != 20: drawer[bill] += 1 + bill -= 5 + while drawer[10] > 0 and bill >= 10: + bill -= 10 + drawer[10] -= 1 + while drawer[5] > 0 and bill >= 5: + bill -= 5 + drawer[5] -= 1 + if bill != 0: return False + return True __________________________________________________________________________________________________ diff --git a/Python3/861.py b/Python3/861.py index 433d2ad..1b95a86 100644 --- a/Python3/861.py +++ b/Python3/861.py @@ -1,5 +1,48 @@ __________________________________________________________________________________________________ - +sample 36 ms submission +class Solution: + def matrixScore(self, A: List[List[int]]) -> int: + n = len(A) + for i in range(n): + if A[i][0] == 0: + A[i] = [1 - x for x in A[i]] + result = 0 + for i in map(sum, zip(*A)): + result = result * 2 + max(i, n - i) + return result __________________________________________________________________________________________________ +sample 12980 kb submission + +class Solution: + def matrixScore(self, A: List[List[int]]) -> int: + R, C = len(A), len(A[0]) + + # ensure 1st item in each row is 1 + for i in range(R): + if A[i][0] == 0: + for j in range(C): + A[i][j] = 1 - A[i][j] + + for i in range(C): + tmp = 0 + for j in range(R): + if A[j][i] == 0: + tmp += 1 + + if tmp * 2 > R: + for k in range(R): + A[k][i] = 1 - A[k][i] + + return self._matrixToScore(A) + + def _matrixToScore(self, A): + R, C = len(A), len(A[0]) + score = 0 + for i in range(R): + tmp = 0 + for j in range(C): + tmp = (tmp << 1) | A[i][j] + score += tmp + return score __________________________________________________________________________________________________ diff --git a/Python3/862.py b/Python3/862.py index 433d2ad..090123d 100644 --- a/Python3/862.py +++ b/Python3/862.py @@ -1,5 +1,100 @@ __________________________________________________________________________________________________ - +sample 856 ms submission +class Solution: + def shortestSubarray(self, nums: List[int], k: int) -> int: + NOT_FOUND = -1 + + if not nums: + return NOT_FOUND + + n = len(nums) + ans = n + 1 + total = 0 + queue = collections.deque([(-1, 0)]) + + for i, x in enumerate(nums): + total += x + + if x > 0: + while queue and total - queue[0][1] >= k: + ans = min(ans, i - queue.popleft()[0]) + else: + while queue and total <= queue[-1][1]: + queue.pop() + + queue.append((i, total)) + + return ans if ans <= n else NOT_FOUND __________________________________________________________________________________________________ +sample 17160 kb submission +class Solution: + + def shortestSubarray(self, A: List[int], K: int) -> int: + if len(A) ==0: + return -1 + + begin=0 + end=0 + # start with a non-negative number + while A[begin]<=0 and begin= K: + found=True + + # we know the first element won't be negative + # if the element is negative try and spread it's value to previous elements + if A[end]<0: + if begin == end: + begin+=1 + c_sum=0 + else: + i=1 + to_save = abs(A[end]) + while to_save > 0: + while A[end-i] ==0 and end-i>=begin: + i+=1 + if end-i A[end-i]: + to_save -=A[end-i] + A[end-i]=0 + else: + A[end-i] -= to_save + A[end]=0 + to_save=0 + + else: + # while the element you are adding compensates + # for moving the begin by one to the right and makes/keeps sum >= K + while A[end] >= A[begin] + K-(c_sum-A[end]): + c_sum-=A[begin] + if c_sum =K: + size = min(size,end-begin+1) + + end +=1 + + if found: + return size + else: + return -1 __________________________________________________________________________________________________ diff --git a/Python3/863.py b/Python3/863.py index 433d2ad..7c38f09 100644 --- a/Python3/863.py +++ b/Python3/863.py @@ -1,5 +1,100 @@ __________________________________________________________________________________________________ +sample 24 ms submission +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None +class Solution(object): + def distanceK(self, root, target, K): + ans = [] + + # Return distance from node to target if exists, else -1 + # Vertex distance: the # of vertices on the path from node to target + def dfs(node): + if not node: + return -1 + elif node is target: + subtree_add(node, 0) + return 1 + else: + L, R = dfs(node.left), dfs(node.right) + if L != -1: + if L == K: + ans.append(node.val) + # return -1 + subtree_add(node.right, L + 1) + return L + 1 + elif R != -1: + if R == K: + ans.append(node.val) + # return -1 + subtree_add(node.left, R + 1) + return R + 1 + else: + return -1 + + # Add all nodes 'K - dist' from the node to answer. + def subtree_add(node, dist): + if not node: + return + elif dist == K: + ans.append(node.val) + else: + subtree_add(node.left, dist + 1) + subtree_add(node.right, dist + 1) + + dfs(root) + return ans __________________________________________________________________________________________________ +sample 13016 kb submission +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None +class Solution: + def distanceK(self, root, target, K): + """ + :type root: TreeNode + :type target: TreeNode + :type K: int + :rtype: List[int] + """ + stack = [] + def dfs(root): + if root is target: + stack.append(root) + return True + if root is None: + return False + stack.append(root) + if dfs(root.right) or dfs(root.left): + return True + stack.pop() + return False + dfs(root) + stack.reverse() + ans = [] + def getNode(root,distance): + if root is None: + return + if distance == 0: + ans.append(root.val) + return + if not stack or root.left is not stack[-1]: + getNode(root.left, distance-1) + if not stack or root.right is not stack[-1]: + getNode(root.right, distance-1) + return + while stack: + node = stack.pop() + if K - len(stack) < 0: + continue + getNode(node,K - len(stack)) + return ans __________________________________________________________________________________________________ diff --git a/Python3/864.py b/Python3/864.py index 433d2ad..2556eae 100644 --- a/Python3/864.py +++ b/Python3/864.py @@ -1,5 +1,117 @@ __________________________________________________________________________________________________ - +sample 172 ms submission +import collections +class Solution: + def shortestPathAllKeys(self, grid: List[str]) -> int: + m, n = len(grid), len(grid[0]) + nodes = {} + for r, row in enumerate(grid): + for c, v in enumerate(row): + if v not in '.#': + nodes[v] = (r, c) + def bfs_from(source): + visited = [[False for _ in range(n)] for _ in range(m)] + r, c = nodes[source] + queue = [(r, c, 0)] + visited[r][c] = True + dist = {} + while queue: + r, c, d = queue.pop(0) + if source != grid[r][c] != '.': + dist[grid[r][c]] = d + continue + for nr, nc in ((r-1, c), (r+1, c), (r, c-1), (r, c+1)): + if nr >= 0 and nr < m and nc >= 0 and nc < n and grid[nr][nc] != '#' and not visited[nr][nc]: + visited[nr][nc] = True + queue.append((nr, nc, d+1)) + return dist + + edges = {node: bfs_from(node) for node in nodes} + num_keys = sum(e.islower() for e in edges.keys()) + #print(edges) + # dijikstra Algo + pq = [(0, '@', '')] + final_dist = collections.defaultdict(lambda: float('inf')) + final_dist[('@', '')] + + while pq: + d, e, state = heapq.heappop(pq) + # print(e, d, state) + if final_dist[e, state] < d: + continue + if len(state) == num_keys: + return d + for destination, dist in edges[e].items(): + state1 = state + if destination.islower(): #key + if destination not in state: + state1 += destination + elif destination.isupper(): + if destination.lower() not in state: + continue + if d + dist < final_dist[destination, state1]: + final_dist[destination, state1] = d + dist + heapq.heappush(pq, (d + dist, destination, state1)) + return -1 __________________________________________________________________________________________________ - +sample 13224 kb submission +import collections +import math +class Solution: + def shortestPathAllKeys(self, grid: List[str]) -> int: + m = len(grid) + n = len(grid[0]) + def neighbor(r, c): + for nr, nc in (r-1, c), (r+1, c), (r, c-1), (r, c+1): + if 0 <= nr < m and 0 <= nc < n: + yield nr, nc + + def bfs(source, row, col): + visited = [[False] * n for _ in range(m)] + visited[row][col] = True + Q = collections.deque([(row, col, 0)]) + dist = {} + while Q: + r, c, d = Q.popleft() + if grid[r][c] != source and grid[r][c] != '.': + dist[grid[r][c]] = d + continue + + for nr, nc in neighbor(r, c): + if grid[nr][nc] != '#' and not visited[nr][nc]: + Q.append((nr, nc, d + 1)) + visited[nr][nc] = True + return dist + + numKey = 0 + dists = collections.defaultdict(dict) + for i in range(m): + for j in range(n): + if grid[i][j] not in '.#': + if grid[i][j].islower(): + numKey += 1 + dists[grid[i][j]] = bfs(grid[i][j], i, j) + + target = 2 ** numKey - 1 + pq = [(0, '@', 0)] + final_dist = collections.defaultdict(lambda: math.inf) + final_dist['@', 0] = 0 + while pq: + d, place, state = heapq.heappop(pq) + if final_dist[place, state] < d: + continue + if state == target: + return d + for destination, d2 in dists[place].items(): + state2 = state + if destination.islower(): + state2 |= (1 << (ord(destination) - ord('a'))) + elif destination.isupper(): + if not (state & (1 << (ord(destination) - ord('A')))): + continue + + if d + d2 < final_dist[destination, state2]: + final_dist[destination, state2] = d + d2 + heapq.heappush(pq, (d + d2, destination, state2)) + return -1 __________________________________________________________________________________________________ diff --git a/Python3/865.py b/Python3/865.py index 433d2ad..ea81624 100644 --- a/Python3/865.py +++ b/Python3/865.py @@ -1,5 +1,48 @@ __________________________________________________________________________________________________ +sample 28 ms submission +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None +class Solution: + def returnDepth(self, root): + if root == None: + return (0, None) + (leftDepth, leftNode) = self.returnDepth(root.left) + rightDepth, rightNode = self.returnDepth(root.right) + if leftDepth == rightDepth: + return (leftDepth+1, root) + elif rightDepth > leftDepth: + return (rightDepth+1, rightNode) + else: + return (leftDepth+1, leftNode) + + def subtreeWithAllDeepest(self, root: TreeNode) -> TreeNode: + depth, node = self.returnDepth(root) + return node __________________________________________________________________________________________________ +sample 13132 kb submission +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None +class Solution: + def subtreeWithAllDeepest(self, root: TreeNode) -> TreeNode: + def deep(root): + if not root: + return 0, None + l, r = deep(root.left), deep(root.right) + if l[0] > r[0]: + return l[0] + 1, l[1] + elif l[0] < r[0]: + return r[0] + 1, r[1] + else: + return l[0] + 1, root + return deep(root)[1] __________________________________________________________________________________________________ diff --git a/Python3/866.py b/Python3/866.py index 433d2ad..a17e1de 100644 --- a/Python3/866.py +++ b/Python3/866.py @@ -1,5 +1,59 @@ __________________________________________________________________________________________________ - +sample 36 ms submission +class Solution: + def primePalindrome(self, N: int) -> int: + def isPrime(n): # only input odd number + return all(n%i for i in range(3, int(n**0.5)+1, 2)) + + if N <= 11: + return {1:2, 2:2, 3:3, 4:5, 5:5, 6:7, 7:7, 8:11, 9:11, 10:11, 11:11}[N] + + # all even length palindrome number must could be divided by 11, so it is not prime number unless it is 11 itself + # all prime palindrome number > 11 will be odd length, so it have a middle digit + if len(str(N))%2 == 0: + leftN = int('1'+'0'* (len(str(N))//2)) + else: + leftN = int(str(N)[:len(str(N))//2+1]) + while leftN < 20000: # max answer will be less than 2*10**9 + leftS = str(leftN) + if int(leftS[0])%2 == 0: # jump all number which first digit is even + leftS = str(int(leftS[0])+1) + '0'*(len(leftS)-1) + cand = int(leftS + leftS[:-1][::-1]) + if cand >= N and isPrime(cand): + return cand + leftN += 1 __________________________________________________________________________________________________ - +sample 40 ms submission +class Solution: + def primePalindrome(self, N: int) -> int: + def isPrime(n): # only input odd number + return all(n%i for i in range(3, int(n**0.5)+1, 2)) + + if N <= 11: + if N <= 2: + return 2 + if N%2 == 0: + N += 1 + while N <= 11: + if isPrime(N): + return N + N += 2 + + S = str(N) + if S[::-1] == S and isPrime(N): # it is palindrome already + return N + # all even length palindrome number must could be divided by 11, so it is not prime number unless it is 11 itself + if len(S)%2 == 0: + S = '1' + '0' * len(S) + # all prime palindrome number > 11 will be odd length, so it have a middle digit + leftS = S[:len(S)//2+1] + leftN = int(leftS) + while leftN < 20000: # max answer will be less than 2*10**9 + leftS = str(leftN) + if int(leftS[0])%2 == 0: + leftS = str(int(leftS[0])+1) + '0'*(len(leftS)-1) + cand = int(leftS + leftS[:-1][::-1]) + if cand >= N and isPrime(cand): + return cand + leftN += 1 __________________________________________________________________________________________________ diff --git a/Python3/867.py b/Python3/867.py index 433d2ad..80da979 100644 --- a/Python3/867.py +++ b/Python3/867.py @@ -1,5 +1,33 @@ __________________________________________________________________________________________________ - +sample 68 ms submission +class Solution: + def transpose(self, A: List[List[int]]) -> List[List[int]]: + return map(list,zip(*A)) __________________________________________________________________________________________________ +sample 13548 kb submission +class Solution: + def transpose(self, A: List[List[int]]) -> List[List[int]]: + #2D list + #[col][row] + #[i][j] + + #switch the size 2 X 3 for A then a 3 X 2 for B + rows = len(A[0]) + cols = len(A) + + #This is what's called a "list comprehension" + B = [ ([0] * cols) for row in range(rows) ] + + + #loop through + #for every row i and every column j get value in A + #for every column j and every row i put value in B + i = 0 + j = 0 + + for i in range(len(A)): #rows + for j in range(len(A[0])): # cols + B[j][i] = A[i][j] + return B __________________________________________________________________________________________________ diff --git a/Python3/868.py b/Python3/868.py index 433d2ad..b242793 100644 --- a/Python3/868.py +++ b/Python3/868.py @@ -1,5 +1,37 @@ __________________________________________________________________________________________________ - +sample 20 ms submission +class Solution: + def binaryGap(self, N: int) -> int: + abin = str(bin(N))[3:] + length = 0 + max_len = 0 + for char in abin: + length += 1 + if int(char): + max_len = max(max_len, length) + length = 0 + return max_len __________________________________________________________________________________________________ - +sample 12892 kb submission +class Solution: + def binaryGap(self, N: int) -> int: + t = '{0:0b}'.format(N) + d = 0 + md = 0 + in_pair = False + + for i in t: + if in_pair: + d += 1 + + if i == '1': + in_pair = False + md = max(d, md) + d = 0 + + if i == '1' and not in_pair: + in_pair = True + d = 0 + + return md __________________________________________________________________________________________________ diff --git a/Python3/869.py b/Python3/869.py index 433d2ad..ccd7d0b 100644 --- a/Python3/869.py +++ b/Python3/869.py @@ -1,5 +1,36 @@ __________________________________________________________________________________________________ - +sample 28 ms submission +from collections import Counter +class Solution: + def reorderedPowerOf2(self, N: int) -> bool: + L = math.floor(math.log(N,10))+1 + log2of10 = math.log(10,2) + + def areRearrangements(adict,b): + bdict = Counter(str(b)) + return bdict == adict + + + adict = Counter(str(N)) + + # if N has k digits, then its rearrangements are between 10^(k-1) and 10^k + # so the powers of 2 it could be have powers between (k-1)log_2(10) and klog_2(10) + exp = math.ceil((L-1)*log2of10) + power = 2**exp + while exp < math.ceil(L*log2of10): + if areRearrangements(adict,power): + return True + else: + power *= 2 + exp += 1 + return False __________________________________________________________________________________________________ +sample 13040 kb submission +class Solution: + # 模範解答をベースに高速化 + def __init__(self): + self.memo = set([''.join(sorted(str(2**i))) for i in range(30)]) + def reorderedPowerOf2(self, N: int) -> bool: + return ''.join(sorted(str(N))) in self.memo __________________________________________________________________________________________________ diff --git a/Python3/870.py b/Python3/870.py index 433d2ad..17c2197 100644 --- a/Python3/870.py +++ b/Python3/870.py @@ -1,5 +1,41 @@ __________________________________________________________________________________________________ - +sample 388 ms submission +class Solution: + def advantageCount(self, A: List[int], B: List[int]) -> List[int]: + + idxs = sorted(range(len(B)), key=B.__getitem__, reverse=True) + + A.sort() + + ai = len(A) -1 + res = [-1] * len(A) + al = 0 + #print(b_sort, a_sort) + for bi in idxs: + if A[ai] > B[bi]: + res[bi] = A[ai] + ai -= 1 + else: + res[bi] = A[al] + al += 1 + #print(bi, res) + + + return res __________________________________________________________________________________________________ - +sample 15404 kb submission +class Solution: + def advantageCount(self, A, B): + A.sort() + N = len(B) + idxB = sorted(range(N), key = lambda i: -B[i]) + L, R, ans = 0, N - 1, [None] * N + for i in idxB: + if B[i] >= A[R]: + ans[i] = A[L] + L += 1 + else: + ans[i] = A[R] + R -= 1 + return ans __________________________________________________________________________________________________ diff --git a/Python3/871.py b/Python3/871.py index 433d2ad..3cc2435 100644 --- a/Python3/871.py +++ b/Python3/871.py @@ -1,5 +1,36 @@ __________________________________________________________________________________________________ - +sample 124 ms submission +import heapq +class Solution: + def minRefuelStops(self, target: int, startFuel: int, stations: List[List[int]]) -> int: + cnt = prevdis = 0 + heap = [] + stations.append((target, 0)) + for (dis, fuel) in stations: + startFuel = startFuel - dis + prevdis + prevdis = dis + while startFuel < 0: + if len(heap) == 0 : return -1 + cnt += 1 + startFuel -= heapq.heappop(heap) + heapq.heappush(heap, -fuel) + return cnt __________________________________________________________________________________________________ - +sample 128 ms submission +class Solution: + def minRefuelStops(self, target, startFuel, stations): + stations.append([target, float('inf')]) + max_hp = [] + tank = startFuel # tank is left gas for car + prev = 0 + ans = 0 + for s in stations: + tank -= s[0] - prev # cosume (s[0]-prev) L gas + while tank < 0 and max_hp: + tank += -heapq.heappop(max_hp) + ans += 1 + if tank < 0: return -1 + heapq.heappush(max_hp, -s[1]) + prev = s[0] + return ans __________________________________________________________________________________________________ diff --git a/Python3/872.py b/Python3/872.py index 433d2ad..9a463ac 100644 --- a/Python3/872.py +++ b/Python3/872.py @@ -1,5 +1,61 @@ __________________________________________________________________________________________________ +sample 24 ms submission +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None +from collections import deque + +class Solution: + def leafSimilar(self, root1: TreeNode, root2: TreeNode) -> bool: + def get_leafs(root): + if not root: + return [] + if not root.left and not root.right: + return [root.val] + + return get_leafs(root.left) + get_leafs(root.right) + + return get_leafs(root1) == get_leafs(root2) __________________________________________________________________________________________________ +sample 12908 kb submission +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None +class Solution: + def leafSimilar(self, root1: TreeNode, root2: TreeNode) -> bool: + + return self.getleaf(root1) == self.getleaf(root2) + + + def getleaf(self, root): + + if not root: return [] + + stack = [root] + out = [] + while root.left: + stack.append(root.left) + root = root.left + + while stack: + p = stack.pop() + if not p.left and not p.right: + out.append(p.val) + + if p.right: + stack.append(p.right) + p = p.right + while p.left: + stack.append(p.left) + p = p.left + + return out __________________________________________________________________________________________________ diff --git a/Python3/873.py b/Python3/873.py index 433d2ad..44d72ea 100644 --- a/Python3/873.py +++ b/Python3/873.py @@ -1,5 +1,33 @@ __________________________________________________________________________________________________ - +sample 128 ms submission +class Solution: + def lenLongestFibSubseq(self, A: List[int]) -> int: + A_set = set(A) + result = 2 + for i in range(len(A) - 1): + for j in range(i + 1, len(A)): + a, b = A[i], A[j] + length = 2 + if b * (result - 2) >= A[-1]: + break + while a + b in A_set: + a, b = b, a + b + length += 1 + result = max(result, length) + return result if result > 2 else 0 __________________________________________________________________________________________________ - +sample 13376 kb submission +class Solution: + def lenLongestFibSubseq(self, A: List[int]) -> int: + index = {x: i for i, x in enumerate(A)} + longest = collections.defaultdict(lambda: 2) + ans = 0 + for k in range(len(A)): + for j in range(k): + i = index.get(A[k]-A[j]) + if i is not None and i < j: + cand = longest[j, k] = longest[i, j] + 1 + ans = max(ans, cand) + + return ans if ans >= 3 else 0 __________________________________________________________________________________________________ diff --git a/Python3/874.py b/Python3/874.py index 433d2ad..1d838f6 100644 --- a/Python3/874.py +++ b/Python3/874.py @@ -1,5 +1,69 @@ __________________________________________________________________________________________________ - +sample 376 ms submission +class Solution: + def robotSim(self, commands: List[int], obstacles: List[List[int]]) -> int: + x = 0 + y = 0 + h = 'n' + ans = 0 + obstacleSet = set(map(tuple, obstacles)) + for c in commands: + if c > 0: + if h =='n': + for i in range(c): + if (x,y+1) not in obstacleSet: + y += 1 + else: + break + elif h == 's': + for i in range(c): + if (x,y-1) not in obstacleSet: + y -= 1 + else: + break + elif h == 'w': + for i in range(c): + if (x-1,y) not in obstacleSet: + x -= 1 + else: + break + elif h == 'e': + for i in range(c): + if (x+1,y) not in obstacleSet: + x += 1 + else: + break + + ans = max(ans, x*x + y*y) + + elif c == -1: + if h =='n': h = 'e' + elif h == 's': h = 'w' + elif h == 'w': h = 'n' + elif h == 'e': h = 's' + elif c == -2: + if h =='n': h = 'w' + elif h == 's': h = 'e' + elif h == 'w': h = 's' + elif h == 'e': h = 'n' + + return ans __________________________________________________________________________________________________ - +sample 17284 kb submission +class Solution: + def robotSim(self, commands, obstacles): + dx, dy = [0, 1, 0, -1], [1, 0, -1, 0] + obstacles = set(map(tuple, obstacles)) + x, y, di, ans = 0, 0, 0, 0 + for c in commands: + if c == -1: di = (di + 1) % 4 + elif c == -2: di = (di - 1) % 4 + else: + for _ in range(c): + if (x + dx[di], y + dy[di]) not in obstacles: + x += dx[di] + y += dy[di] + else: break + if x * x + y * y > ans: ans = x * x + y * y + return ans __________________________________________________________________________________________________ diff --git a/Python3/875.py b/Python3/875.py index 433d2ad..290e667 100644 --- a/Python3/875.py +++ b/Python3/875.py @@ -1,5 +1,32 @@ __________________________________________________________________________________________________ +sample 164 ms submission +class Solution: + def minEatingSpeed(self, piles: List[int], H: int) -> int: + def eat_all(K): + return sum([(p - 1) // K + 1 for p in piles]) <= H + if H < len(piles): + return -1 + elif H == len(piles): + return max(piles) + else: + total = sum(piles) + lo, hi = math.ceil(total / H), math.ceil((total) / (H - len(piles) + 1)) + while lo < hi: + mid = (lo + hi) // 2 + if eat_all(mid): + hi = mid + else: + lo = mid + 1 + return lo __________________________________________________________________________________________________ - +sample 14116 kb submission +class Solution: + def minEatingSpeed(self, piles, H): + l, r = 1, max(piles) + while l < r: + m = (l + r) // 2 + if sum((p + m - 1) // m for p in piles) > H: l = m + 1 + else: r = m + return l __________________________________________________________________________________________________ diff --git a/Python3/876.py b/Python3/876.py index 433d2ad..da5832b 100644 --- a/Python3/876.py +++ b/Python3/876.py @@ -1,5 +1,54 @@ __________________________________________________________________________________________________ +sample 20 ms submission +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, x): +# self.val = x +# self.next = None +class Solution: + def middleNode(self, head: ListNode) -> ListNode: + if(head.next == None): + return head + slowPtr = head + fastPtr = head.next + while(fastPtr.next!=None and fastPtr.next.next!=None): + fastPtr = fastPtr.next.next + slowPtr = slowPtr.next + return slowPtr.next __________________________________________________________________________________________________ +sample 13056 kb submission +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, x): +# self.val = x +# self.next = None +import math +class Solution: + def middleNode(self, head: ListNode) -> ListNode: + target = None + length = Solution.getLength(head) + + if length % 2 == 0: + target = (length/2) + 1 + else: + target = math.ceil(length/2) + + n = 1 + node = head + while True: + if n == target: + return node + node = node.next + n += 1 + + @staticmethod + def getLength(head: ListNode) -> int: + n = 1 + node = head + while node.next is not None: + node = node.next + n += 1 + return n __________________________________________________________________________________________________ diff --git a/Python3/877.py b/Python3/877.py index 433d2ad..556baa3 100644 --- a/Python3/877.py +++ b/Python3/877.py @@ -1,5 +1,11 @@ __________________________________________________________________________________________________ - +sample 28 ms submission +class Solution: + def stoneGame(self, piles: List[int]) -> bool: + return True __________________________________________________________________________________________________ - +sample 13072 kb submission +class Solution: + def stoneGame(self, piles: List[int]) -> bool: + return True __________________________________________________________________________________________________ diff --git a/Python3/878.py b/Python3/878.py index 433d2ad..7e6e7c0 100644 --- a/Python3/878.py +++ b/Python3/878.py @@ -1,5 +1,54 @@ __________________________________________________________________________________________________ - +sample 20 ms submission +class Solution: + def nthMagicalNumber(self, N: int, A: int, B: int) -> int: + + mod = int(1e9+7) + from math import gcd + + a, b = (A, B) if A 'int': + smaller = min(A, B) + left, right = smaller, smaller * N + + while (left <= right): + mid = left + (right - left) // 2 + count = self.countMagical(mid, A, B) + + if count == N: + return int(max(mid // A * A, mid // B * B)) % int(10 ** 9 + 7) + elif count > N: + right = mid - 1 + else: + left = mid + 1 + + return -1 + + def countMagical(self, X, A, B): + return X // A + X // B - X // self.minCommonTimes(A, B) + + def minCommonTimes(self, A, B): + smaller = min(A, B) + bigger = max(A, B) + while (smaller): + left = bigger % smaller + bigger = smaller + smaller = left + return A // bigger * B __________________________________________________________________________________________________ diff --git a/Python3/879.py b/Python3/879.py index 433d2ad..2c02f2c 100644 --- a/Python3/879.py +++ b/Python3/879.py @@ -1,5 +1,39 @@ __________________________________________________________________________________________________ - +sample 268 ms submission +import numpy as np +class Solution: + def profitableSchemes(self, G: int, P: int, group: List[int], profit: List[int]) -> int: + n = len(profit) + dp = np.zeros((n,G+1,P+1),dtype='int') + dp[0,:,0] = 1 # profit, gang , target profit + dp[0,group[0]:,:profit[0] + 1] += 1 + + for i in range(1,n): + dp[i,:,:] = dp[i-1,:,:] + if(profit[i] < P): + dp[i, group[i]:, profit[i]+1:] += dp[i-1, 0: G + 1 - group[i], 1: P - profit[i] + 1] + for g in range(group[i], G + 1): + dp[i, g , :profit[i] + 1] += dp[i-1, g - group[i], 0] # why ? + dp[i,:,:] = np.mod(dp[i,:,:], 1000000007) + return int(dp[-1][-1][-1]) __________________________________________________________________________________________________ - +sample 800 ms submission +class Solution: + def profitableSchemes(self, G: int, P: int, group: List[int], profit: List[int]) -> int: + + # solution but slow + n = len(group) + dp = [[0 for _ in range(G+1)] for _ in range(P+1)] + dp[0][0] = 1 + + for i in range(n): + temp = [row[:] for row in dp] + for p in range(P+1): + pbound = min(p+profit[i], P) + for g in range(G-group[i]+1): + temp[pbound][g+group[i]] += dp[p][g] + + dp = temp + + return sum(dp[P]) % (pow(10, 9) + 7) __________________________________________________________________________________________________ diff --git a/Python3/880.py b/Python3/880.py index 433d2ad..3253311 100644 --- a/Python3/880.py +++ b/Python3/880.py @@ -1,5 +1,31 @@ __________________________________________________________________________________________________ - +sample 24 ms submission +class Solution: + def decodeAtIndex(self, S: str, K: int) -> str: + N = 0 + for i, c in enumerate(S): + N = N * int(c) if c.isdigit() else N + 1 + if K <= N: break + for j in range(i, -1, -1): + c = S[j] + if c.isdigit(): + N /= int(c) + K %= N + else: + if K == N or K == 0: return c + N -= 1 __________________________________________________________________________________________________ - +sample 28 ms submission +class Solution: + def decodeAtIndex(self, S: str, K: int) -> str: + stack, l = [], 0 + for c in S: + l = l + 1 if c.isalpha() else l * int(c) + stack += c, + while l >= K: + while stack[-1].isdigit(): l //= int(stack.pop()) + K = K % l + if not K: return stack[-1] + l -= 1 + stack.pop() __________________________________________________________________________________________________ diff --git a/Python3/881.py b/Python3/881.py index 433d2ad..5e095df 100644 --- a/Python3/881.py +++ b/Python3/881.py @@ -1,5 +1,29 @@ __________________________________________________________________________________________________ - +sample 536 ms submission +class Solution: + def numRescueBoats(self, people: List[int], limit: int) -> int: + people = sorted(people) + ans, start, end = 0, 0, len(people)-1 + while start < end: + if people[end] + people[start] <= limit: + start += 1 + ans += 1 + end -= 1 + return ans+1 if start == end else ans __________________________________________________________________________________________________ - +sample 17560 kb submission +class Solution: + def numRescueBoats(self, people: List[int], limit: int) -> int: + people.sort() + + count = 0 + p, q = 0, len(people) - 1 + while p <= q: + if people[p] + people[q] <= limit: + p += 1 + q -= 1 + else: + q -= 1 + count += 1 + return count __________________________________________________________________________________________________ diff --git a/Python3/882.py b/Python3/882.py index 433d2ad..442ed1d 100644 --- a/Python3/882.py +++ b/Python3/882.py @@ -1,5 +1,124 @@ __________________________________________________________________________________________________ - +sample 504 ms submission +class Solution: + def reachableNodes(self, edges: List[List[int]], M: int, N: int) -> int: + ''' + single source weighted graph for shortest distance - dijkstra + + re exam each edge + p-----q + + from p side we can reach x nodes, from q size we can reach y nodes then we know how many nodes + can be reached on this edge + ''' + g= collections.defaultdict(dict) + for i, j, w in edges: + g[i][j] = w + g[j][i] = w + + solved = [False]*N + dist = [math.inf]*N + dist[0] = 0 + + heap = [[0, 0]] + while heap: + d, i = heapq.heappop(heap) + + if solved[i]: + continue + + solved[i] = True + for j in g[i]: + if not solved[j]: + w = dist[i] + g[i][j] + 1 # w+1 steps to reach the other original node + if w<=M and w int: + '''Create graph''' + graph = {} + for vetex in range(N): + graph[vetex] = {} + + for vetex1, vetex2, distance in edges: + graph[vetex1][vetex2] = distance + graph[vetex2][vetex1] = distance + + '''initialize heap queue''' + heap_queue = [] + move, src = 0, 0 + hpush(heap_queue, (move, src)) + + '''initialize others''' + visited = set() + count = 0 + + while heap_queue: + move, vetex = hpop(heap_queue) + + '''Use all the step, break''' + if move > M: + break + + '''This node has been visited, pass''' + if vetex in visited: + continue + + + visited.add(vetex) + count += 1 + + for neighbor in graph[vetex]: + weight = graph[vetex][neighbor] + next_move = move + weight + 1 + + ''' + if the neighboring vetex has been visited, + we check if the remaining step (M - move) is enough + to cover all nodes between vetex and neighbor + Later on, we'll see we only save nodes that have not been + visited. + ''' + if neighbor in visited: + count = count + min(M - move, graph[vetex][neighbor]) + + else: + if next_move > M: + count = count + (M - move) + '''Save nodes that are only not been visited.''' + graph[neighbor][vetex] -= (M - move) + + else: + count = count + weight + ''' + Save nodes that are only not been visited. + Here, all have been visited, so updated to 0 + ''' + graph[neighbor][vetex] = 0 + '''Update next_move as move''' + hpush(heap_queue, (next_move, neighbor)) + + return count __________________________________________________________________________________________________ diff --git a/Python3/883.py b/Python3/883.py index 433d2ad..36bca91 100644 --- a/Python3/883.py +++ b/Python3/883.py @@ -1,5 +1,24 @@ __________________________________________________________________________________________________ - +sample 68 ms submission +class Solution: + def projectionArea(self, grid): + hor = sum(map(max, grid)) + ver = sum(map(max, zip(*grid))) + top = sum(v > 0 for row in grid for v in row) + return ver + hor + top __________________________________________________________________________________________________ - +sample 13140 kb submission +class Solution: + def projectionArea(self, grid: List[List[int]]) -> int: + if len(grid)==1: + a=len([i for i in grid[0] if i>0]) + b=max(grid[0]) + else: + a=sum(list(map(lambda x:len([i for i in x if i>0]),grid))) + b=sum(list(map(lambda x:max(x),grid))) + if len(grid[0])==1: + c=max(*grid) + else: + c=sum(list(map(lambda x:max(x),list(zip(*grid))))) + return a+b+c __________________________________________________________________________________________________ diff --git a/Python3/884.py b/Python3/884.py index 433d2ad..c2509e2 100644 --- a/Python3/884.py +++ b/Python3/884.py @@ -1,5 +1,40 @@ __________________________________________________________________________________________________ - +sample 20 ms submission +class Solution: + def uncommonFromSentences(self, A: str, B: str) -> List[str]: + A = A + ' ' + B + words = A.split() + table = {word: 0 for word in words} + res = [] + + for word in words: + if table[word] is 0: + table[word] = 1 + else: + table[word] += 1 + + for k,v in table.items(): + if v == 1: + res.append(k) + + return res __________________________________________________________________________________________________ +sample 12948 kb submission +from collections import defaultdict +class Solution: + def uncommonFromSentences(self, A: str, B: str) -> List[str]: + freq1, freq2 = defaultdict(int), defaultdict(int) + for w in A.split(' '): + freq1[w] += 1 + for w in B.split(' '): + freq2[w] += 1 + res = [] + for key in freq1: + if key not in freq2 and freq1[key] == 1: + res.append(key) + for key in freq2: + if key not in freq1 and freq2[key] == 1: + res.append(key) + return res __________________________________________________________________________________________________ diff --git a/Python3/885.py b/Python3/885.py index 433d2ad..0eb96f2 100644 --- a/Python3/885.py +++ b/Python3/885.py @@ -1,5 +1,66 @@ __________________________________________________________________________________________________ - +sample 108 ms submission +class Solution: + def spiralMatrixIII(self, R: int, C: int, r0: int, c0: int) -> List[List[int]]: + up = down = r0 + left = right = c0 + direct = 0 + num = 1 + res = [] + while (num <= C*R): + direct %= 4 + if direct == 0: + if up >= 0: + for i in range(max(0,left), min(right+1,C)): + res.append([up,i]) + num += 1 + right += 1 + elif direct == 1: + if right < C: + for i in range(max(0,up), min(down+1,R)): + res.append([i,right]) + num += 1 + down += 1 + elif direct == 2: + if down < R: + for i in reversed(range(max(0,left),min(right+1,C))): + res.append([down,i]) + num += 1 + left -= 1 + elif direct == 3: + if left >=0: + for i in reversed(range(max(0,up),min(down+1,R))): + res.append([i,left]) + num += 1 + up -= 1 + direct += 1 + return res __________________________________________________________________________________________________ - +sample 13924 kb submission +class Solution: + def spiralMatrixIII(self, R: int, C: int, r0: int, c0: int) -> List[List[int]]: + solu = [] + count = 0 + for gap in range(0, max(R,C)): + if 0 <= r0-gap < R: + for j in range(max(c0-gap,0), min(c0+gap+1, C)): + solu.append([r0-gap, j]) + count += 1 + if count == R*C: return solu + if 0 <= c0+gap+1 < C: + for i in range(max(r0-gap,0), min(r0+gap+1, R)): + solu.append([i, c0+gap+1]) + count += 1 + if count == R*C: return solu + if 0 <= r0+gap+1 < R: + for j in range(min(c0+gap+1, C-1), max(c0-gap-1, -1), -1): + solu.append([r0+gap+1, j]) + count += 1 + if count == R*C: return solu + if 0 <= c0-gap-1 < C: + for i in range(min(r0+gap+1, R-1), max(r0-gap-1, -1), -1): + solu.append([i, c0-gap-1]) + count += 1 + if count == R*C: return solu + return solu __________________________________________________________________________________________________ diff --git a/Python3/886.py b/Python3/886.py index 433d2ad..1064561 100644 --- a/Python3/886.py +++ b/Python3/886.py @@ -1,5 +1,53 @@ __________________________________________________________________________________________________ - +sample 772 ms submission +class Solution: + def possibleBipartition(self, N: int, dislikes: List[List[int]]) -> bool: + d = collections.defaultdict(list) + for a, b in dislikes: + d[a].append(b) + d[b].append(a) + color = [0] * (N + 1) + for i in range(1, N + 1): + if not color[i]: + queue = collections.deque([i]) + color[i] = 1 + while queue: + cur = queue.popleft() + for nei in d[cur]: + if not color[nei]: + color[nei] = -color[cur] + queue.append(nei) + elif color[nei] == color[cur]: + return False + return True __________________________________________________________________________________________________ +sample 784 ms submission +from collections import defaultdict + +class Solution: + def possibleBipartition(self, n: int, dislikes: List[List[int]]) -> bool: + graph = defaultdict(list) + for a, b in dislikes: + graph[a].append(b) + graph[b].append(a) + + tags = {} + for start in range(1, n+1): + if start in tags: + continue + + tags[start] = 0 + to_visit = [start] + while to_visit: + node = to_visit.pop() + tag = tags[node] + for neighbor in graph[node]: + if neighbor not in tags: + tags[neighbor] = 1 - tag # Alternates between 0 and 1 + to_visit.append(neighbor) + else: + if tags[neighbor] != 1 - tag: + return False + return True __________________________________________________________________________________________________ diff --git a/Python3/887.py b/Python3/887.py index 433d2ad..7b151d3 100644 --- a/Python3/887.py +++ b/Python3/887.py @@ -1,5 +1,32 @@ __________________________________________________________________________________________________ - +sample 28 ms submission +class Solution: + def superEggDrop(self, K: int, N: int) -> int: + memo = {} + m = 1 + + while True: + for k in range(1, K+1): + if k == 1: + memo[(m, k)] = m + elif m < k: + memo[(m, k)] = memo[(m, m)] + else: + memo[(m, k)] = memo[(m-1, k)] + memo[(m-1, k-1)] + 1 + if memo[(m, K)] >=N: + return m + m += 1 __________________________________________________________________________________________________ - +sample 13060 kb submission +class Solution: + def superEggDrop(self, K, N): + dp = [0, 0] + m = 0 + while dp[-1] < N: + for i in range(len(dp) - 1, 0, - 1): + dp[i] += dp[i - 1] + 1 + if len(dp) < K + 1: + dp.append(dp[-1]) + m += 1 + return m __________________________________________________________________________________________________ diff --git a/Python3/888.py b/Python3/888.py index 433d2ad..1686b95 100644 --- a/Python3/888.py +++ b/Python3/888.py @@ -1,5 +1,43 @@ __________________________________________________________________________________________________ - +sample 396 ms submission +class Solution: + def fairCandySwap(self, A: List[int], B: List[int]) -> List[int]: + x, y = sum(A), sum(B) + z = (x + y) // 2 + + B = set(B) + for a in A: + b = a + z - x + if b in B: + return [a, b] __________________________________________________________________________________________________ - +sample 14536 kb submission +class Solution: + def fairCandySwap(self, A: List[int], B: List[int]) -> List[int]: + sumA = sum(A) + sumB = sum(B) + d = abs(sumA-sumB) + + lenA = len(A) + lenB = len(B) + i = j = 0 + + A.sort() + B.sort() + + while i < lenA and j < lenB: + if sumA < sumB: + if B[j]-A[i]==d/2: + return [A[i], B[j]] + elif B[j]-A[i] > d/2: + i+=1 + else: + j+=1 + else: + if A[i]-B[j]==d/2: + return [A[i], B[j]] + elif A[i]-B[j] > d/2: + j+=1 + else: + i+=1 __________________________________________________________________________________________________ diff --git a/Python3/889.py b/Python3/889.py index 433d2ad..879bc4f 100644 --- a/Python3/889.py +++ b/Python3/889.py @@ -1,5 +1,51 @@ __________________________________________________________________________________________________ +sample 48 ms submission +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None +class Solution: + def constructFromPrePost(self, pre: List[int], post: List[int]) -> TreeNode: + p = 0 + root = TreeNode(pre[0]) + stack = [root] + for i in range(1, len(pre)): + node = None + while stack and stack[-1].val == post[p]: + node = stack.pop() + p += 1 + newNode = TreeNode(pre[i]) + if node: + stack[-1].right = newNode + else: + stack[-1].left = newNode + stack.append(newNode) + return root __________________________________________________________________________________________________ +sample 13028 kb submission +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None +class Solution: + def constructFromPrePost(self, pre: List[int], post: List[int]) -> TreeNode: + stack = [TreeNode(pre[0])] + j = 0 + for v in pre[1:]: + node = TreeNode(v) + while stack[-1].val == post[j]: + stack.pop() + j += 1 + if not stack[-1].left: + stack[-1].left = node + else: + stack[-1].right = node + stack.append(node) + return stack[0] __________________________________________________________________________________________________ diff --git a/Python3/890.py b/Python3/890.py index 433d2ad..443bbd7 100644 --- a/Python3/890.py +++ b/Python3/890.py @@ -1,5 +1,52 @@ __________________________________________________________________________________________________ - +sample 24 ms submission +class Solution: + def findAndReplacePattern(self, words: List[str], pattern: str) -> List[str]: + result = [] + for w in words: + error = False + if len(w) == len(pattern): + p2w = {} + w2p = {} + for i in range(len(w)): + if w[i] not in w2p and pattern[i] not in p2w: + w2p[w[i]] = pattern[i] + p2w[pattern[i]] = w[i] + elif w[i] in w2p and w2p[w[i]] != pattern[i]: + error = True + break + elif pattern[i] in p2w and p2w[pattern[i]] != w[i]: + error = True + break + if not error: + result.append(w) + + return result __________________________________________________________________________________________________ - +sample 13024 kb submission +class Solution: + def findAndReplacePattern(self, words: List[str], pattern: str) -> List[str]: + matches = [] + + for word in words: + match = {} + used = set() + + for i, c in enumerate(word): + pc = pattern[i] + expected = match.get(pc) + + if expected is not None: + if c != expected: + break + else: + if c in used: + break + + used.add(c) + match[pc] = c + else: + matches.append(word) + + return matches __________________________________________________________________________________________________ diff --git a/Python3/891.py b/Python3/891.py index 433d2ad..38684f6 100644 --- a/Python3/891.py +++ b/Python3/891.py @@ -1,5 +1,37 @@ __________________________________________________________________________________________________ - +sample 340 ms submission +class Solution: + def sumSubseqWidths(self, A): + A = sorted(A) + ans = 0 + mod = 10 ** 9 + 7 + for i, v in enumerate(A): + ans *= 2 + ans += A[~i] - A[i] + ans %= mod + return ans __________________________________________________________________________________________________ - +sample 348 ms submission +class Solution: + def sumSubseqWidths(self, A: List[int]) -> int: + N = len(A) + # there are 2**N subsequences of A (including empty and single elements), so we can't iterate over them all for large N + A.sort() + # the original order of A doesn't matter, and since the sub-sequence property we're interesting in 'width' needs (only) the min/max value, sorting A seems like a reasonable first step + + # now, consider all the subsequences that contain both A[0] and A[-1] (the min and max elements). there are 2**(N-2) of these, all with the same width, and we need not construct/consider them any more. progress? + # it seems like the width is uniquelly determined by how many leading/trailing elements are *not in the set*. after that on each end, we know the width is constant, so we need not consider all the sub-strings. so we can do something N^2 on the sorted array, and adjust width by (2-to-the-power-of-) the number of 'inner' elements. + + # okay, next step -- split the calculation into the min and max halves; thus we need only total the maxes and mins of all subsequences seperatly, and then take the delta. we use the same trick to calculate the number of sub-sequenced with a given min/max based on how many elements there are before/after it. + + # FIXME: seems like we could maybe still use the const-factor optimization to reduce the number of bignum ops? + tot_wid = 0 + num_seqs = 1 + for i in range(N): # first included element index + tot_wid += (A[i]-A[N-i-1]) * (num_seqs - 1) # num_seqs can't be zero by fermat's little theorem + num_seqs = (num_seqs << 1) % (10**9 + 7) + + + + return tot_wid % (10**9 + 7) __________________________________________________________________________________________________ diff --git a/Python3/892.py b/Python3/892.py index 433d2ad..7c2842f 100644 --- a/Python3/892.py +++ b/Python3/892.py @@ -1,5 +1,42 @@ __________________________________________________________________________________________________ - +sample 84 ms submission +class Solution: + def surfaceArea(self, grid: List[List[int]]) -> int: + ans = 0 + for i in range(len(grid)): + for j in range(len(grid[0])): + if grid[i][j]: + ans += 4 * grid[i][j] + 2 + for row in grid: + ans -= self.checking(row) + for col in zip(*grid): + ans -= self.checking(col) + return ans + + def checking(self, nums: List[int]) -> int: + total = 0 + for i in range(len(nums) - 1): + total += 2 * nums[i] if nums[i] < nums[i + 1] else 2 * nums[i + 1] + return total __________________________________________________________________________________________________ - +sample 13228 kb submission +class Solution: + def surfaceArea(self, grid: List[List[int]]) -> int: + M = len(grid) + total = 0 + remove = 0 + for i in range(M): + for j in range(M): + total += grid[i][j] + if grid[i][j] != 0: + remove += grid[i][j] - 1 + if j + 1 < M and grid[i][j+1] != 0: + remove += min(grid[i][j],grid[i][j+1]) + + for i in range(M): + for j in range(M): + if j + 1 < M and grid[j+1][i] != 0: + remove += min(grid[j+1][i],grid[j][i]) + + return 6*total - 2*remove __________________________________________________________________________________________________ diff --git a/Python3/893.py b/Python3/893.py index 433d2ad..deb0477 100644 --- a/Python3/893.py +++ b/Python3/893.py @@ -1,5 +1,18 @@ __________________________________________________________________________________________________ - +sample 40 ms submission +class Solution: + def numSpecialEquivGroups(self, A: List[str]) -> int: + def count(A): + ans = [0] * 52 + for i, letter in enumerate(A): + ans[ord(letter) - ord('a') + 26 * (i%2)] += 1 + return tuple(ans) + return len({count(word) for word in A}) __________________________________________________________________________________________________ - +sample 13208 kb submission +class Solution: + def numSpecialEquivGroups(self, A: List[str]) -> int: + temp = ["".join(sorted(a[::2]) + sorted(a[1::2])) for a in A] + # print(temp) + return len(list(set(temp))) __________________________________________________________________________________________________ diff --git a/Python3/894.py b/Python3/894.py index 433d2ad..00f58ca 100644 --- a/Python3/894.py +++ b/Python3/894.py @@ -1,5 +1,55 @@ __________________________________________________________________________________________________ +sample 136 ms submission +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None +class Solution: + def allPossibleFBT(self, N: int) -> List[TreeNode]: + def rec(N): + res = [] + for i in range(1,N,2): + for left in self.memo[i]: + for right in self.memo[N-i-1]: + root = TreeNode(0) + root.left = left + root.right = right + res.append(root) + self.memo[N] = res + self.memo = {1:[TreeNode(0)]} + if N % 2 == 0: + return [] + for i in range(3, N+1, 2): + rec(i) + return self.memo[N] __________________________________________________________________________________________________ +sample 16204 kb submission +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None +class Solution: + def allPossibleFBT(self, N: int) -> List[TreeNode]: + if N % 2 == 0: + return [] # the number of node must be odd + cache = {} + for i in range(1,N+1,2): # only consider odd number + if i == 1: # initialize + cache[1] = [TreeNode(0)] + continue + cache[i] = [] + for j in range(1,i,2): # j represent the number of node in left child + for lkid in cache[j]: + for rkid in cache[i-1-j]: + node = TreeNode(0) + node.left = lkid + node.right = rkid + cache[i].append(node) + return cache[N] __________________________________________________________________________________________________ diff --git a/Python3/895.py b/Python3/895.py index 433d2ad..383b30c 100644 --- a/Python3/895.py +++ b/Python3/895.py @@ -1,5 +1,64 @@ __________________________________________________________________________________________________ +sample 332 ms submission +class FreqStack: + def __init__(self): + self.count = collections.defaultdict(int) + self.freqTable = collections.defaultdict(list) + self.maxFreq = 0 + + + def push(self, x: int) -> None: + self.count[x] += 1 + freq = self.count[x] + self.freqTable[freq].append(x) + if freq>self.maxFreq: + self.maxFreq = freq + + + def pop(self) -> int: + out = self.freqTable[self.maxFreq].pop() + self.count[out] -= 1 + if not self.freqTable[self.maxFreq]: + self.maxFreq -= 1 + return out + +# Your FreqStack object will be instantiated and called as such: +# obj = FreqStack() +# obj.push(x) +# param_2 = obj.pop() __________________________________________________________________________________________________ +sample 19708 kb submission +class FreqStack: +#keep max freq +#keep dict for frequency +#keep dict for occurance of same frequency + def __init__(self): + self.maxFreq = 0 + self.freq = {} + self.stack = collections.defaultdict(list) + + + def push(self, x: int) -> None: + if x in self.freq: + self.freq[x] += 1 + else: + self.freq[x] = 1 + + f = self.freq[x] + self.maxFreq = max(self.maxFreq,f) + self.stack[f].append(x) + + + def pop(self) -> int: + x = self.stack[self.maxFreq].pop() + self.freq[x] -=1 + if not self.stack[self.maxFreq]: + self.maxFreq -=1 + return x +# Your FreqStack object will be instantiated and called as such: +# obj = FreqStack() +# obj.push(x) +# param_2 = obj.pop() __________________________________________________________________________________________________ diff --git a/Python3/896.py b/Python3/896.py index 433d2ad..237698a 100644 --- a/Python3/896.py +++ b/Python3/896.py @@ -1,5 +1,20 @@ __________________________________________________________________________________________________ - +sample 516 ms submission +class Solution: + def isMonotonic(self, A: List[int]) -> bool: + + return all(A[i] <= A[i+1] for i in range(len(A) - 1)) or all(A[i] >= A[i+1] for i in range(len(A) - 1)) __________________________________________________________________________________________________ - +sample 17172 kb submission +class Solution: + def isMonotonic(self, A: 'List[int]') -> 'bool': + hasInc, hasDec = False, False + for i in range(len(A) - 1): + if A[i] < A[i + 1]: + hasInc = True + elif A[i] > A[i + 1]: + hasDec = True + if hasInc and hasDec: + return False + return True __________________________________________________________________________________________________ diff --git a/Python3/897.py b/Python3/897.py index 433d2ad..8fe3925 100644 --- a/Python3/897.py +++ b/Python3/897.py @@ -1,5 +1,57 @@ __________________________________________________________________________________________________ +sample 76 ms submission +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None +class Solution: + def inorderTree(self, root: TreeNode, arr: List[TreeNode]): + if root is None: + return + if(root.left): + self.inorderTree(root.left, arr) + arr.append(root) + if(root.right): + self.inorderTree(root.right, arr) + + def increasingBST(self, root: TreeNode) -> TreeNode: + if root is None: + return root + arr = [] + self.inorderTree(root, arr) + length = len(arr) + root = arr[0] + cur = root + for i in range(1, length): + cur.left = None + cur.right = arr[i] + cur = arr[i] + cur.left = None + cur.right = None + return root __________________________________________________________________________________________________ +sample 13168 kb submission +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None +class Solution: + def increasingBST(self, root): + def inorder(node): + if node: + inorder(node.left) + node.left = None + self.cur.right = node + self.cur = node + inorder(node.right) + + ans = self.cur = TreeNode(None) + inorder(root) + return ans.right __________________________________________________________________________________________________ diff --git a/Python3/898.py b/Python3/898.py index 433d2ad..c80c976 100644 --- a/Python3/898.py +++ b/Python3/898.py @@ -1,5 +1,24 @@ __________________________________________________________________________________________________ - +sample 796 ms submission +class Solution: + def subarrayBitwiseORs(self, A: List[int]) -> int: + Set = set() + tmp = {0} + for cur in A: + tmp = {cur | ele for ele in tmp} + tmp.add(cur) + Set.update(tmp) + + return len(Set) __________________________________________________________________________________________________ - +sample 39212 kb submission +class Solution: + def subarrayBitwiseORs(self, A: List[int]) -> int: + + ans = set() + cur = {0} + for x in A: + cur = {x | y for y in cur} | {x} + ans |= cur + return len(ans) __________________________________________________________________________________________________ diff --git a/Python3/899.py b/Python3/899.py index f464918..cf19ed5 100644 --- a/Python3/899.py +++ b/Python3/899.py @@ -1,11 +1,34 @@ +__________________________________________________________________________________________________ +sample 32 ms submission class Solution: def orderlyQueue(self, S: str, K: int) -> str: - s = ''.join(sorted(S)) + # Simulate + if K == 0: + return S if K == 1: - mi = S - for i in range(len(S)): - S = S[1:]+S[0] - mi = min(mi,S) - return mi + idxes = collections.defaultdict(list) + for i, ch in enumerate(S): + idxes[ch].append(i) + first = min(S) + best = -1 + curmin = "z" * len(S) + for start in idxes[first]: + if S[start:] < curmin: + curmin = S[start:] + best = start + return S[best:] + S[:best] + return "".join(sorted(S)) +__________________________________________________________________________________________________ +sample 36 ms submission +class Solution: + def orderlyQueue(self, S: str, K: int) -> str: + if K > 1: + return ''.join(sorted(S)) else: - return s + minS = S + for i in range(len(S)): + S = S[1:] + S[0] + if S < minS: minS = S + return minS + +__________________________________________________________________________________________________ diff --git a/Python3/900.py b/Python3/900.py index 433d2ad..6579cd8 100644 --- a/Python3/900.py +++ b/Python3/900.py @@ -1,5 +1,62 @@ __________________________________________________________________________________________________ +sample 36 ms submission +class RLEIterator: + def __init__(self, A): + self.A = A + self.pointer = 1 + self.num = 0 + + def next(self, n): + while self.pointer < len(self.A): + if self.num + n > self.A[self.pointer - 1]: + n -= self.A[self.pointer - 1] - self.num + self.num = 0 + self.pointer += 2 + else: + self.num += n + return self.A[self.pointer] + return -1 + +# Your RLEIterator object will be instantiated and called as such: +# obj = RLEIterator(A) +# param_1 = obj.next(n) __________________________________________________________________________________________________ +sample 13432 kb submission +class RLEIterator(object): + + def __init__(self, A): + """ + :type A: List[int] + """#510 + i=0 + # while ilen(A) else i + + + def next(self, n): + """ + :type n: int + :rtype: int + """ + #print(self.i,self.curr,n) + if self.i == -1 or self.i>=len(self.A): + return -1 + if self.curr+n-1 < self.A[self.i]: + self.curr+=n + return self.A[self.i+1] + else: + n=n-(self.A[self.i]-self.curr) + self.i+=2 + self.curr=0 + return self.next(n) + +# Your RLEIterator object will be instantiated and called as such: +# obj = RLEIterator(A) +# param_1 = obj.next(n) __________________________________________________________________________________________________