Problems Solved:
- #34 Find First and Last Position of Element in Sorted Array
- #153 Find Minimum in Rotated Sorted Array
This continues my daily series (Day 9: Binary Search variations). Today I focused on two key search problems: locating the first and last occurrence of a target in a sorted array, and finding the minimum element in a rotated sorted array. Both are implemented in Python and C++.
π‘ What I Learned
Todayβs focus was on binary search boundary conditions:
- For Find First and Last Position, the trick is to perform two binary searches: one biased to the left (first occurrence) and one biased to the right (last occurrence).
- For Find Minimum in Rotated Sorted Array, we leverage the property that at least one half of the array is sorted, comparing
nums[mid]
withnums[right]
to shrink the search space. - Funny discovery: I first tried
return min(nums)
in Python and it actually passed π β but thatβs O(n), not the intended O(log n). - Practiced how careful pointer updates (
left
,right
) decide correctness at the edges.
π§© #34 β Find First and Last Position of Element in Sorted Array
Python (Two binary searches)
class Solution:
def searchRange(self, nums: List[int], target: int) -> List[int]:
left, right = 0, len(nums) - 1
start, end = -1, -1
if not nums:
return [-1, -1]
# First occurrence
while left <= right:
mid = (left + right) // 2
if nums[mid] >= target:
right = mid - 1
else:
left = mid + 1
if nums[mid] == target:
start = mid
if start == -1:
return [-1, -1]
# Last occurrence
left, right = 0, len(nums) - 1
while left <= right:
mid = (left + right) // 2
if nums[mid] <= target:
left = mid + 1
else:
right = mid - 1
if nums[mid] == target:
end = mid
return [start, end]
C++ (Two binary searches)
class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
if (nums.empty()) return {-1, -1};
int n = nums.size();
int start = -1, end = -1;
int left = 0, right = n - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] >= target) {
right = mid - 1;
} else {
left = mid + 1;
}
if (nums[mid] == target) start = mid;
}
if (start == -1) return {-1, -1};
left = 0; right = n - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] <= target) {
left = mid + 1;
} else {
right = mid - 1;
}
if (nums[mid] == target) end = mid;
}
return {start, end};
}
};
Why it works:
One binary search forces the boundary leftward, the other forces it rightward. Together they capture the full target range.
Time: O(log n)
Space: O(1)
π§© #153 β Find Minimum in Rotated Sorted Array
Python (First attempt β surprisingly passed π )
class Solution:
def findMin(self, nums: List[int]) -> int:
# I first tried this almost as a joke...
return min(nums) # O(n), works but not optimal
Python (Correct binary search solution)
class Solution:
def findMin(self, nums: List[int]) -> int:
left, right = 0, len(nums) - 1
while left < right:
mid = (left + right) // 2
if nums[mid] > nums[right]:
left = mid + 1
else:
right = mid
return nums[left]
C++ (Binary search)
class Solution {
public:
int findMin(vector<int>& nums) {
int left = 0, right = nums.size() - 1;
while (left < right) {
int mid = (left + right) / 2;
if (nums[mid] > nums[right]) {
left = mid + 1;
} else {
right = mid;
}
}
return nums[left];
}
};
Why it works:
We always shrink towards the unsorted part. If nums[mid] > nums[right]
, minimum is to the right; otherwise itβs at mid
or left of it.
Time: O(log n)
Space: O(1)
πΈ Achievements
- Find First and Last Position (Python & C++)
- Find Minimum in Rotated Sorted Array (Python & C++)
π¦ Complexity Recap
-
Find First and Last Position:
O(log n)
time,O(1)
space -
Find Minimum in Rotated Sorted Array:
O(log n)
time,O(1)
space (though my firstmin(nums)
attempt wasO(n)
π )
π£ Join the Journey
Iβm solving and documenting problems daily in both Python and C++, highlighting binary search patterns and their tricky edge cases. Follow along if youβre into algorithms and optimization.
Links
- LinkedIn: https://www.linkedin.com/in/ertugrul-mutlu
- GitHub Series: https://github.com/Ertugrulmutlu/leetcode-series
Top comments (0)