- 03. 数组中重复的数字
- 04. 二维数组中的查找
- 05.替换空格
- 06.从尾到头打印链表
- 07.重建二叉树
- 09.用两个栈实现队列
- 10-1.斐波那契数列
- 10-2.青蛙跳台阶问题
- 剑指 offer 11.旋转数组的最小数字
- 剑指offer 12.矩阵中的路径
- 剑指offer 13.机器人的运动范围
- 剑指offer 14-1.剪绳子
- 剑指offer 14-2.剪绳子2
- 剑指offer 15.二进制中1的个数
- 剑指offer 16.数值的整数次方
- 剑指offer 17.打印从1到最大的n位数
- 剑指offer 18.删除链表的节点
- 剑指offer 19.正则表达式匹配
- 剑指offer 20.表示数值的字符串
- 剑指offer 21.调整数组顺序使奇数位于偶数前面
- 剑指offer 22.链表中倒数第k个节点
- 剑指offer 24.反转链表
- 剑指offer 25.合并两个排序的链表
- 剑指offer 26.树的子结构
- 剑指offer 27.二叉树的镜像
- 剑指offer 28.对称的二叉树
- 剑指offer 29.顺时针矩阵
- 剑指offer 30.包含min函数的栈
- 剑指offer 31.栈的压入、弹出序列
- 剑指offer 32-1.从上到下打印二叉树
- 剑指offer 32-2.从上到下打印二叉树
- 剑指offer 32-3.从上到下打印二叉树
- 剑指offer 33.二叉搜索树的后序遍历序列
- 剑指offer 34.二叉树中和为某一值的路径
- 剑指offer 35.复杂链表的复制
- 剑指offer 36.二叉搜索树与双向链表
- 剑指offer 37.序列化二叉树
- 剑指offer 38.字符串的排列
- 剑指 offer 39. 数组中出现次数超过一半的数字
- 剑指offer 40. 最小的k个数
- 剑指offer 41.数据流中的中位数
- 剑指offer 42. 连续子数组的最大和
- 剑指offer 43. 1~n整数中1出现的次数
- 剑指offer 44. 数字序列中某一位的数字
- 剑指offer 45. 把数组排成最小的数
- 剑指offer 46.把数字翻译成字符串
- 剑指offer 47.礼物的最大值
- 剑指offer 48.最长不含重复字符的字符子串
- 剑指offer 49.丑数
- 剑指offer 50.第一个只出现一次的字符
- 剑指offer 51. 数组中的逆序对
- 剑指offer 52.两个链表的第一个公共节点
- 剑指offer 53-1. 在排序数组中查找数字
- 剑指offer 53-2. 0~n-1中缺失的数字
- 剑指offer 54. 二叉搜索树的第k大节点
- 剑指offer 55-1. 二叉树的深度
- 剑指offer 55-2. 平衡二叉树
- 剑指offer 56-1. 数组中数字出现的次数
- 剑指offer 56-2. 数字中出现的次数
- 剑指offer 57-1. 和为s的两个数字
- 剑指offer 57-2. 和为s的连续正数序列
- 剑指offer 58-1. 翻转单词顺序
- 剑指offer 58-2.左旋转字符串
- 剑指offer 59-1. 滑动窗口的最大值
- 剑指offer 59-2. 队列的最大值
- 剑指offer 60. n个骰子的点数
- 剑指offer 61. 扑克牌中的顺子
- 剑指offer 62. 圆圈中最后剩下的数字
- 剑指offer 63. 股票的最大利润
- 剑指offer 64. 求1+2+…+n
- 剑指offer 65. 不用加减乘除做加法
- 剑指offer 66. 构建乘积数组
- 剑指offer 67. 把字符串转成整数
- 剑指offer 68-1. 二叉搜索树的最近公共祖先
- 剑指offer 68-2. 二叉树的最近公共祖先
剑指offer 68-2. 二叉树的最近公共祖先
题目描述
请定义一个队列并实现函数 max_value 得到队列里的最大值,要求函数max_value、push_back 和 pop_front 的 均摊 时间复杂度都是O(1)。
若队列为空,pop_front 和 max_value 需要返回 -1
示例 1:
输入:
["MaxQueue","push_back","push_back","max_value","pop_front","max_value"]
[[],[1],[2],[],[],[]]
输出: [null,null,null,2,1,2]
示例 2:
输入:
["MaxQueue","pop_front","max_value"]
[[],[],[]]
输出: [null,-1,-1]
限制:
- 1 <= push_back,pop_front,max_value的总操作数 <= 10000
- 1 <= value <= 10^5
题解
(队列) O(n)
使用两个队列,一个普通队列 q 用来存储元素,一个双端队列用来维护队列 q 中的最大值。
push_back(value)
: 如果 max_q 的最大值小于当前值 val,则一直弹出队尾,然后将当前值压入队列 q 和 max_q 中。pop_front()
: 弹出队列 q 的队头并返回,同时如果队头正好是 max_q 的队头,则 max_q 也要弹出队头。max_value()
: 返回 max_q 的队头即可,队头就是最大值。
时间复杂度
O(n)
空间复杂度
O(n)
C++ 代码
class MaxQueue {
public:
queue<int> q;
deque<int> max_q;
MaxQueue() {
}
int max_value() {
if (max_q.empty()) return -1;
return max_q.front();
}
void push_back(int value) {
while (max_q.size() && max_q.back() < value) max_q.pop_back();
q.push(value);
max_q.push_back(value);
}
int pop_front() {
if (q.empty()) return -1;
int t = q.front();
q.pop();
if (t == max_q.front()) max_q.pop_front();
return t;
}
};
/**
* Your MaxQueue object will be instantiated and called as such:
* MaxQueue* obj = new MaxQueue();
* int param_1 = obj->max_value();
* obj->push_back(value);
* int param_3 = obj->pop_front();
*/
Java 代码
class MaxQueue {
Queue<Integer> q;
LinkedList<Integer> max_q;
public MaxQueue() {
q = new LinkedList<>();
max_q = new LinkedList<>();
}
public int max_value() {
if (max_q.isEmpty()) return -1;
return max_q.getFirst();
}
public void push_back(int value) {
while (!max_q.isEmpty() && max_q.getLast() < value) {
max_q.removeLast();
}
q.add(value);
max_q.add(value);
}
public int pop_front() {
if (q.isEmpty()) return -1;
int t = q.poll();
if (t == max_q.getFirst()) {
max_q.removeFirst();
}
return t;
}
}
/**
* Your MaxQueue object will be instantiated and called as such:
* MaxQueue obj = new MaxQueue();
* int param_1 = obj.max_value();
* obj.push_back(value);
* int param_3 = obj.pop_front();
*/
Python 代码
class MaxQueue:
def __init__(self):
self.q = deque()
self.max_q = deque()
def max_value(self) -> int:
if not self.max_q:
return -1
return self.max_q[0]
def push_back(self, value: int) -> None:
while self.max_q and self.max_q[-1] < value:
self.max_q.pop()
self.q.append(value)
self.max_q.append(value)
def pop_front(self) -> int:
if not self.q:
return -1
t = self.q.popleft()
if t == self.max_q[0]:
self.max_q.popleft()
return t
# 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()
本文由读者提供,Github地址:https://github.com/tonngw
点击面试手册,获取本站面试手册PDF完整版