Priyanka and Toys

  • + 0 comments
    def toys(w):
        count = 0
        w.sort()
        N = len(w)
        i = 0
    
        while i < N:
            # The weight of the current toy.
            current_toy_weight = w[i]
            
            # We need at least one container, so we increment the count.
            count += 1
            
            # We can fit any toy with a weight up to 4 units greater than the first toy in the container.
            limit = current_toy_weight + 4
            
            # We find how many toys fit in this container.
            while i < N and w[i] <= limit:
                i += 1
                
        return count