LC119 Pascal's Triangle II

```python class Solution: time O(N) space O(N) def getRow(self, rowIndex: int) -> List[int]: pre = [] for i in range(rowIndex+1): # i: row cur = [] j = 0 # j: col while j <= i : if j == 0 or j == i: cur.append(1) else: cur.append(pre[j-1]+pre[j]) j +=... [Read More]
Tags: Array

LC274 H-Index

```python class Solution: def hIndex(self, citations: List[int]) -> int: # time O(NlgN) space O(1) # main idea: first sort and compare citations[i] > i , find max i to meet the condition. hence k = i+1 citations.sort()# here if reverse will slow down the speed size = len(citations) i =... [Read More]
Tags: Array

LC171 Excel Sheet Column Number

class Solution: def titleToNumber(self, s: str) -> int: #main idea: # 0 # A : 26*0+1 # 01 # AB : 26^1*1 + 2 # 012 # ABC: 26^2 * 1 + 26^1 * 2 + 3 ==> 26^(size-1-idx) * d[val] # step1: create dictionary, save A-Z as key to... [Read More]
Tags: Math

LC189 Rotate Array

solution 1: class Solution: def rotate(self, nums: List[int], k: int) -> None: # solution 1: create extra space and copy # time O(N) space O(N) if not nums or k<=0: return copy = [] size = len(nums) for i in range(size): copy.append(nums[i]) for j in range(size): nums[(j+k)%size] = copy[j] [Read More]
Tags: Array

LC994 Rotting Oranges

naive solution class Solution: # main idea: count fresh orange ; loop fresh. do BFS to rot oranges by decreasing fresh; count days each loop. each loop check if fresh changes. if not, return -1 # note # time O(h*w*(h+w)) space O(1) def orangesRotting(self, grid: List[List[int]]) -> int: def rot(grid,i,j,days):... [Read More]
Tags: BFS