398. Random Pick Index

Follow up: Stream input solution ```python class Solution: def init(self, nums: List[int]): self.A = nums def pick(self, target: int) -> int: “”” tc O(N) sc O(1) use cnt to record by index i, the amount of number target if we choose first number as sample position, by array[:i+1] the probability... [Read More]

307. Range Sum Query - Mutable

static@method v.s. classmethod v.s. instanc emethod ```python class SegmentTreeNode(object): def init(self, s, e): self.start = s # start end point self.end = e # end endpoint self.total = 0 # total sum within range [start, end] inclusive self.left = None # left subtree self.right = None # right substree @staticmethod... [Read More]

1796. Second Largest Digit in a String

```python class Solution: def secondHighest(self, s: str) -> int: “"”tc O(N) sc O(1) digits : 0 - 9 “”” fst, snd = -1, -1 for c in s: if c.isdigit(): x = int(c) if fst < x : fst,snd = x, fst elif snd < x and x < fst:... [Read More]

500. Keyboard Row

Set version : when we do set(string) = > python will automatically flatten them into single character Besides, for one word if its characters are in one row, it can be consider one row’s subset , hence set_w <= line1 [Read More]