300. Longest Increasing Subsequence

class Solution: def lengthOfLIS(self, nums: List[int]) -> int: # dp[i]: longest subsequence length ending with nums[i] # time O(N^2) space O(N) # base case dp = [1] * len(nums) for i in range(len(nums)): for j in range(i): if nums[j] < nums[i]: dp[i] = max(dp[i],dp[j] + 1) # get biggest val... [Read More]

974. Subarray Sums Divisible by K

case1: psum = MK case 2 psum[j] - psum[i] == PK ==> psump[i] = MK + a psum[j] = N*K + a, therefore psum[j]- psm[i] = (N-M)K = PK class Solution: def subarraysDivByK(self, A: List[int], K: int) -> int: # time O(N) space O(min(K,N)) cnt = rsum = 0 #... [Read More]

423. Reconstruct Original Digits from English

```python class Solution: def originalDigits(self, s: str) -> str: “”” zero z one o 1-0-2-4 two w three t 3-2-8 four u five f 5-4 six x seven s 7-6 eight g nine i 9-8-6-5 """ # step1 c cnt = [0] * 10 # first even then odd for... [Read More]
Tags: Math

226. Invert Binary Tree

```python “”” Definition for a Node. class Node: def init(self, val: int = 0, left: ‘Node’ = None, right: ‘Node’ = None, next: ‘Node’ = None): self.val = val self.left = left self.right = right self.next = next “”” idea: use queue do level order traverse make sure current node’s... [Read More]
Tags: Tree DFS BFS