当前位置: 首页 > news >正文

Code Exercising Day 10 of “Code Ideas Record“:StackQueue part02

文章目录

    • 【150. Evaluate Reverse Polish Notation】
    • 【239. Sliding Window Maximum】
    • 【347. Top K Frequent Elements】

【150. Evaluate Reverse Polish Notation】

Problem Link
Approach: Use a stack. Push numbers onto the stack; when encountering an operator, pop the top two numbers, perform the operation, and push the result back onto the stack.
Helper Function: stoll function: converts a string to a long long type.

class Solution {
public:int evalRPN(vector<string>& tokens) {// LeetCode has modified the backend test data, so long long is neededstack<long long> st; for (int i = 0; i < tokens.size(); i++) {if (tokens[i] == "+" || tokens[i] == "-" || tokens[i] == "*" || tokens[i] == "/") {long long num1 = st.top();st.pop();long long num2 = st.top();st.pop();if (tokens[i] == "+") st.push(num2 + num1);if (tokens[i] == "-") st.push(num2 - num1);if (tokens[i] == "*") st.push(num2 * num1);if (tokens[i] == "/") st.push(num2 / num1);} else {st.push(stoll(tokens[i]));}}long long result = st.top();st.pop(); // Pop the last element from the stack (though it's not strictly necessary)return result;}
};

【239. Sliding Window Maximum】

Problem Link
Approach: In fact, the queue doesn’t need to maintain all elements within the window. It only needs to maintain elements that could potentially become the maximum in the window, while ensuring the elements in the queue are ordered from largest to smallest.

This type of queue that maintains elements in monotonically decreasing order is called a monotonic queue, i.e., a queue that is either monotonically decreasing or increasing. C++ does not directly support monotonic queues, so we need to implement one ourselves.

Do not assume that implementing a monotonic queue means sorting the numbers within the window. If sorting were involved, how would it differ from a priority queue?

When designing the monotonic queue, the pop and push operations must adhere to the following rules:

  1. pop(value): If the element being removed from the window (value) equals the exit element of the monotonic queue, then the queue pops this element. Otherwise, no action is taken.
  2. push(value): If the element being pushed (value) is greater than the value of the entry element in the queue, then the entry elements of the queue are popped until the value being pushed is less than or equal to the value of the entry element.
    By adhering to these rules, whenever the window moves, you can simply query que.front() to return the current maximum value in the window.

Using a deque to implement this monotonic queue is most appropriate.
Solution Demo

class Solution {
private:class MyQueue { // Monotonic Queue (decreasing order)public:deque<int> que; // Using deque to implement the monotonic queue// When popping, check if the current value to pop equals the front element of the queue.// Also, check if the queue is currently empty before popping.void pop(int value) {if (!que.empty() && value == que.front()) {que.pop_front();}}// If the value being pushed is greater than the back element of the queue,// pop elements from the back until the pushed value is less than or equal to the back element.// This ensures the queue remains in decreasing order.void push(int value) {while (!que.empty() && value > que.back()) {que.pop_back();}que.push_back(value);}// Query the current maximum in the queue by simply returning the front element.int front() {return que.front();}};
public:vector<int> maxSlidingWindow(vector<int>& nums, int k) {MyQueue que;vector<int> result;for (int i = 0; i < k; i++) { // First, push the first k elements into the queueque.push(nums[i]);}result.push_back(que.front()); // Record the maximum of the first k elementsfor (int i = k; i < nums.size(); i++) {que.pop(nums[i - k]); // Remove the leftmost element of the sliding windowque.push(nums[i]); // Add the new rightmost element to the sliding windowresult.push_back(que.front()); // Record the corresponding maximum}return result;}
};

【347. Top K Frequent Elements】

Problem Link
Approach: Use a min-heap implemented with a priority queue to sort frequencies (the min-heap is used to exclude the smallest frequencies while maintaining the top K frequencies).
Key Code:

class mycomparison {
public:bool operator()(const pair<int, int>& lhs, const pair<int, int>& rhs) {return lhs.second > rhs.second;// '>' creates a min-heap, opposite to quicksort  }
};priority_queue<pair<int, int>, vector<pair<int, int>>, mycomparison> pri_que;for (unordered_map<int, int>::iterator it = map.begin(); it != map.end(); it++) {pri_que.push(*it);if (pri_que.size() > k) { // If the heap size exceeds K, pop the smallest to maintain size K  pri_que.pop();}
}
class Solution {
public:// Min-heap  class mycomparison {public:bool operator()(const pair<int, int>& lhs, const pair<int, int>& rhs) {return lhs.second > rhs.second;}};vector<int> topKFrequent(vector<int>& nums, int k) {// Count element frequencies  unordered_map<int, int> map; // map<nums[i], frequency>  for (int i = 0; i < nums.size(); i++) {map[nums[i]]++;}// Sort frequencies  // Define a min-heap of size k  priority_queue<pair<int, int>, vector<pair<int, int>>, mycomparison> pri_que;// Traverse all frequencies with the fixed-size min-heap  for (unordered_map<int, int>::iterator it = map.begin(); it != map.end(); it++) {pri_que.push(*it);if (pri_que.size() > k) { // If the heap size exceeds K, pop the smallest to maintain size K  pri_que.pop();}}// Extract the top K frequent elements  // Since the min-heap pops the smallest first, reverse the output order  vector<int> result(k);for (int i = k - 1; i >= 0; i--) {result[i] = pri_que.top().first;pri_que.pop();}return result;}
};
http://www.dtcms.com/a/324215.html

相关文章:

  • MVCC和日志
  • 国内外主流大模型深度体验与横向评测:技术、场景与未来展望
  • 后置定语:for + 宾语 + 被动不定式
  • CentOS 10在文本控制台模式下修改字体大小
  • 2020/12 JLPT听力原文 问题一
  • LLM多模态模型应用探索调研
  • 【0基础3ds Max】主工具栏介绍(下)
  • 故障诊断 | VMD-CNN-LSTM西储大学轴承故障诊断附MATLAB代码
  • 智慧社区--4
  • 【C++详解】红黑树规则讲解与模拟实现(内附红黑树插入操作思维导图)
  • 本地代码上传Github步骤
  • 《设计模式》UML类图
  • 通过trae开发你的第一个Chrome扩展插件
  • A4.0:继C5.2的BJT理论引申的开关作用的应用示例
  • DAY36打卡
  • 计算机网络:求地址块128.14.35.7/20中的相关信息
  • 枚举-dfs深度优先搜索
  • 女子试穿4条裤子留下血渍赔50元引争议:消费责任边界在哪?
  • C/C++类型转换(C++四大强制类型转换)
  • 北京JAVA基础面试30天打卡06
  • 编程基础之多维数组——矩阵交换行
  • 每日五个pyecharts可视化图表-line:从入门到精通 (2)
  • 周学会Matplotlib3 Python 数据可视化-绘制折线图(Lines)
  • GPT-5与中国AI发展(DeepSeek R1视角)
  • 基于Django的图书馆管理系统的设计与实现
  • drippingblues靶机通关练习笔记
  • Jotai:React轻量级状态管理新选择
  • 【Bluetooth】【Transport层篇】第六章 基于SDIO的蓝牙硬件发送协议 SDIO Transport详解
  • QT常用控件三
  • Redis 简介与 redis-plus-plus 使用指南