DP way
```python
[Read More]
1487. Making File Names Unique
```python class Solution: def getFolderNames(self, names: List[str]) -> List[str]: “”” tc O(N) sc O(N) 1. initialize res, seen as set, a dictionary to record max cnt for key x has appeared 2. each time get an entry, extract cnt, and candidate key x 3. if current candidate key has appeared...
[Read More]
1464. Maximum Product of Two Elements in an Array
class Solution: def maxProduct(self, nums: List[int]) -> int: # O(N) O(1) fst,snd = -1,-1 for x in nums: if fst <= x: # for fst and snd is same snd = fst if fst != -1 else snd # can ignore but somehow will optimize fst = x elif x...
[Read More]
LC320. Generalized Abbreviation
```python class Solution: def generateAbbreviations(self, word: str) -> List[str]: “”” tc O(N*2^N) sc O(N) for path space “”” res = [] def dfs(pos,path, cnt): if pos == len(word): if cnt > 0: path.append(str(cnt)) res.append(''.join(path)) else: # put current cnt into path dfs(pos+1,path+[str(cnt) if cnt else ""] + [word[pos]], 0 )...
[Read More]
1018. Binary Prefix Divisible By 5
Naive solution # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def recoverFromPreorder(self, S: str) -> TreeNode: """ tc O(N) sc O(N) N: len(S) """ level = 0 y...
[Read More]