
π‘ μ΄μ§ νμ μκ³ λ¦¬μ¦
μμ°¨ νμ : μμμλΆν° νλμ© νμΈ.
μ΄μ§ νμ : μ λ ¬λμ΄ μλ 리μ€νΈμμ νμ λ²μ μ λ°μ© μ’νκ°λ©΄μ νμ. μμμ , λμ , μ€κ°μ μ΄μ©.
βοΈμμ [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]μμ 4 μ°ΎκΈ°
1. μμμ [0], λμ [9], μ€κ°μ [4] (μμμ μ΄ν μ κ±°)
| 0 | 2 | 4 | 6 | 8 | 10 | 12 | 14 | 16 | 18 |
| [0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9] |
μ€κ°μ κΈ°μ€μΌλ‘ μΌμͺ½μ νμν μ§, μ€λ₯Έμͺ½μ νμν μ§ κ³ λ₯΄κΈ° : μ°Ύλ κ° 4κ° 8λ³΄λ€ μκΈ° λλ¬Έμ μΌμͺ½μΌλ‘
2. μμμ [0], λμ [3], μ€κ°μ [1]
| 0 | 2 | 4 | 6 | ||||||
| [0] | [1] | [2] | [3] |
μ°Ύλ κ° 4κ° μ€κ°κ° 2λ³΄λ€ ν¬κΈ° λλ¬Έμ μ€λ₯Έμͺ½μΌλ‘
3. μμμ [2], λμ [3], μ€κ°μ [2]
| 4 | 6 | ||||||||
| [2] | [3] |
-> μκ°λ³΅μ‘λ O(logN)
π‘ νμ΄μ¬ μ½λ : μ¬κ·μ ꡬν
μ€κ°κ°κ³Ό λΉκ΅νμ¬ μμ μΈλ±μ€ λλ λ μΈλ±μ€λ₯Ό λ°κΏ μ¬κ·μ μΌλ‘ ꡬν
βοΈ μ½λ μ€λͺ
def μ΄μ§νμ ν¨μ (λ°°μ΄, μ°Ύλ κ°, μμ μΈλ±μ€, λ μΈλ±μ€):
if μμ μΈλ±μ€ > λ μΈλ±μ€ : None
μ€κ° μΈλ±μ€ = ( μμ + λ ) // 2
if μ€κ° κ° == μ°Ύλ κ° : μ€κ° μΈλ±μ€ λ°ν
elif μ°Ύλ κ°μ΄ μ€κ° κ°λ³΄λ€ μμΌλ©΄ : μ¬κ·(μμ ~ μ€κ°-1)
else : μ¬κ·(μ€κ°+1 ~ λ)
μ λ ₯λ°κΈ° - μμ κ°μ(λ μΈλ±μ€), μ°Ύμ κ°, μ 체 μμ 리μ€νΈ
μΆλ ₯κ° = μ΄μ§νμ ν¨μ(μ λ ₯λ°μ κ°)
print(λͺλ²μ§Έ μΈλ±μ€μΈμ§)
βοΈ μ½λ ꡬν
def binary_search (array, target, start, end):
if (start > end): return None
mid = (start + end) // 2
if array[mid] == target:
return mid
elif array[mid] > target:
return binary_search(array, target, start, mid-1)
else:
return binary_search(array, target, mid+1, end)
n, target = map(int, input().split())
array = list(map(int, input().split()))
result = binary_search(array, target, 0, n-1)
if result == None:
print("μ°Ύλ κ²°κ³Όκ° μμ΅λλ€.")
else :
print("ν΄λΉ κ°μ {}λ²μ§Έ μμΉμ μμ΅λλ€.".format(result+1))
| μ λ ₯ | μΆλ ₯ |
| 10 7 1 3 5 7 9 11 13 |
ν΄λΉ κ°μ {}λ²μ§Έ μμΉμ μμ΅λλ€. |
| 10 7 1 3 5 6 9 11 13 |
μ°Ύλ κ²°κ³Όκ° μμ΅λλ€. |
π‘ νμ΄μ¬ μ½λ : λ°λ³΅λ¬Έ ꡬν
whileλ¬Έ μ¬μ©νμ¬ μμ μΈλ±μ€κ° λ μΈλ±μ€λ³΄λ€ μμ κ²½μ°μ λ°λ³΅ μ€ν
βοΈ μ½λ μ€λͺ
def μ΄μ§νμ ν¨μ (λ°°μ΄, μ°Ύλ κ°, μμ μΈλ±μ€, λ μΈλ±μ€):
while μμ μΈλ±μ€ <= λ μΈλ±μ€:
μ€κ° μΈλ±μ€ = (μμ μΈλ±μ€ + λ μΈλ±μ€) // 2
if μ€κ° κ° == μ°Ύλ κ° : μ€κ° μΈλ±μ€ λ°ν
elif μ€κ° κ° < μ°Ύλ κ° : λ μΈλ±μ€ = μ€κ° μΈλ±μ€ - 1
else : μμ μΈλ±μ€ = μ€κ° μΈλ±μ€ + 1
λͺ»μ°ΎμΌλ©΄ None
μ λ ₯λ°κΈ° - μμ κ°μ(λ μΈλ±μ€), μ°Ύμ κ°, μ 체 μμ 리μ€νΈ
μΆλ ₯κ° = μ΄μ§νμ ν¨μ(μ λ ₯λ°μ κ°)
print(λͺλ²μ§Έ μΈλ±μ€μΈμ§)
βοΈ μ½λ ꡬν
def binary_search (array, target, start, end):
while (start <= end):
mid = (start + end) // 2
if array[mid] == target:
return mid
elif array[mid] > target:
end = mid - 1
else:
start = mid + 1
return None
n, target = map(int, input().split())
array = list(map(int, input().split()))
result = binary_search(array, target, 0, n-1)
if result == None:
print("μ°Ύλ κ²°κ³Όκ° μμ΅λλ€.")
else :
print("ν΄λΉ κ°μ {}λ²μ§Έ μμΉμ μμ΅λλ€.".format(result+1))
| μ λ ₯ | μΆλ ₯ |
| 10 7 1 3 5 7 9 11 13 |
ν΄λΉ κ°μ {}λ²μ§Έ μμΉμ μμ΅λλ€. |
| 10 7 1 3 5 6 9 11 13 |
μ°Ύλ κ²°κ³Όκ° μμ΅λλ€. |
π‘ biscect λΌμ΄λΈλ¬λ¦¬
bisect_left(λ°°μ΄, μ«μ) : μ λ ¬λ μμ μ μ§νλ©΄μ λ°°μ΄ aμ xλ₯Ό μ½μΌν κ°μ₯ μΌμͺ½ μΈλ±μ€
bisect_right(λ°°μ΄, μ«μ) : μ λ ¬λ μμ μ μ§νλ©΄μ λ°°μ΄ aμ xλ₯Ό μ½μΌν κ°μ₯ μ€λ₯Έμͺ½ μΈλ±μ€
| 1 | 2 | 4 | 4 | 8 |
| [0] | [1] | [2] | [3] | [4] |
μμ 리μ€νΈμ ννλ₯Ό λ°°μ΄ aλΌκ³ ν λ, bisect_left(a, 4)λ 2, bisect_right(a, 4)λ 4
βοΈ μ½λ ꡬν
from bisect import bisect_left, bisect_right
a = [1, 2, 4, 4, 8]
x = 4
print(bisect_left(a, x)) #2
print(bisect_right(a, x)) #4
π‘ κ°μ΄ νΉμ λ²μμ μνλ λ°μ΄ν° κ°μ ꡬνκΈ°
βοΈ μ½λ μ€λͺ
def κ°μ λ°ν ν¨μ (λ°°μ΄, λ²μ μ²μ μ, λ²μ λ§μ§λ§ μ):
μ€λ₯Έμͺ½ μΈλ±μ€ = biscect_right(λ°°μ΄, right_value)
μΌμͺ½ μΈλ±μ€ = biscect_left(λ°°μ΄, right_left)
return μ€λ₯Έμͺ½ μΈλ±μ€ - μΌμͺ½ μΈλ±μ€
λ°°μ΄ μ μΈ
print(κ°μ λ³ν ν¨μ(λ°°μ΄, λ²μ μ²μ μ, λ²μ λ§μ§λ§ μ)
βοΈ μ½λ ꡬν
from bisect import bisect_left, bisect_right
def count_by_range (array, left_value, right_value):
right_index = bisect_right(array, right_value)
left_index = bisect_left(array, left_value)
return right_index - left_index
array = [1, 2, 3, 3, 3, 3, 4, 4, 8, 9]
print(count_by_range(array, 4, 4)) #2
print(count_by_range(array, -1, 3)) #6
'Algorithm > Python' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
| [Python] μλ£κ΅¬μ‘° - μ€ν, ν (0) | 2024.03.12 |
|---|---|
| [Python] BFS μκ³ λ¦¬μ¦ (0) | 2023.12.20 |
| [Python] μ±μ νκ· - μ½λ©ν μ€νΈ λ¬Έμ (4) | 2023.11.20 |
| [Python] μ λ ¬ μκ³ λ¦¬μ¦ - μ ν, μ½μ , ν΅, κ³μ (2) | 2023.11.15 |
| [Python] DFS μκ³ λ¦¬μ¦ (0) | 2023.11.13 |