Simple Python; sort + greedy


  • 0
    Y
    def min_arrows(ivals):
        if not ivals:
            return 0
        ivals.sort(key=lambda ival: ival[0])
        n_arrows, last = 1, ivals[0]
        for ival in ivals[1:]:
            last_start, last_end = last
            ival_start, ival_end = ival
            if ival_start <= last_end:
                last = max(last_start, ival_start), min(last_end, ival_end)
            else:
                last = ival
                n_arrows += 1
        return n_arrows
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.