KarmaCoder Day 1 LC704/27/977
1️⃣ Binary search Link: lc704
Key points:
- The principle is the range definition, either a double-closed range [A, B] or a left-closed right-open range [A, B)
- the while loop condition depends on the range definition:
- double-closed:
left <= rightis valid - left-closed right open:
left < rightis valid.
- double-closed:
- the if-else statement depends on the range definition:
- double-closed: the updated left or right does not include mid
- left-closed right open: the updated right can include mid
- Similarly, right is initialized to numsSize - 1 or numsSize also depends on the range definition.
Time complexity: O(logn) Space complexity: O(1)
def search(self, nums: List[int], target: int) -> int:
left = 0
right = len(nums)
while left < right:
mid = (left + right) // 2
if target < nums[mid]:
right = mid
elif target > nums[mid]:
left = mid + 1
else:
return mid
return -1
2️⃣ Remove element Link: lc27 Key points:
- Use two pointers, fast and slow.
- Fast pointer points to the to-be-recorded element in the original array
- Slow pointer points to the new location for the to-be-recorded element.
Time complexity: O(n) Space complexity: O(1)
def removeElement(self, nums: List[int], val: int) -> int:
slow = 0
for i in range(len(nums)):
if nums[i] != val:
nums[slow] = nums[i]
slow += 1
return slow
3️⃣ Squares of a sorted array Link: lc977 Key points:
- Use two pointers, head and tail.
- A third pointer writes the larger data between head and tail pointers into the result array
def sortedSquares(self, nums: List[int]) -> List[int]:
i = 0
j = len(nums) - 1
k = j
sorted_nums = [0.0] * len(nums)
while i <= j:
if nums[i] * nums[i] > nums[j] * nums[j]:
sorted_nums[k] = nums[i] * nums[i]
i += 1
else:
sorted_nums[k] = nums[j] * nums[j]
j -= 1
k -= 1
return sorted_nums