# time O(N) space O(1) class Solution: def fizzBuzz(self, n: int) -> List[str]: res = [] for i in range(n): i = i +1 if i %5 == 0 and i %3 == 0: res.append('FizzBuzz') elif i % 3 == 0 : res.append('Fizz') elif i %5 == 0: res.append('Buzz') else:...
[Read More]
LC40 Combination Sum II
class Solution: # time O(2^N) space O(1) def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]: res = [] candidates.sort() size = len(candidates) def dfs(start, target, path): if target < 0: return if target == 0: res.append(path[:]) for i in range(start,size): if candidates[i] == candidates[i-1] and i > start: continue target...
[Read More]
LC39 Combination Sum
class Solution: # time O(2^N) space O(1) def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: res = [] candidates.sort() # I. this is optional, to optimize speed def dfs(start,target,path): if target < 0: # to optimize, can put this check in for loop and if condition applied, break (assuming sorted)...
[Read More]
LC216 Combination Sum III
class Solution: #time O(2^N) space O(1) def combinationSum3(self, k: int, n: int) -> List[List[int]]: nums = [i for i in range(1,10)] res = [] def dfs(start,target,path): if target < 0 or len(path) > k: return if target == 0 and len(path) == k: res.append(path[:]) return for i in range(start,len(nums)): target...
[Read More]
LC983 Minimum Cost For Tickets
```python from functools import lru_cache class Solution: # time O(W) W: max travelrable days, 365. def mincostTickets(self, days: List[int], costs: List[int]) -> int: duration = [1,7,30] @lru_cache(None) def dp(i): if i > 365: return 0 elif i in days: return min(dp(i+d) + c for c,d in zip(costs, duration)) else: return...
[Read More]