|
| 1 | +# Search in Rotated Sorted Array |
| 2 | + |
| 3 | +Page on leetcode: https://leetcode.com/problems/search-in-rotated-sorted-array/ |
| 4 | + |
| 5 | +## Problem Statement |
| 6 | + |
| 7 | +There is an integer array nums sorted in ascending order (with distinct values). |
| 8 | + |
| 9 | +Prior to being passed to your function, nums is possibly rotated at an unknown pivot index k (1 <= k < nums.length) such that the resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-indexed). For example, [0,1,2,4,5,6,7] might be rotated at pivot index 3 and become [4,5,6,7,0,1,2]. |
| 10 | + |
| 11 | +Given the array nums after the possible rotation and an integer target, return the index of target if it is in nums, or -1 if it is not in nums. |
| 12 | + |
| 13 | +You must write an algorithm with O(log n) runtime complexity. |
| 14 | + |
| 15 | +### Constraints |
| 16 | + |
| 17 | +- 1 <= nums.length <= 5000 |
| 18 | +- -104 <= nums[i] <= 104 |
| 19 | +- All values of nums are unique. |
| 20 | +- nums is an ascending array that is possibly rotated. |
| 21 | +- -104 <= target <= 104 |
| 22 | + |
| 23 | +### Example |
| 24 | + |
| 25 | +``` |
| 26 | +Input: nums = [4,5,6,7,0,1,2], target = 0 |
| 27 | +Output: 4 |
| 28 | +``` |
| 29 | + |
| 30 | +## Solution |
| 31 | + |
| 32 | +- return index of target |
| 33 | +- -1 if no target found |
| 34 | +- Must be O(logn) |
| 35 | +- [15, 20, 23, 40, 78] |
| 36 | +- [40, 78, 15, 20, 23] |
| 37 | + |
| 38 | +### Pseudocode |
| 39 | + |
| 40 | +1. Check if end is less than start, if so it has been rotated |
| 41 | +2. If not rotated use binary search (find mid, check mid, iteratively) |
| 42 | +3. If rotated check if target is less than end, if true then check in second part of array, if not check in first part |
| 43 | +4. Check second part, move start to mid point, check if mid is less than target, if less then proceed with binary search |
| 44 | +5. If greater than, move start to midpoint between 0-index and start |
| 45 | + |
| 46 | +### Initial Attempt |
| 47 | + |
| 48 | +```javascript |
| 49 | +const search = function (nums, target) { |
| 50 | + let start = 0; |
| 51 | + let end = nums.length - 1; |
| 52 | + |
| 53 | + if (nums[end] > sums[start]) { |
| 54 | + return binarySearch(start, end); |
| 55 | + } |
| 56 | + |
| 57 | + // Find which part of rotated array the target is in |
| 58 | + if (target < nums[end]) { |
| 59 | + start = start + (end - start) / 2; |
| 60 | + return findSortedArray(start, end); |
| 61 | + } else { |
| 62 | + end = start + (end - start) / 2; |
| 63 | + findSortedArray(start, end); |
| 64 | + } |
| 65 | + |
| 66 | + function findSortedArray(l, r) { |
| 67 | + // Proceed with binary search |
| 68 | + if (l < target && r > target) { |
| 69 | + return binarySearch(l, r); |
| 70 | + } |
| 71 | + |
| 72 | + if (l > target && l !== 0) { |
| 73 | + l = l / 2; |
| 74 | + } else if (r < target && r !== nums.length - 1) { |
| 75 | + r = r / 2; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + // binary search helper function |
| 80 | + function binarySearch(l, r) { |
| 81 | + while (l < r) { |
| 82 | + // update mid each loop |
| 83 | + const mid = l + (r - l) / 2; |
| 84 | + if (nums[mid] > target) { |
| 85 | + r = mid - 1; |
| 86 | + } else if (nums[mid] < target) { |
| 87 | + l = mid + 1; |
| 88 | + } else { |
| 89 | + return mid; |
| 90 | + } |
| 91 | + } |
| 92 | + // target not found |
| 93 | + return -1; |
| 94 | + } |
| 95 | +}; |
| 96 | +``` |
| 97 | + |
| 98 | +### Optimized Solution |
| 99 | + |
| 100 | +This solution has a time complexity of O(logn) and a space complexity of O(1). You can see an explanation of the solution here: https://www.youtube.com/watch?v=U8XENwh8Oy8 |
| 101 | + |
| 102 | +```javascript |
| 103 | +const search = function (nums, target) { |
| 104 | + let l = 0; |
| 105 | + let r = nums.length - 1; |
| 106 | + |
| 107 | + while (l <= r) { |
| 108 | + // update mid each loop |
| 109 | + const mid = Math.floor((l + r) / 2); |
| 110 | + |
| 111 | + // target found |
| 112 | + if (nums[mid] === target) { |
| 113 | + return mid; |
| 114 | + } |
| 115 | + |
| 116 | + // mid point is on the left sorted portion of the array |
| 117 | + if (nums[l] <= nums[mid]) { |
| 118 | + if (target > nums[mid] || target < nums[l]) { |
| 119 | + l = mid + 1; |
| 120 | + } else { |
| 121 | + r = mid - 1; |
| 122 | + } |
| 123 | + } else { |
| 124 | + // mid points is on the right sorted portion of the array |
| 125 | + if (target < nums[mid] || target > nums[r]) { |
| 126 | + r = mid - 1; |
| 127 | + } else { |
| 128 | + l = mid + 1; |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + // target not found |
| 134 | + return -1; |
| 135 | +}; |
| 136 | +``` |
0 commit comments