代码随想录算法训练营第36期day13

DAY13

239滑动窗口最大值,困难

已二刷:

  1. class Solution {
  2. private:
  3.     class MyQueue{
  4.     public:
  5.         deque<int> que;//写特殊的弹出、写特殊的push_back,单调队列要给出一个最大值front
  6.         //这个好理解
  7.         void pop(int value)
  8.         {
  9.             if(!que.empty()&&value==que.front())
  10.             {
  11.                 que.pop_front();
  12.             }
  13.         }
  14.         //从(back)(不是入口)开始比较,新值大,就要弹出,给新值让位置(有多大呢?得用while,实现“直到”)
  15.         void push(int value)
  16.         {
  17.             while(!que.empty()&&value>que.back())
  18.             {
  19.                 que.pop_back();
  20.             }
  21.             que.push_back(value);
  22.         }
  23.         int front(){
  24.             return que.front();
  25.         }
  26.     } ;
  27. public:
  28.     vector<intmaxSlidingWindow(vector<int>& nums, int k) {
  29.         //子串个数:size()-k+1;也是输出个数。我们先读入第一个滑动窗口,那么之后的循环语句只用循环size()-k次;
  30.         MyQueue que;
  31.         vector<int> result;
  32.         for(int i=0;i<k;i++) que.push(nums[i]);
  33.         result.push_back(que.front());
  34.         for(int i=k;i<nums.size();i++)
  35.         {
  36.             que.pop(nums[i-k]);
  37.             que.push(nums[i]);
  38.             result.push_back(que.front());
  39.         }
  40.     return result;
  41.     }
  42. };

347前K个高频元素,中等

先学习c++的priority queue;

已二刷:

  1. class Solution {
  2. public:
  3.     class mycompare{
  4.     public:
  5.         //注意重载比较运算符的写法
  6.         bool operator()(const pair<int,int>&lhs,const pair<int,int>&rhs)
  7.         {
  8.             return lhs.second>rhs.second;//和快排反过来
  9.         }
  10.     };
  11.     vector<inttopKFrequent(vector<int>& nums, int k) {
  12.         unordered_map<int,intmap;
  13.         for(int i=0;i<nums.size();i++) map[nums[i]]++;
  14.         priority_queue<pair<int,int>,vector<pair<int,int>>,mycompare> pr_que;
  15.         //扫描map,注意初始化语句
  16.         for(unordered_map<int,int>::iterator it=map.begin();it!=map.end();it++) 
  17.         {
  18.             pr_que.push(*it);
  19.             if(pr_que.size()>k) pr_que.pop();
  20.         }
  21.         //注意定义方式,最后一个(idx=k)好像不能用
  22.         vector<intresult(k);
  23.         for(int i=k-1;i>=0;i--)
  24.         {
  25.             //注意是top,而不是其他的(front)。 top().first注意写法
  26.             result[i]=pr_que.top().first;
  27.             pr_que.pop();
  28.         }
  29.         return result;
  30.     }
  31. };

ACWING827双链表

图片来自:https://www.cnblogs.com/qdu-lkc/p/12221538.html

同理题给k对应到代码函数是k+1;

  1. #include <iostream>
  2. using namespace std;
  3. const int N = 100010;
  4. int m;
  5. int e[N], l[N], r[N], idx;
  6. // e:这个点的值
  7. // l:这个点左边是谁
  8. // r:这个点右边是谁
  9. //idx:idx 存储当前已经用到了哪个点
  10. void init () {
  11.     l[1] = 0;//左指针   0:头结点
  12.     r[0] = 1;//右指针   1:尾结点  
  13.     idx = 2//已经有俩个结点了 即移动了俩次
  14. }
  15. //在下标是k的点的右边,插入x
  16. //在下标为k的节点左边插入节点:add(l[k],x)别弄错
  17. void  insert (int k,int x) {
  18.     e[idx] = x;//赋值
  19.     l[idx] = k;
  20.     r[idx] = r[k]; //建立指针
  21.     
  22.     l[r[k]] = idx;//修改指针
  23.     r[k] = idx++;
  24. }
  25. //删除下标为k 的结点
  26. void remove (int k) {
  27.     l[r[k]] = l[k];//让左边的右边直接等于右边
  28.     r[l[k]] = r[k];//让右边的左边直接等于左边
  29. }
  30.  
  31. int main ( ) {
  32.     cin >> m;
  33.     init();//初始化
  34.     while (m --) {
  35.         string op; 
  36.         int k, x;
  37.         cin >> op;
  38.         if ( op== "L") {// 从最左侧插入也就是在第0位的右边插入
  39.             cin >> x;
  40.             insert (0, x);
  41.         }
  42.         else if (op == "R") {// 从最右侧插入即在第1位的左边插入
  43.             cin >> x;
  44.             insert(l[1], x);
  45.         }
  46.         else if (op == "D") {
  47.             cin >> k;
  48.             remove(k + 1);
  49.         }
  50.         else if (op == "IL") {
  51.             cin >> k >> x;
  52.             insert(l[k + 1], x);
  53.         }
  54.         else {
  55.             cin >> k >> x;
  56.             insert(k + 1, x);
  57.         }
  58.     }
  59.     for (int i = r[0]; i != 1;i=r[i]) {
  60.         cout << e[i] << ' ';
  61.     }
  62.     cout << endl;
  63.     return 0;
  64. }

ACWING828 模拟栈

思路:

  1. #include<iostream>
  2. using namespace std;
  3. const int N=100010;
  4. //栈顶下标tt
  5. int stk[N],tt;
  6. //插入一个元素 
  7. stk[++tt]=x;
  8. //删除元素
  9. tt--;
  10. //判断栈是否为空
  11. if(t>0not empty;
  12. else empty
  13. //取栈顶元素
  14. stk[tt];

可运行:

  1. #include<iostream>
  2. using namespace std;
  3. constexpr int N = 100010;
  4. int stk[N];
  5. int top = 0;
  6. int main() {
  7.  string s;
  8.  int n, x;
  9.  cin >> n;
  10.  while (n--) {
  11.   cin >> s;
  12.   if (s == "push") {
  13.    cin >> x;
  14.    stk[++top] = x;
  15.   } else if (s == "pop") {
  16.    top--;
  17.   } else if (s == "query") {
  18.    cout << stk[top] << endl;
  19.   } else if (s == "empty") {
  20.    cout << (top == 0 ? "YES" : "NO") << endl;
  21.   }
  22.  }
  23.  
  24.  return 0;
  25. }

ACWING829 模拟队列

思路:

  1. #include<iostream>
  2. using namespace std;
  3. const int N=100010;
  4. //队头hh,队尾tt
  5. int q[N],hh,tt=-1;
  6. q[++t]=x;
  7. hh++;
  8. if(hh<=tt) not empty;
  9. else empty;
  10. q[hh];
  11. q[tt];

可运行:

  1. #include<iostream>
  2. using namespace std;
  3. const int N=100010;
  4. int qu[N];
  5. int head=1;
  6. int tail=0;
  7. void push(int x) {
  8.     qu[++tail]=x;
  9. }
  10. void pop() {
  11.     if(tail>=head) head++;
  12.     else cout<<"FBI WARNING\n";
  13. }
  14. bool empty() {
  15.     if(tail>=head) return false;
  16.     else return true;
  17. }
  18. int query() {
  19.     if(tail>=head) return qu[head];
  20.     else cout<<"FBI WARNING\n";
  21.     return -1;
  22. }
  23. int main() {
  24.     int n;
  25.     cin>>n;
  26.     string str;
  27.     int x;
  28.     while(n--) {
  29.         cin>>str;
  30.         if(str=="push") {
  31.             cin>>x;
  32.             push(x);
  33.         } else if(str=="pop") {
  34.             pop();
  35.         } else if(str=="empty") {
  36.             if(empty()) cout<<"YES\n";
  37.             else cout<<"NO\n";
  38.         } else {
  39.             cout<<query()<<"\n";
  40.         }
  41.     }
  42.     return 0;
  43. }

ACWING算法基础课第一章习题课:

799最长连续不重复子序列

之前在B站学了滑动窗口,正好练练最长问题。

模版没用出来。。。。之前做过,但是还是想不出,多练吧。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值