• 滑动窗口好题


    【leetcode】1438. 绝对差不超过限制的最长连续子数组

    给你一个整数数组 nums ,和一个表示限制的整数 limit,请你返回最长连续子数组的长度,该子数组中的任意两个元素之间的绝对差必须小于或者等于 limit 。

    如果不存在满足条件的子数组,则返回 0 。

    很好的一道关于滑动窗口的题目,与上学期写TCP所用的思想有类似之处,重点在于用一个set进行查询最大和最小值(rbegin,begin)以及删除相关元素(erase(set.find(num)))的优化

    
     class Solution {
    public:
        int longestSubarray(vector<int>& nums, int limit) {
            multiset<int> window;
            int left = 0, right = 0, ans = 0;
            while(right < nums.size()) {
                window.insert(nums[right]);
                while(*window.rbegin() - *window.begin() > limit) { //if > : move the window
                    window.erase(window.find(nums[left]));
                    left++;
                }
                ans = max(ans, right - left + 1);
                right++;
            }
            return ans;
        }
    };
    

    两个解法(后者比前者时间复杂度更低,相当于另外使用一个循环求最大增量,类似于递推,后一状态等于减去首部加上尾部)

    //方法一
    class Solution {
    public:
        int maxSatisfied(vector<int>& customers, vector<int>& grumpy, int X) {
            int ans = 0;
            for(int i = 0; i < grumpy.size(); ++i) {
                if(grumpy[i] == 0) {
                    ans += customers[i];
                }
            }
            int left = 0, right = X;
            int ans0 = ans;
            while(right <= customers.size()) {
                int temp = ans0;
                for(int i = left; i < right; ++i) {
                    if(grumpy[i] == 1) {
                        temp += customers[i];
                    }
                }
                ans = max(temp, ans);
                left++;
                right++;
            }
            return ans;
        }
    };
    
    //方法二
    class Solution {
    public:
        int maxSatisfied(vector<int>& customers, vector<int>& grumpy, int X) {
            int total = 0;
            int n = customers.size();
            for (int i = 0; i < n; i++) {
                if (grumpy[i] == 0) {
                    total += customers[i];
                }
            }
            int increase = 0;
            for (int i = 0; i < X; i++) {
                increase += customers[i] * grumpy[i];
            }
            int maxIncrease = increase;
            for (int i = X; i < n; i++) {
                increase = increase - customers[i - X] * grumpy[i - X] + customers[i] * grumpy[i];
                maxIncrease = max(maxIncrease, increase);
            }
            return total + maxIncrease;
        }
    };
    
    作者:LightAc
    出处:https://www.cnblogs.com/lightac/
    联系:
    Email: dzz@stu.ouc.edu.cn
    QQ: 1171613053
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
  • 相关阅读:
    vim中字符串的替换
    利用nginx 来实现内网yum源(反向代理)
    linux下分卷压缩,合并解压的3种方法
    Lua io.lines()
    Springboot入门:
    GitHub起步---创建第一个项目
    常用的git命令
    Linux下安装Git
    Mysql连接报错:1130-host ... is not allowed to connect to this MySql server如何处理
    windows下开启远程连接Mysql
  • 原文地址:https://www.cnblogs.com/lightac/p/14425989.html
Copyright © 2020-2023  润新知