Skip to content

Commit ff09514

Browse files
committed
Add solution for Find Minimum in Rotated Sorted Array using binary search
1 parent 65e96d0 commit ff09514

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
# Intuition
3+
이진탐색으로 찾는다.
4+
5+
# Complexity
6+
- Time complexity: nums의 길이 N일때, O(logN)
7+
8+
- Space complexity: 재귀스택 O(logN)
9+
"""
10+
11+
12+
class Solution:
13+
def findMin(self, nums: list[int]) -> int:
14+
l, m, r = 0, len(nums) // 2, len(nums) - 1
15+
16+
def findMinimum(l, m, r):
17+
if l == m or r == m:
18+
return min(nums[l], nums[r])
19+
20+
# 오른쪽 구간
21+
if nums[m] > nums[r]:
22+
return findMinimum(m, (m + r) // 2, r)
23+
24+
# 왼쪽 구간
25+
return findMinimum(l, (l + m) // 2, m)
26+
27+
return findMinimum(l, m, r)

0 commit comments

Comments
 (0)