π€ λ¬Έμ
π νμ΄
ν΄λΉ λ¬Έμ λ μ΄μ§ νμμ μ‘°κΈ λ μμ©νμ¬ νμ΄ν μ μλ λ¬Έμ μ λλ€. νΉμ μΈλ±μ€λ₯Ό κΈ°μ€μΌλ‘ rotate λμ΄ κΈ°μ‘΄ λ°©μμΌλ‘ μ΄μ§ νμμ νλ κ²μ΄ λΆκ°λ₯νμ¬ ν λ¨κ³ λ κ³Όμ μ κ±°μ³μΌ λ¬Έμ 쑰건μ λ§κ² νμ΄ν μ μμ΅λλ€.
κ°μ₯ μ²μ μκ°ν λ°©λ²μ λ€μκ³Ό κ°μ΅λλ€.
- rotateκ° λͺ λ² λμλμ§ νμΈνλ€. μ¦, λ°°μ΄μμ κ°μ₯ μμ κ°μ μμΉλ₯Ό μ°Ύλλ€.
- ν΄λΉ μΈλ±μ€λ₯Ό κΈ°μ€μΌλ‘ μ’μΈ‘κ³Ό μ°μΈ‘μ μ λ ¬λμ΄ μλ μνμ΄λ―λ‘ μ΄λ₯Ό λμμΌλ‘ κ°κ° μ΄μ§νμμ νλ€.
- μ’, μ°μΈ‘μ μ΄μ§ νμμ κ²°κ³Όλ‘ λ΅μ ꡬνλ€.
μ λ°©λ²μ΄ μλ μ 체 μ½λμμ λμ¬ νμ΄ 1μ ν΄λΉν©λλ€. μ½λλ₯Ό λΆλΆμ μΌλ‘ μ΄ν΄λ³΄κ² μ΅λλ€.
# λ°°μ΄μμ κ°μ₯ μμ κ°μ΄ μ΄λμλμ§ μ΄μ§ νμ
def find_smallest_num_index(nums):
left, right = 0, len(nums) - 1
while left < right:
mid = (left + right) // 2
if nums[mid] > nums[right]:
left = mid + 1
else:
right = mid
return left
μ μ½λλ₯Ό ν΅ν΄ λ°°μ΄μμ κ°μ₯ μμ κ°μ μΈλ±μ€λ₯Ό μ΄μ§νμ λ°©μμΌλ‘ μ°Ύμ μ μκ² λκ³ μ΄λ 곧 rotateκ° λͺ λ² λμλμ§μ λν μ 보λ₯Ό μ€λλ€.
ν΄λΉ μΈλ±μ€λ₯Ό κΈ°μ€μΌλ‘ μ’μΈ‘κ³Ό μ°μΈ‘μ μ λ ¬λμ΄μμΌλ―λ‘ κΈ°μ‘΄μ μκ³ μλ μ΄μ§ νμμ μ¬μ©ν μ μκ² λ©λλ€. λ°λΌμ μ’μΈ‘κ³Ό μ°μΈ‘ κ°κ° μ΄μ§ νμμ ν΅ν΄ targetμ κ²μνλ©΄ μ λ΅μ ꡬν μ μμ΅λλ€.
μ¬κΈ°κΉμ§κ°,, κ°μ₯ μ²μ μκ°νλ νμ΄ λ°©λ²μ λλ€.
νμ§λ§ rotateλ νμλ₯Ό offsetμΌλ‘ μ¬μ©νμ¬ μ΄μ§νμμμ΄μ§ νμμ νλ€λ©΄ μ’, μ°μΈ‘μΌλ‘ λλμ΄ λ λ² μ΄μ§ νμμ νμ§ μκ³ ν λ²μ μ΄μ§νμμΌλ‘λ μ λ΅μ ꡬν μ μμ΅λλ€.
ν΄λΉ λΆλΆ μ½λλ μλμ κ°μ΅λλ€.
pivot = find_smallest_num_index(nums)
left, right = 0, len(nums) - 1
while left <= right:
mid = (left + right) // 2
mid_with_offset = (mid + pivot) % len(nums)
if nums[mid_with_offset] == target:
return mid_with_offset
elif nums[mid_with_offset] > target:
right = mid - 1
else:
left = mid + 1
return -1
mid_with_offsetμ΄λΌλ mid κ°μ pivot λ§νΌ offsetμ μ€ λ³μλ₯Ό ν΅ν΄ λ§μΉ λ°°μ΄μ΄ νμ λκΈ° μ μ λͺ¨μ΅μ κ°μ§κ³ μ΄μ§ νμμ νλ κ²κ³Ό κ°μ ν¨κ³Όλ₯Ό λΌ μ μμ΅λλ€! LeetCode μ μΆ κ²°κ³Όλ₯Ό 보면 runtimeμ΄ λ°μ κ°λ μ€μ΄λ λͺ¨μ΅μ λ³Ό μ μμ£ .
π» μ 체 νμ΄ μ½λλ μλμ κ°μ΅λλ€.
νμ΄1 - μ’, μ°μΈ‘μ λν΄ κ°κ° μ΄μ§ νμμ μ μ©νλ λ°©μ
class Solution:
def search(self, nums: List[int], target: int) -> int:
def find_smallest_num_index(nums):
left, right = 0, len(nums) - 1
while left < right:
mid = (left + right) // 2
if nums[mid] > nums[right]:
left = mid + 1
else:
right = mid
return left
def binary_search(arr, _target):
left, right = 0, len(arr) - 1
while left <= right:
mid = (left + right) // 2
if arr[mid] == _target:
return mid
elif arr[mid] > _target:
right = mid - 1
else:
left = mid + 1
return -1
smallest_num_index = find_smallest_num_index(nums)
left_nums = nums[:smallest_num_index]
right_nums = nums[smallest_num_index:]
left_idx = binary_search(left_nums, target)
right_idx = binary_search(right_nums, target)
if left_idx != -1:
return left_idx
if right_idx != -1:
return right_idx + smallest_num_index
return -1
νμ΄2 - Offsetμ ν΅ν΄ μ΄μ§ νμμ ν λ²λ§ νλ μ’ λ ν¨μ¨μ μΈ λ°©μ
class Solution:
def search(self, nums: List[int], target: int) -> int:
def find_smallest_num_index(nums):
left, right = 0, len(nums) - 1
while left < right:
mid = (left + right) // 2
if nums[mid] > nums[right]:
left = mid + 1
else:
right = mid
return left
pivot = find_smallest_num_index(nums)
left, right = 0, len(nums) - 1
while left <= right:
mid = (left + right) // 2
mid_with_offset = (mid + pivot) % len(nums)
if nums[mid_with_offset] == target:
return mid_with_offset
elif nums[mid_with_offset] > target:
right = mid - 1
else:
left = mid + 1
return -1
'Algorithm > LeetCode' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[LeetCode] 42. Trapping Rain Water - Python (0) | 2022.04.27 |
---|---|
[LeetCode] 49. Group Anagrams - Python (3) | 2022.04.26 |