728x90

https://school.programmers.co.kr/learn/courses/30/lessons/155651

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

def solution(book_time):
    answer = 1
    book_time.sort(key= lambda x: x[0])

    for i in range(0, len(book_time)):
        book_time[i][0] = int(book_time[i][0][0:2]) * 60 + int(book_time[i][0][3:5])
        book_time[i][1] = int(book_time[i][1][0:2]) * 60 + int(book_time[i][1][3:5])

    print(book_time)

    min_f = [book_time[0][1]]
    for i in range(0, len(book_time)-1):
        if min(min_f) + 10 > book_time[i+1][0]:
            answer += 1
            min_f.append(book_time[i+1][1])
    return answer

역시나 반례들이 수두룩...

 

from heapq import heappop, heappush

def solution(book_time):
    answer = 1
    book_time.sort(key= lambda x: x[0])

    for i in range(0, len(book_time)):
        book_time[i][0] = int(book_time[i][0][0:2]) * 60 + int(book_time[i][0][3:5])
        book_time[i][1] = int(book_time[i][1][0:2]) * 60 + int(book_time[i][1][3:5])

    print(book_time)

    heap = []
    for s,e in book_time:
        if not heap:
            heappush(heap, e)
            continue
        if heap[0] <= s:
            heappop(heap)
        else:
            answer += 1
        heappush(heap, e+10)
    return answer

다른 분의 풀이를 참조하여...heap을 써야 빠르구나

 

# 처음 작성한 코드를 다시

def solution(book_time):
    answer = 1
    book_time.sort(key= lambda x: x[0])

    for i in range(0, len(book_time)):
        book_time[i][0] = int(book_time[i][0][0:2]) * 60 + int(book_time[i][0][3:5])
        book_time[i][1] = int(book_time[i][1][0:2]) * 60 + int(book_time[i][1][3:5])

    print(book_time)


    min_f = []

    for i in range(0, len(book_time)):
        if not min_f:
            min_f.append(book_time[0][1])
            continue
        min_f.sort()
        if min_f[0] + 10 > book_time[i][0]:
            answer += 1
        else:
            min_f.pop(0)
        min_f.append(book_time[i][1])
    return answer
728x90

'Algorithm > Programmers' 카테고리의 다른 글

Lv.1 공원 산책  (0) 2023.11.05
Lv1.추억 점수  (0) 2023.11.05
Lv2. 과제 진행하기(Python)  (0) 2023.07.04
프로그래머스 괄호변환 - python  (0) 2021.03.13

+ Recent posts