DEV Community

Ertugrul
Ertugrul

Posted on

πŸ—“ Daily LeetCode Progress – Day 9

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] with nums[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]
Enter fullscreen mode Exit fullscreen mode

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};
    }
};
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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]
Enter fullscreen mode Exit fullscreen mode

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];
    }
};
Enter fullscreen mode Exit fullscreen mode

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++)

fflp python

fflp python

  • Find Minimum in Rotated Sorted Array (Python & C++)

Joke but accepted
fm python1

real solution
fm python1

fm cpp


πŸ“¦ 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 first min(nums) attempt was O(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

Top comments (0)