Skip to content

Commit 3e7983c

Browse files
committed
Add week 6 solutions : findMinimumInRotatedSortedArray
1 parent 3e99718 commit 3e7983c

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Time Complexity: O(n log n)
2+
// Space Complexity: O(1)
3+
4+
var findMin = function(nums) {
5+
// initialize left pointer.
6+
let left = 0;
7+
// initialize right pointer.
8+
let right = nums.length - 1;
9+
10+
while (left < right) {
11+
// calculate the middle index.
12+
let mid = left + ((right - left) / 2 | 0);
13+
14+
// if the value at the middle index is bigger, move the left pointer to the right.
15+
if (nums[mid] > nums[right]) {
16+
left = mid + 1;
17+
} else { // else, move the right pointer to the middle index.
18+
right = mid;
19+
}
20+
}
21+
22+
// return the element which is the minimum pointed to by the left pointer.
23+
return nums[left];
24+
};

0 commit comments

Comments
 (0)