LeetCode 632. 最小区间

news/2024/5/20 6:06:40 标签: 滑动窗口, 哈希, 双指针

原题目:https://leetcode-cn.com/problems/smallest-range-covering-elements-from-k-lists/

 

思路:

采用hash、滑动窗口双指针的方法,当窗口不包含每个数组中的元素时,右指针右移,直到窗口包含。包含以后,左指针右移,直到不包含,此时记录betsleft,bestright

 

代码:

class Solution {
public:
    vector<int> smallestRange(vector<vector<int>>& nums) {
        
        int n = nums.size();
        unordered_map<int, vector<int>> indices;
        int xMin = INT_MAX, xMax = INT_MIN;
        for (int i = 0; i < n; ++i) {
            for (const int& x: nums[i]) {
                indices[x].push_back(i);
                xMin = min(xMin, x);
                xMax = max(xMax, x);
            }
        }

        vector<int> freq(n);
        int inside = 0;
        int left = xMin, right = xMin - 1;
        int bestLeft = xMin, bestRight = xMax;

        while (right < xMax) {
            ++right;
            if (indices.count(right)) {
                for (const int& x: indices[right]) {
                    ++freq[x];
                    if (freq[x] == 1) {
                        ++inside;
                    }
                }
                while (inside == n) {
                    if (right - left < bestRight - bestLeft) {
                        bestLeft = left;
                        bestRight = right;
                    }
                    if (indices.count(left)) {
                        for (const int& x: indices[left]) {
                            --freq[x];
                            if (freq[x] == 0) {
                                --inside;
                            }
                        }
                    }
                    ++left;
                }
            }
        }

        return {bestLeft, bestRight};
    }
};

 


http://www.niftyadmin.cn/n/898997.html

相关文章

LeetCode 114. Flatten Binary Tree to Linked List

原题目&#xff1a;https://leetcode-cn.com/problems/flatten-binary-tree-to-linked-list/ 代码&#xff1a; class Solution { public:void flatten(TreeNode* root) {if(rootNULL) return;else if(root->left NULL) flatten(root->right);else if(root->right …

LeetCode 969. 煎饼排序

原题目&#xff1a;https://leetcode-cn.com/problems/pancake-sorting/ 思路&#xff1a; 每次把最大的元素移到最后面&#xff0c;方法&#xff1a;找到当前的最大元素位置k&#xff0c;进行k翻转&#xff1b;然后把该元素&#xff0c;翻转到应该在的位置。迭代即可 例如:[…

LeetCode 415. Add Strings

原题目&#xff1a;https://leetcode-cn.com/problems/add-strings/ 代码&#xff1a; class Solution { public:string addStrings(string num1, string num2) {reverse(num1.begin(),num1.end());reverse(num2.begin(),num2.end());string ans;int c0,i,j,a,b;for(i0;i<n…

LeetCode 973. 最接近原点的 K 个点

原题目&#xff1a; https://leetcode-cn.com/problems/k-closest-points-to-origin/ 思路&#xff1a; 采用封装好的nth_element方法 代码&#xff1a; class Solution { public:vector<vector<int>> kClosest(vector<vector<int>>& points, in…

LeetCode 173. 二叉搜索树迭代器

原题目&#xff1a;https://leetcode-cn.com/problems/binary-search-tree-iterator/ 思路&#xff1a; 使用数组记录中序遍历的结果&#xff0c;next一次返回该数组的元素&#xff0c;havenext判断是否到达数组的尾部 代码&#xff1a; class BSTIterator { private:vector&…

LeetCode 1381. 设计一个支持增量操作的栈

原题目&#xff1a;https://leetcode-cn.com/problems/design-a-stack-with-increment-operation/ 代码&#xff1a; class CustomStack { private:vector<int> nums;int count,index0,tmp; public:CustomStack(int maxSize) {count maxSize;}void push(int x) {if(ind…

使用栈实现二叉树的前中后序遍历

前序&#xff1a; class Solution { public:vector<int> preorderTraversal(TreeNode* root) {if(rootNULL) return {};stack<TreeNode*> s;s.push(root);vector<int> ans;while(!s.empty()){TreeNode* tmp s.top();s.pop();ans.push_back(tmp->val);if…

LeetCode 1047. 删除字符串中的所有相邻重复项

原题目&#xff1a;https://leetcode-cn.com/problems/remove-all-adjacent-duplicates-in-string/ 代码&#xff1a; class Solution { public:string removeDuplicates(string S) {stack<char> s;for(int iS.size()-1;i>0;--i){if(s.size() && s.top()S[i]…