1268. Search Suggestions System

class Solution: def suggestedProducts(self, words: List[str], query: str) -> List[List[str]]: """ tc O(NlgN+L^2*lg(N)) sc O(L) L: len(query) 1. sort the dictionary 2. iterate query to concatenate prefix of query 3. for each prefix, find the index where everything previously is smaller than current prefix 4. start from current index up... [Read More]
Tags: Array String Trie

Lc829

def consecutiveNumbersSum(self, N): res = 1 i = 3 while N % 2 == 0: N /= 2 while i * i <= N: count = 0 while N % i == 0: N /= i count += 1 res *= count + 1 i += 2 return res if... [Read More]

973. K Closest Points to Origin

class Solution: def kClosest(self, points: List[List[int]], k: int) -> List[List[int]]: """ main idea: keep a max heap size of k, push each point into it with distance to origin and its coordinate tc O(NlgK) sc O(K) """ pq = [] for x,y in points: heapq.heappush(pq,(-x*x-y*y,x,y)) if len(pq)> k: heapq.heappop(pq) return... [Read More]