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]
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]
1010. Pairs of Songs With Total Durations Divisible by 60
One round
class Solution:
def numPairsDivisibleBy60(self, time: List[int]) -> int:
d = [0]*60
res = 0
for t in time:
rem = t%60
res += d[(60-rem%60)%60] # rem =0
d[rem] += 1
return res
[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]
295. Find Median from Data Stream
```python
class MedianFinder:
[Read More]