当前位置:网站首页>Leetcode: sword finger offer 59 - ii Maximum value of queue [deque + sortedlist]

Leetcode: sword finger offer 59 - ii Maximum value of queue [deque + sortedlist]

2022-06-11 18:47:00 Review of the white speed Dragon King

 Insert picture description here

analysis

Whatever it is o1
Just pick it up sortedlist + deque Simulation is enough

ac code

from sortedcontainers import SortedList


class MaxQueue:

    def __init__(self):
        self.lst = SortedList()
        self.q = deque([])


    def max_value(self) -> int:
        if not self.q: return -1
        return self.lst[-1]


    def push_back(self, value: int) -> None:
        self.q.append(value)
        self.lst.add(value)


    def pop_front(self) -> int:
        if not self.q: return -1
        val = self.q.popleft()
        self.lst.remove(val)
        return val



# Your MaxQueue object will be instantiated and called as such:
# obj = MaxQueue()
# param_1 = obj.max_value()
# obj.push_back(value)
# param_3 = obj.pop_front()

summary

deque and sortedlist Investigate

原网站

版权声明
本文为[Review of the white speed Dragon King]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111836366490.html