Skip to content

Commit cd49973

Browse files
committed
add solution: find-minimum-in-rotated-sorted-array
1 parent cc32a15 commit cd49973

File tree

1 file changed

+30
-0
lines changed
  • find-minimum-in-rotated-sorted-array

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'''
2+
Docstring for find-minimum-in-rotated-sorted-array.ZetBe
3+
๋ฌธ์ œ: ํšŒ์ „๋œ ์ •๋ ฌ ๋ฐฐ์—ด์—์„œ ์ตœ์†Ÿ๊ฐ’์„ ์ฐพ์œผ์‹œ์˜ค.
4+
ํ’€์ด: ์ด์ง„ ํƒ์ƒ‰์„ ์‚ฌ์šฉํ•˜์—ฌ ํšŒ์ „๋œ ๋ฐฐ์—ด์—์„œ ์ตœ์†Ÿ๊ฐ’์„ ํšจ์œจ์ ์œผ๋กœ ์ฐพ์Šต๋‹ˆ๋‹ค.
5+
์‹œ๊ฐ„ ๋ณต์žก๋„: O(log n), n์€ ๋ฐฐ์—ด์˜ ๊ธธ์ด์ž…๋‹ˆ๋‹ค. ์ด์ง„ ํƒ์ƒ‰์„ ์‚ฌ์šฉํ•˜์—ฌ ์ ˆ๋ฐ˜์”ฉ ํƒ์ƒ‰ ๋ฒ”์œ„๋ฅผ ์ค„์ด๋ฏ€๋กœ ์ „์ฒด ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(log n)์ž…๋‹ˆ๋‹ค.
6+
๊ณต๊ฐ„ ๋ณต์žก๋„: O(1), ์ถ”๊ฐ€์ ์ธ ๊ณต๊ฐ„์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ๊ณต๊ฐ„ ๋ณต์žก๋„๋Š” O(1)์ž…๋‹ˆ๋‹ค.
7+
์‚ฌ์šฉํ•œ ์ž๋ฃŒ๊ตฌ์กฐ: ๋ฐฐ์—ด
8+
์ถ”๊ฐ€๋กœ, while๋ฌธ ๋‚ด๋ถ€์—์„œ ๋ฌด์กฐ๊ฑด ๋ฆฌํ„ดํ•˜๊ธฐ ๋–„๋ฌธ์—, while๋ฌธ ์ดํ›„ ๋„๋‹ฌํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ์—†๋‹ค.
9+
'''
10+
11+
12+
class Solution:
13+
def findMin(self, nums: List[int]) -> int:
14+
n = len(nums)
15+
if nums[0] <= nums[n-1]:
16+
return nums[0]
17+
18+
r, l = 0, n-1
19+
20+
while r < l:
21+
now = (r+l)//2
22+
if now < n-1 and nums[now] > nums[now+1]:
23+
return nums[now+1]
24+
25+
if now < n-1 and nums[0] > nums[now] <= nums[now+1]:
26+
l = now
27+
elif now < n-1 and nums[0] <= nums[now] <= nums[now+1]:
28+
r = now
29+
30+

0 commit comments

Comments
ย (0)