Movie Festival

超簡單,用貪心線性去比較"下一個不重疊且最早結束的電影。"

將li照結束時間排序,因為貪心法就是要儘早結束這部電影,讓出更多時間給後面的電影!

n = int(input())
li = []
for _ in range(n):
    a,b = map(int,input().split())
    li.append((a,b))

li.sort(key = lambda x : x[1]) # 用結束時間做排序

ans = 1 # 一定會看到至少一部
n_s,n_e = li[0]
for ne_s,ne_e in li:
    if n_e<=ne_s:
        ans +=1
        n_e = ne_e
print(ans)

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *