Why my solution is wrong? Use Python


  • 0
    Z

    My idea is, first, find the point with minimum end, and then, use a loop to see find other points with heads not larger than that minimum end, those points will disappear with that point, so I see these steps as one arrow. Do these until there is no points left, then return the count.

    My solution cannot pass the test, but I cannot find where is wrong.

    Hope to find the mistake. Wait and thank for the suggestion!

    class Solution(object):
        def findMinArrowShots(self, points):
            """
            :type points: List[List[int]]
            :rtype: int
            """     
            # turn list into dic, structure is like {end1:head1, end2:head2, ...}
            points_dic = {}
            for i in points:
                points_dic[i[1]] = i[0]
            
            count = 0
            while not len(points_dic) == 0:
                min_x2 = min(points_dic.keys())
                # burst by arrow, so pop it
                points_dic.pop(min_x2)
                for i in points_dic.keys():
                    # if it burst by the same arrow
                    if min_x2 >= points_dic[i]:
                        points_dic.pop(i)
                # use one arrow, add it
                count += 1
            
            return count
    

Log in to reply
 

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