C - 3- Advanced algorithm (2) - sliding window / stack monotonous

C - 3- Advanced algorithm (2) - sliding window / stack monotonous

No. Sliding window class topic
1 Sliding window maximum
2 Achieve maximum queue
3 And s is a continuous sequence of positive numbers
4 No repeating longest substring of characters (sliding window)
Title sliding window maximum 1
Standard Solution sliding window !!!!!
class Solution {
public:
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        // 定义一个双端队列
        deque<int> window;
        vector<int> ans;
        if (nums.empty())
        {
            return ans;
        }
        for ( int i = 0 ; i < nums.size(); i++ ) // 对每个位置进行入窗口的操作
        {
            // r 右移策略 
            while ( !window.empty() && nums[window.back()] <= nums[i] )
            {
                window.pop_back();
            }
            window.push_back( i );

            // 每次检查窗口最前面的元素是否过期
            if ( window.front() <= i - k )
            {
                window.pop_front();
            }

            // 窗口大小存储元素
            if ( i  >=  k-1 )
            {
                ans.push_back( nums[window.front()] );
            }
        }
        return ans;
    }
};
  1. Their own practices
class Solution {
public:
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        // 定义一个双端队列
        deque<int> window;
        vector<int> ans;
        if (nums.empty())
        {
            return ans;
        }
        int cur_max = nums[0];

        // 把初始窗口填上, 求得当前窗口最值, 注册

        int m = k <= ( nums.size()) ? k :nums.size();

        for(int i = 0 ;  i < m ; i ++)
        {
            window.push_back(nums[i]);
            cur_max = max( cur_max , window.back());
        }
        ans.push_back(cur_max);

        // 窗口大小要是比数组size大,就不用滑动了
        if ( k >= nums.size())
        {
            return ans;
        }

        // 从 k 位置开始滑动,前端出一个,后端进一个 
        for (int i = k; i < nums.size() ; i ++ )
        {
            int out = window.front();
            window.pop_front();
            window.push_back(nums[i]);

            // ①如果 out 的 数字 不是当前最大值 
            // 只需要用 cur_max 和 in 的值比较就可以确定新的最大值
            if ( out  != cur_max)
            {
                cur_max = (cur_max > nums[i]) ? cur_max : nums[i];
            }
            //  ②如果 out 的 数字 刚好是当前最大值
            //  分为两种情况
            //  A--> in 的值比 out 的值 大 或相等 
            //  B--> in 的值比 out 小 , 那就只能在队列里遍历求新的最大值了
            else if (out == cur_max)
            {
                if (nums[i] >= out)
                {
                    cur_max = nums[i];
                }else{
                    cur_max = window.front();
                    for (int j = 0 ; j < window.size(); j ++)
                    {
                        cur_max = max(cur_max, window[j]);
                    }
                }
            }
            ans.push_back(cur_max);
        }
        return ans;
    }
};
Title 2 - II the maximum queue.

Define and implement a queue maximum queue function max_value obtained, requiring max_value function, and time complexity push_back pop_front are O (1).

If the queue is empty, pop_front max_value and need -1

Example 1:

输入:
["MaxQueue","push_back","push_back","max_value","pop_front","max_value"]
[[],[1],[2],[],[],[]]
输出: [null,null,null,2,1,2]
示例 2:

Input:
[ "MaxQueue", "pop_front", "MAX_VALUE"]
[[], [], []]
Output: [null, -1, -1]

class MaxQueue {
public:
    deque<int> window; // 维持最大值的窗口
    deque<int> queue_;  // 维持基本元素的队列
    MaxQueue() {
    
    }
    
    // 直接返回最大值窗口的front()
    int max_value() {
        if ( window.empty() ){
            return -1 ;
        }else{
            return window.front();
        }
    }

    // 从后面插入-> 修改基本队列 和 最大值窗口
    void push_back( int value ) {

        queue_.push_back(value);

        while ( ! window.empty() && window.back() <= value)
        {
            window.pop_back();
        }
        window.push_back(value);
    }
    
    //  从前端弹出--> 
    int pop_front() {

        if ( queue_.empty() )
            return -1;
        int ans = queue_.front();
        queue_.pop_front();
        // ①如果要弹出的元素刚好等于  --> window.pop_front() 
        // ②如果不等于 window,front() ,说明在这个元素插入之后,有比他大的元素进了window ,所以window里不可能有它,不用管了  
        if ( ans == window.front() )
        {
            window.pop_front();
        }
        return ans;
    }
};

/**
 * 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();
 */
3 and the title number of consecutive positive sequence s (sliding window)

Enter a positive integer target, output of all consecutive positive integers, and the target sequence (containing at least two numbers).

The numbers in ascending sequence are arranged in ascending order of different sequences according to the first digit.

Example 1:

Input: target = 9
Output: [[2,3,4], [4,5]]
Example 2:

Input: target = 15
Output: [[1,2,3,4,5], [4,5,6], [7,8]]

class Solution {
public:
    vector<vector<int>> findContinuousSequence( int target ) {
        vector<vector<int>> ans;
        deque<int> window;
        int num = target >> 1 ;
        int sum = 0 ;
        vector<int> tmp;
        // 遍历到中位数的下一位就可以了, 进队列,
        for ( int i = 1 ; i <= num+1 ; i++)
        {
            window.push_back(i);
            sum = sum + i ; 
            
            // 只要是当前队列和 大于target , 就从前面 pop 
            while( sum > target )
            {
                sum = sum - window.front();
                window.pop_front();
            }

            if ( sum == target )
            {
                for( auto k : window )
                {
                    tmp.push_back(k);
                }
                ans.push_back(tmp);
                tmp.clear();
            }
        }
        return ans;
    }
};
Title 4 longest substring no repeating characters (sliding window)

Please find a longest common string does not contain repeating characters from a string, we calculate the length of the longest substring.

Example 1:
Input: "abcabcbb"
Output: 3
Explanation: Because the longest substring of characters without repetition is "abc", so its length is 3.

Example 2:
Input: "bbbbb"
Output: 1
Explanation: Because the longest substring is repeated characters without "b", so that its length is 1.

Example 3:
Input: "pwwkew"
Output: 3
Explanation: Because the longest substring is repeated characters without "wke", so its length is 3.

Please note that your answer must be a substring of length, "pwke" is a sub-sequence, not a substring.

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        if( s.empty() || s.size() == 1 )
        {
            return s.size();
        }
        deque<char> window;
        set<char> st;
        int ans = 0 ;
        for ( int  i = 0 ; i < s.size() ; i ++ ) // 每个位置的字符串入队列
        {
            // 如果 st 没有是s[i] 更新 window, st ,ans
            if ( ! st.count(s[i])  )
            {
                window.push_back(s[i]);
                st.insert(s[i]);
                ans   = ans > window.size()  ? ans : window.size() ;

            }else if ( st.count(s[i]) )
            {
                ans   = ans > window.size()  ? ans : window.size() ;
                // 队列里是s[i]之前的出队列
                char cur = window.front();               
                while ( cur != s[i] )
                {
                    window.pop_front();
                    st.erase(cur);
                    cur = window.front();
                }
                window.pop_front();
                window.push_back(s[i]);
                st.insert(s[i]);
            }
        }
        return ans;
    }
};

Guess you like

Origin www.cnblogs.com/jiangxinyu1/p/12407699.html
Recommended