[Coderust] Find First and Last Position of Element in Sorted Array Problem Solution class Solution: def searchRange(self, nums: List[int], target: int) -> List[int]: # find target with binary search, # then expand until the number changes left = 0 right = len(nums)-1 start = -1 end = -1 while left CODE/Algorithms & Data Structures 2022.02.24
[Coderust] Find Smallest Common Element in All Rows Problem Solution Look at the first number on the first row. Then perform binary search (time complexity = log N) to find that element in the rest of the matrix. If the loop ended before currRow is updated to the row length of matrix, the code did not find the number in the rest of the rows. Move onto the next number in the first row and repeat. Time Complexity O(M+N) maximum Space Complexity O(1.. CODE/Algorithms & Data Structures 2022.02.22
[Coderust] Search in Rotated Sorted Array Problem Solution Time complexity constraint is O(log N) -> perform binary search First determine the how the array rotated, then do the search """ No rotated: 1 2 3 4 5 6 7 mid left rotated: pivot at the left side of the origin sorted array, A[mid] >= A[left] 3 4 5 6 7 1 2 mid search in A[left] ~ A [mid] if A[left] CODE/Algorithms & Data Structures 2022.02.14
[Coderust] Binary Search on Sorted Array Problem Solution class Solution(object): def search(self, nums, target): """ :type nums: List[int] :type target: int :rtype: int """ left = 0 right = len(nums)-1 while left nums[mid]: left = mid + 1 return -1 CODE/Algorithms & Data Structures 2021.10.11