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]
1920. Build Array from Permutation
Brute Force with O(N) space
class Solution:
def buildArray(self, nums: List[int]) -> List[int]:
"""tc O(N) sc O(N)
"""
n = len(nums)
A = [None]*n
for i,x in enumerate(nums):
A[i] = nums[x]
return A
[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]