Skip to content

Latest commit

 

History

History
64 lines (52 loc) · 1.3 KB

File metadata and controls

64 lines (52 loc) · 1.3 KB

Find Minimum in Rotated Sorted Array

Solution 1

Brute Force

/**
 * Question   : 153. Find Minimum in Rotated Sorted Array
 * Complexity : Time: O(n) ; Space: O(1)
 * Topics     : Array
 */
class Solution {
    public int findMin(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        int min = nums[0];
        for (int i = 1; i < nums.length; i++) {
            min = Math.min(min, nums[i]);
        }
        return min;
    }
}

Solution 2

Binary Search

Note: If you compare nums[low] with nums[mid] you won't able to know the smallest number is in the left or right part.

/**
 * Question   : 153. Find Minimum in Rotated Sorted Array
 * Complexity : Time: O(log(n)) ; Space: O(1)
 * Topics     : Array
 */
class Solution {
    public int findMin(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }

        int low = 0;
        int high = nums.length - 1;
        int min = Integer.MAX_VALUE;

        while (low <= high) {
            int mid = low + (high - low) / 2;

            min = Math.min(min, nums[mid]);

            if (nums[mid] < nums[high]) {
                high = mid - 1;
            } else {
                low = mid + 1;
            }
        }

        return min;
    }
}