Skip to content

Commit c985241

Browse files
committed
search-in-rotated-sorted-array sol (py)
1 parent db72d18 commit c985241

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

โ€Žsearch-in-rotated-sorted-array/hi-rachel.pyโ€Ž

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,33 @@
11
"""
22
https://leetcode.com/problems/search-in-rotated-sorted-array/solutions/
33
4-
๋ฌธ์ œ: ํšŒ์ „๋œ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ๋œ ๋ฐฐ์—ด nums์—์„œ target ๊ฐ’์˜ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ผ.
5-
6-
Idea: ์ด์ง„ ํƒ์ƒ‰ ํŒจํ„ด ์‚ฌ์šฉ
4+
- in โ†’ ๋ฆฌ์ŠคํŠธ ์ „์ฒด๋ฅผ ์„ ํ˜• ํƒ์ƒ‰ (O(N))
5+
- index โ†’ ๋‹ค์‹œ ์ฒ˜์Œ๋ถ€ํ„ฐ ์„ ํ˜• ํƒ์ƒ‰ (O(N))
76
8-
TC: O(logN)
7+
Brute Force
8+
TC: O(N)
99
SC: O(1)
1010
"""
1111

1212
from typing import List
1313

14+
15+
class Solution:
16+
def search(self, nums: List[int], target: int) -> int:
17+
if target in nums:
18+
return nums.index(target)
19+
else:
20+
return -1
21+
22+
"""
23+
๋ฌธ์ œ: ํšŒ์ „๋œ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ๋œ ๋ฐฐ์—ด nums์—์„œ target ๊ฐ’์˜ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ผ.
24+
25+
Idea: ์ด์ง„ ํƒ์ƒ‰ ํŒจํ„ด ์‚ฌ์šฉ
26+
27+
28+
TC: O(logN), while ๋ฃจํ”„์—์„œ ๋งค๋ฒˆ ์ ˆ๋ฐ˜์”ฉ ์ค„
29+
SC: O(1)
30+
"""
1431
class Solution:
1532
def search(self, nums: List[int], target: int) -> int:
1633
left, right = 0, len(nums) - 1

0 commit comments

Comments
ย (0)