πŸ’»μ½”λ”©ν…ŒμŠ€νŠΈ λ¬Έμ œν’€μ΄9

μ§€λ―Όμ„œΒ·2023λ…„ 3μ›” 13일
0

coding test

λͺ©λ‘ 보기
9/30

Chapter8. 이진 탐색

[문제32] 징검닀리 κ±΄λ„ˆκΈ° - Level3

카카였 μ΄ˆλ“±ν•™κ΅μ˜ 'λ‹ˆλ‹ˆμ¦ˆ μΉœκ΅¬λ“€'이 '라이언' μ„ μƒλ‹˜κ³Ό ν•¨κ»˜ 가을 μ†Œν’μ„ κ°€λŠ” 쀑에 징검닀리가 μžˆλŠ” κ°œμšΈμ„ λ§Œλ‚˜μ„œ κ±΄λ„ˆνŽΈμœΌλ‘œ κ±΄λ„ˆλ €κ³  ν•©λ‹ˆλ‹€. '라이언' μ„ μƒλ‹˜μ€ 'λ‹ˆλ‹ˆμ¦ˆ μΉœκ΅¬λ“€'이 λ¬΄μ‚¬νžˆ 징검닀리λ₯Ό 건널 수 μžˆλ„λ‘ λ‹€μŒκ³Ό 같이 κ·œμΉ™μ„ λ§Œλ“€μ—ˆμŠ΅λ‹ˆλ‹€.

- μ§•κ²€λ‹€λ¦¬λŠ” 일렬둜 놓여 있고 각 μ§•κ²€λ‹€λ¦¬μ˜ λ””λ”€λŒμ—λŠ” λͺ¨λ‘ μˆ«μžκ°€ μ ν˜€ 있으며 λ””λ”€λŒμ˜ μˆ«μžλŠ” ν•œ 번 λ°Ÿμ„ λ•Œλ§ˆλ‹€ 1μ”© μ€„μ–΄λ“­λ‹ˆλ‹€.

- λ””λ”€λŒμ˜ μˆ«μžκ°€ 0이 되면 더 이상 λ°Ÿμ„ 수 μ—†μœΌλ©° μ΄λ•ŒλŠ” κ·Έλ‹€μŒ λ””λ”€λŒλ‘œ ν•œ λ²ˆμ— μ—¬λŸ¬ 칸을 κ±΄λ„ˆλ›Έ 수 μžˆμŠ΅λ‹ˆλ‹€.

- 단, λ‹€μŒμœΌλ‘œ λ°Ÿμ„ 수 μžˆλŠ” λ””λ”€λŒμ΄ μ—¬λŸ¬ 개인 경우 무쑰건 κ°€μž₯ κ°€κΉŒμš΄ λ””λ”€λŒλ‘œλ§Œ κ±΄λ„ˆλ›Έ 수 μžˆμŠ΅λ‹ˆλ‹€.

'λ‹ˆλ‹ˆμ¦ˆ μΉœκ΅¬λ“€'은 개울의 μ™Όμͺ½μ— 있으며, 개울의 였λ₯Έμͺ½ κ±΄λ„ˆνŽΈμ— 도착해야 징검닀리λ₯Ό κ±΄λ„Œ κ²ƒμœΌλ‘œ μΈμ •ν•©λ‹ˆλ‹€.

'λ‹ˆλ‹ˆμ¦ˆ μΉœκ΅¬λ“€'은 ν•œ λ²ˆμ— ν•œ λͺ…μ”© 징검닀리λ₯Ό κ±΄λ„ˆμ•Ό ν•˜λ©°, ν•œ μΉœκ΅¬κ°€ 징검닀리λ₯Ό λͺ¨λ‘ κ±΄λ„Œ 후에 κ·Έλ‹€μŒ μΉœκ΅¬κ°€ κ±΄λ„ˆκΈ° μ‹œμž‘ν•©λ‹ˆλ‹€.

λ””λ”€λŒμ— 적힌 μˆ«μžκ°€ μˆœμ„œλŒ€λ‘œ λ‹΄κΈ΄ λ°°μ—΄ stones와 ν•œ λ²ˆμ— κ±΄λ„ˆλ›Έ 수 μžˆλŠ” λ””λ”€λŒμ˜ μ΅œλŒ€ μΉΈ 수 kκ°€ λ§€κ°œλ³€μˆ˜λ‘œ μ£Όμ–΄μ§ˆ λ•Œ, μ΅œλŒ€ λͺ‡ λͺ…κΉŒμ§€ 징검닀리λ₯Ό 건널 수 μžˆλŠ”μ§€ returnν•˜λ„λ‘ solution ν•¨μˆ˜λ₯Ό μ™„μ„±ν•΄μ£Όμ„Έμš”.

[μ œν•œμ‚¬ν•­]

  • 징검닀리λ₯Ό κ±΄λ„ˆμ•Ό ν•˜λŠ” 'λ‹ˆλ‹ˆμ¦ˆ μΉœκ΅¬λ“€'의 μˆ˜λŠ” λ¬΄μ œν•œμ΄λΌκ³  κ°„μ£Ό
  • stones λ°°μ—΄μ˜ ν¬κΈ°λŠ” 1 이상 200,000 μ΄ν•˜
  • stones λ°°μ—΄ 각 μ›μ†Œλ“€μ˜ 값은 1 이상 200,000,000 μ΄ν•˜μΈ μžμ—°μˆ˜
  • kλŠ” 1 이상 stones의 길이 μ΄ν•˜μΈ μžμ—°μˆ˜

[λ¬Έμ œν’€μ΄]

  • μ²˜μŒμ€ 0, 끝은 λ””λ”€λŒ 쀑 κ°€μž₯ 큰 숫자λ₯Ό 지정
  • ν˜„μž¬ 주어진 인원 수만큼 닀리λ₯Ό 건널 수 μžˆλŠ”μ§€λ₯Ό νŒλ‹¨ν•˜λŠ” ν•¨μˆ˜λ§Œλ“€κΈ°
  • ν•¨μˆ˜μ˜ 결괏값에 따라 이진 탐색 μˆ˜ν–‰

[μ½”λ“œμž‘μ„±]

  • μ²˜μŒμ€ 0, 끝은 λ””λ”€λŒ 쀑 κ°€μž₯ 큰 숫자λ₯Ό 지정
def solution(stones, k):
	start, end = 0, max(stones)
    answer = 0
  • ν˜„μž¬ 주어진 인원 수만큼 닀리λ₯Ό 건널 수 μžˆλŠ”μ§€λ₯Ό νŒλ‹¨ν•˜λŠ” ν•¨μˆ˜λ§Œλ“€κΈ°
def available(n, stones, k):
	skip = 0
    for stone in stones:
    	if stone < n:
        	skip += 1
            if skip >= k: return False
            else: skip = 0
        return True
  • ν•¨μˆ˜μ˜ 결괏값에 따라 이진 탐색 μˆ˜ν–‰
while start <= end:
	mid = (start + end) // 2
    if available(mid, stones, k):
    	start = mid + 1
        answer = max(answer, mid)
    else: end = mid - 1
    
return answer

[μ „μ²΄μ½”λ“œ]

def available(n, stones, k):
	skip = 0
    for stone in stones:
    	if stone < n:
        	skip += 1
            if skip >= k: return False
        else: skip = 0
    return True
    
def solution(stones, k):
	start, end = 0, max(stones)
    answer = 0
    while start <= end:
    	mid = (start + end) // 2
        if available(mid, stones, k):
        	start = mid + 1
            answer = max(answer, mid)
        else: end = mid - 1
        
    return answer

[λ‹€λ₯Έ 풀이]

  • 미리 λ”•μ…”λ„ˆλ¦¬ 데이터λ₯Ό λ§Œλ“€μ–΄λ†“μ€ λ³€μˆ˜ μ„ μ–Έ
from collections import defaultdict
def solution(info, query):
	answer = []
    people = defaultdict(list)
  • 주어진 λͺ¨λ“  μ§€μ›μžμ˜ 데이터λ₯Ό for 문으둜 μˆœνšŒν•˜λ©΄μ„œ κ°€λŠ₯ν•œ 경우의 수λ₯Ό λ”•μ…”λ„ˆλ¦¬μ— 기둝
from itertools import combinations

for i in info:
	person = i.split()
    score = int(person.pop())
    people[''.join(person)].append(score)
    
    for j in range(4):
    	case = list(combinations(person, j))
        for c in case:
        	people[''.join(c)].append(score)
  • κΈ°λ‘ν•œ λ”•μ…”λ„ˆλ¦¬μ˜ 성적 데이터λ₯Ό λͺ¨λ‘ μ •λ ¬
for i in people: people[i].sort()
  • 문의 쑰건에 따라 κ²€μƒ‰ν•˜κ³ , λ‚˜μ˜¨ 결과의 성적 배열을 이진 νƒμƒ‰ν•˜μ—¬ λͺ‡ λͺ…인지 확인
from bisect import bisect_left as left_bound

for i in query:
	key = i.split()
    score = int(key.pop())
    key = ''.join(key)
    key = key.replce('and', '').replace(' ', '').replace('-', '')
    answer.append(len(people[key]) - left_bound(people[key], score))
    
    return answer

[μ „μ²΄μ½”λ“œ]

from itertools import combinations
from collections import defaultdict
from bisect import bisect_left as left_bound

def solution(info, query):
	answer = []
    people = defaultdict(list)
    
    for i in info:
	person = i.split()
    score = int(person.pop())
    people[''.join(person)].append(score)
    
    for j in range(4):
    	case = list(combinations(person, j))
        for c in case:
        	people[''.join(c)].append(score)
            
    for i in people: people[i].sort()
    
    for i in query:
	key = i.split()
    score = int(key.pop())
    key = ''.join(key)
    key = key.replce('and', '').replace(' ', '').replace('-', '')
    answer.append(len(people[key]) - left_bound(people[key], score))
    
return answer
profile
πŸ’» + πŸŽ₯

0개의 λŒ“κΈ€