LC747. Largest Number At Least Twice of Others

two pass class Solution: def dominantIndex(self, A: List[int]) -> int: """ O(N)/O(1) 1. find largetst val and idx 2. compare with all other elements """ # two pass max_v = max_i = 0 for i in range(len(A)): if A[i] > max_v: max_v = A[i] max_i = i for a in... [Read More]
Tags: Array

LC561. Array Partition I

nums class Solution: def arrayPairSum(self, nums: List[int]) -> int: nums.sort() return sum(nums[::2])
Tags: Array

LC448. Find All Numbers Disappeared in an Array

class Solution: def findDisappearedNumbers(self, nums: List[int]) -> List[int]: """ O(N)/O(1) two loops 1. iterate throught each num, mark nums[num-1] as negtive 2. loop through the array, the positions where numbers are positive is the missing numbers instead of using negtive sign to mark numbers at position i already existed, can... [Read More]
Tags: Array

LC268. Missing Number

```python class Solution: def missingNumber(self, A: List[int]) -> int: “”” O(N) /O(1) 1. get expected sum if A is sequence 2. cnt actual sum 3. the one missing is difference """ n = len(A) total = n*(n+1)//2 actual = sum(A) return total - actual [Read More]