• 카테고리

    질문 & 답변
  • 세부 분야

    알고리즘 · 자료구조

  • 해결 여부

    미해결

마구간 문제

20.07.21 15:12 작성 조회수 125

0

밑의 코드와 같이 작성하여 실행시켰는데, 2번 예제에 대해서만 오류가 뜹니다. 

정답은 43이라고 되어 있는데, 제 답은 37로 나옵니다.

강사님의 강의와 제 코드를 비교해봐도 어느 부분이 정확히 틀렸는지 잘 모르겠습니다.

검토해주시고 알려주시면 정말 감사하겠습니다.

import sys

sys.stdin = open(r"C:\Users\user\Desktop\Documents\파이썬 알고리즘 문제풀이(코딩테스트 대비)\섹션 4\4. 마구간 정하기\in2.txt", "r")
,target_count = list(map(int, input().split()))
input_list1 = []
for i in range(n):

    input_list1.append(int(input()))


def decide_horse_house():

    input_list = sorted(input_list1)

    left = 1
    right = max(input_list) - 1
    distance_between_closet_horses = (left+right)//2
    result = 0
    
    while True:
        
        count = 1
        start = input_list[0]
        for i in range(start+1, len(input_list)):
            
            if (input_list[i] - start) >= distance_between_closet_horses:
                
                count = count + 1
                start = input_list[i]
                
        if count >= target_count:
            
            result = distance_between_closet_horses
            left = distance_between_closet_horses + 1
            distance_between_closet_horses = (left+right)//2
            
        elif count < target_count:
            
            right = distance_between_closet_horses - 1
            distance_between_closet_horses = (left+right)//2
        
        if left > right:
                
            print(result)
            break
        
decide_horse_house() 

답변 2

·

답변을 작성해보세요.

1

문제가 있는 코드는 아래부분입니다.

for i in range(start+1, len(input_list)):

0

감사합니다. 매개변수 i에 들어가는 index 초기값이 이상하게 되어 있었네요. 해결했습니다.