LC896. Monotonic Array

```python class Solution: def isMonotonic(self, A: List[int]) -> bool: “”” tc: O(N) sc: O(1) main idea: use dec,inc flags to keep track if each element follows monotonic rules “”” dec = inc = True n = len(A) for i in range(1,n): inc &= A[i]>=A[i-1] dec &= A[i] <= A[i-1] return... [Read More]
Tags: Array

LC846. Hand of Straights

```python class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: “”” 0. edge case len = m k 1. cnt freq 2. iterate sorted num, num,num+1, .. num+W-1 freq -= cnt from the most front number 3. if num == 0 but val before num != 0: return False... [Read More]
Tags: Ordered Map

LC945. Minimum Increment to Make Array Unique

```python class Solution: def minIncrementForUnique(self, A: List[int]) -> int: “”” time O(NlgN) space O(1) “”” low = res = 0 for a in sorted(A): if low > a : res += low - a low = max(low + 1, a + 1) return res [Read More]
Tags: Array

697. Degree of an Array

class Solution: def findShortestSubArray(self, nums: List[int]) -> int: """ tc O(N) sc O(M) M: number of unique val 1. find val with max freq 2. find last time idx with val and first time 3. return diff """ mcnt = -1 mval = nums[0] d = {}# val: [cnt,first,last] cnt_val... [Read More]
Tags: Array

LC721. Accounts Merge

```python class Solution: def accountsMerge(self, accounts: List[List[str]]) -> List[List[str]]: “”” DFS way 1. build graph email: acnt, vitsit_acnt set to avoid cycle 2. dfs traverse email and its owner’s other accnt, record path emails and skip visited acnt 3. each time one acnt have done dfs, add sorted emails into... [Read More]
Tags: DFS UF