-
-
Notifications
You must be signed in to change notification settings - Fork 195
/
Copy pathChaedie.py
40 lines (34 loc) ยท 1.12 KB
/
Chaedie.py
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
"""
Solution:
1) ์ํํ๋ฉฐ ์ด์ ๊ฐ์ด ํ์ฌ ๊ฐ๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ๋ค๋ฉด ํ์ฌ ๊ฐ์ด ์ต์๊ฐ์ด๋ค.
2) ๋๊น์ง ๋์๋ ์ต์๊ฐ์ด ์์ ๊ฒฝ์ฐ ์ฒซ๋ฒ์จฐ ๊ฐ์ด ์ต์๊ฐ์ด๋ค.
Time: O(n)
Space: O(1)
"""
class Solution:
def findMin(self, nums: List[int]) -> int:
for i in range(1, len(nums)):
if nums[i - 1] >= nums[i]:
return nums[i]
return nums[0]
"""
Solution:
์๊ฐ ๋ณต์ก๋ O(log n)์ผ๋ก ํ๊ธฐ ์ํด binary search ์ฌ์ฉ
Time: O(log n)
Space: O(1)
"""
def findMin(self, nums: List[int]) -> int:
l, r = 1, len(nums) - 1
while l <= r:
mid = (l + r) // 2
# prev ๊ฐ๋ณด๋ค mid ๊ฐ ์์ผ๋ฉด ์ฐพ๋ ๊ฐ
if nums[mid - 1] > nums[mid]:
return nums[mid]
# mid ๊น์ง ์ ์ ์์๋ฉด ์ฐ์ธก ํ์
if nums[0] < nums[mid]:
l = mid + 1
# mid ๊น์ง ๋น ์ ์ ์์๋ฉด ์ข์ธก ํ์
else:
r = mid - 1
# ๋ชป์ฐพ์ ๊ฒฝ์ฐ ์ ์ฒด ์ ์ ์์ ์ผ์ด์ค
return nums[0]