-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind-minimum-in-rotated-sorted-array.js
More file actions
49 lines (39 loc) · 1.08 KB
/
find-minimum-in-rotated-sorted-array.js
File metadata and controls
49 lines (39 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* Problem: Find Minimum in Rotated Sorted Array
* Link: https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/
* Difficulty: Medium
*
* Find the minimum element in a rotated sorted array (no duplicates).
*
* Example: nums = [3,4,5,1,2] => 1
*
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
// JavaScript Solution - Binary Search
function findMin(nums) {
let lo = 0, hi = nums.length - 1;
while (lo < hi) {
const mid = Math.floor((lo + hi) / 2);
if (nums[mid] > nums[hi]) {
// Minimum is in the right half (rotation point is there)
lo = mid + 1;
} else {
// Minimum is in the left half (including mid)
hi = mid;
}
}
return nums[lo]; // lo === hi, pointing to the minimum
}
module.exports = findMin;
/* Python Solution:
def findMin(nums):
lo, hi = 0, len(nums) - 1
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] > nums[hi]:
lo = mid + 1 # min is in right half
else:
hi = mid # min is in left half (including mid)
return nums[lo]
*/