Python solution with heap

  • 0
    from collections import namedtuple
    from heapq import *
    class Item(namedtuple('Item', ['col', 'row', 'val'])):
    class ZigzagIterator(object):
        def __init__(self, v1, v2):
            Initialize your data structure here.
            :type v1: List[int]
            :type v2: List[int]
            self.h = []
            vs = [v1, v2]
            for v_id, v in enumerate(vs):
                for num_id, num in enumerate(v):
                    heappush(self.h, Item(num_id, v_id, num))
        def next(self):
            :rtype: int
            return heappop(self.h).val
        def hasNext(self):
            :rtype: bool
            return len(self.h)
    # Your ZigzagIterator object will be instantiated and called as such:
    # i, v = ZigzagIterator(v1, v2), []
    # while i.hasNext(): v.append(

Log in to reply

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