|
4 | 4 | **
|
5 | 5 | * Suppose an array of length n sorted in ascending order is rotated between 1 and n times.
|
6 | 6 | * For example, the array nums = [0,1,2,4,5,6,7] might become:
|
7 |
| - * [4,5,6,7,0,1,2] if it was rotated 4 times. |
8 |
| - * [0,1,2,4,5,6,7] if it was rotated 7 times. |
| 7 | + * • [4,5,6,7,0,1,2] if it was rotated 4 times. |
| 8 | + * • [0,1,2,4,5,6,7] if it was rotated 7 times. |
9 | 9 | *
|
10 |
| - * Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time |
11 |
| - * results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]]. |
| 10 | + * Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] |
| 11 | + * 1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]]. |
12 | 12 | *
|
13 |
| - * Given the sorted rotated array nums of unique elements, return the minimum element of this array. |
| 13 | + * Given the sorted rotated array nums of unique elements, |
| 14 | + * return the minimum element of this array. |
14 | 15 | *
|
15 | 16 | * You must write an algorithm that runs in O(log n) time.
|
| 17 | + * |
| 18 | + * Example 1: |
| 19 | + * Input: nums = [3,4,5,1,2] |
| 20 | + * Output: 1 |
| 21 | + * Explanation: The original array was [1,2,3,4,5] rotated 3 times. |
| 22 | + * |
| 23 | + * Example 2: |
| 24 | + * Input: nums = [4,5,6,7,0,1,2] |
| 25 | + * Output: 0 |
| 26 | + * Explanation: The original array was [0,1,2,4,5,6,7] and it was rotated 4 times. |
| 27 | + * |
| 28 | + * Example 3: |
| 29 | + * Input: nums = [11,13,15,17] |
| 30 | + * Output: 11 |
| 31 | + * Explanation: The original array was [11,13,15,17] and it was rotated 4 times. |
| 32 | + * |
| 33 | + * Constraints: |
| 34 | + * • n == nums.length |
| 35 | + * • 1 <= n <= 5000 |
| 36 | + * • -5000 <= nums[i] <= 5000 |
| 37 | + * • All the integers of nums are unique. |
| 38 | + * • nums is sorted and rotated between 1 and n times. |
| 39 | + * |
| 40 | + * Hint 1: |
| 41 | + * Array was originally in ascending order. |
| 42 | + * Now that the array is rotated, |
| 43 | + * there would be a point in the array where |
| 44 | + * there is a small deflection from the increasing sequence.eg. |
| 45 | + * The array would be something like [4, 5, 6, 7, 0, 1, 2]. |
| 46 | + * |
| 47 | + * Hint 2: |
| 48 | + * You can divide the search space into two and see which direction to go. |
| 49 | + * Can you think of an algorithm which has O(logN) search complexity? |
| 50 | + * |
| 51 | + * Hint 3: |
| 52 | + * 1. All the elements to the left of inflection point > first element of the array. |
| 53 | + * 2. All the elements to the right of inflection point < first element of the array. |
16 | 54 | **
|
17 | 55 | * https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/
|
18 | 56 | ***/
|
|
0 commit comments