Skip to content

Commit 66c3e3a

Browse files
committed
find-minimum-in-rotated-sorted-array solution
1 parent 090ccc5 commit 66c3e3a

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var findMin = function (nums) {
6+
while (1 < nums.length) {
7+
8+
const mid = Math.floor(nums.length / 2);
9+
10+
const firstToHalf = [...nums].slice(0, mid);
11+
12+
const midToEnd = [...nums].slice(mid, nums.length);
13+
14+
const isInFront = Math.min(...firstToHalf) < Math.min(...midToEnd);
15+
16+
nums = isInFront ? firstToHalf : midToEnd;
17+
}
18+
19+
return nums[0];
20+
};
21+
22+
// 시간복잡도 0(logn) -> 이진탐색을 통해 배열 nums를 반으로 쪼개가며 해답을 찾기때문에
23+
// 공간복잡도 O(n) -> 처음 while문이 돌 때, nums를 쪼갠 두 배열 총 길이의 합은 nums의 처음 길이와 같기때문에

0 commit comments

Comments
 (0)