剑指 Offer(第2版)面试题 59:队列的最大值

剑指 Offer(第2版)面试题 59:队列的最大值

剑指 Offer(第2版)面试题 59:队列的最大值

题目一:滑动窗口的最大值

题目来源:79. 滑动窗口的最大值

思路 1:优先队列

代码:

c 复制代码
class Solution
{
public:
	vector<int> maxInWindows(vector<int> &nums, int k)
	{
		priority_queue<pair<int, int>> pq;
		// 初始化
		for (int i = 0; i < k; i++)
			pq.push(make_pair(nums[i], i));
		vector<int> ans;
		ans.push_back(pq.top().first);
		// 遍历
		for (int i = k; i < nums.size(); i++)
		{
			pq.push(make_pair(nums[i], i));
			while (!pq.empty() && pq.top().second <= i - k)
				pq.pop();
			ans.push_back(pq.top().first);
		}
		return ans;
	}
};

复杂度分析:

时间复杂度:O(n),其中 n 是数组 nums 的长度。

空间复杂度:O(n),其中 n 是数组 nums 的长度。

思路 2:双端队列

cpp 复制代码
class Solution
{
public:
    vector<int> maxSlidingWindow(vector<int> &nums, int k)
    {
        deque<int> dq;
        vector<int> ans;
        for (int i = 0; i < nums.size(); i++)
        {
            if (!dq.empty() && dq.front() == i - k)
                dq.pop_front();
            while (!dq.empty() && nums[dq.back()] < nums[i])
                dq.pop_back();
            dq.push_back(i);
            if (i >= k - 1)
                ans.push_back(nums[dq.front()]);
        }
        return ans;
    }
};

复杂度分析:

时间复杂度:O(n),其中 n 是数组 nums 的长度。

空间复杂度:O(n),其中 n 是数组 nums 的长度。

题目二:队列的最大值

题目描述:请定义一个队列并实现函数 max_value 得到队列里的最大值,要求函数max_value、push_back 和 pop_front 的时间复杂度都是O(1)。若队列为空,pop_front 和 max_value 需要返回 -1。

算法:

  • 使用了 std::queue 作为存储数据的队列,使用了 std::deque(是双端队列,可以在队头和队尾进行插入和删除操作)作为辅助队列。
  • max_value() 函数用于返回当前队列中的最大值。如果辅助队列 helpQueue 为空,说明队列中没有元素,返回 -1;否则,返回 helpQueue 的队头元素。
  • push_back() 函数用于向队列中插入元素。我们首先将元素插入 queue 队尾,然后从 helpQueue 队尾开始,逐个比较并删除小于等于插入元素的元素,直到遇到大于插入元素的元素,然后将插入元素插入 helpQueue 队尾。这样,helpQueue 中的元素始终保持递减的顺序。
  • pop_front() 函数用于删除队列的队头元素。如果队列 queue 为空,说明队列中没有元素,返回 -1;否则,将 queue 的队头元素保存为 front,然后将 queue 的队头元素删除。如果 queue 的队头元素 front 等于 helpQueue 的队头元素,说明队头元素是当前最大值,需要将 helpQueue 的队头元素删除。最后,返回 front。

代码:

c 复制代码
/**
 * 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();
 */
 
 class MaxQueue {
public:
    MaxQueue() {
    }
    int max_value() {
        if (helpQueue.empty()) {
            return -1;
        }
        return helpQueue.front();
    }
    void push_back(int value) {
        queue.push(value);
        while (!helpQueue.empty() && helpQueue.back() < value) {
            helpQueue.pop_back();
        }
        helpQueue.push_back(value);
    }
    int pop_front() {
        if (queue.empty()) {
            return -1;
        }
        int front = queue.front();
        queue.pop();
        if (front == helpQueue.front()) {
            helpQueue.pop_front();
        }
        return front;
    }
private:
    queue<int> queue;
    deque<int> helpQueue;
};

复杂度分析:

时间复杂度:

  • max_value():O(1)。
  • push_back():O(1)。
  • pop_back():O(1)。

空间复杂度:O(n),其中 n 是插入元素的个数。

相关推荐
海盗猫鸥2 小时前
C++入门基础篇(1)
开发语言·c++·学习
逆水寻舟3 小时前
算法学习记录2
python·学习·算法
羞儿3 小时前
【读点论文】基于二维伽马函数的光照不均匀图像自适应校正算法
人工智能·算法·计算机视觉
青衫酒1454 小时前
中国剩余定理
算法
鸽鸽程序猿5 小时前
【数据结构】顺序表
java·开发语言·数据结构·学习·算法·intellij idea
Thunter_5 小时前
[QT入门]树形视图控件
开发语言·c++·qt
Chris-zz5 小时前
C++:继承
开发语言·c++·算法·学习方法
硕风和炜5 小时前
【LeetCode:3033. 修改矩阵 + 模拟】
java·算法·leetcode·矩阵·模拟
川爻5 小时前
String类(STL开始)
开发语言·c++
取加若则_6 小时前
C++入门(C语言过渡)
c语言·开发语言·数据结构·c++·算法