Brush questions every day Day13

Posted by sniped22 on Wed, 26 Jan 2022 03:03:15 +0100

Question 1: the next larger element I
The next larger element of the number x in nums1 refers to the first element larger than x to the right of the corresponding position of X in nums2.
Give you two arrays nums1 and nums2 without duplicate elements. The subscript counts from 0, where nums1 is a subset of nums2.
For each 0 < = I < nums1 Length, find the subscript j satisfying nums1[i] == nums2[j], and determine the next larger element of nums2[j] in nums2. If there is no next larger element, the answer to this query is - 1.
Returns a length of nums1 An array of length ans is used as the answer, and ans[i] is the next larger element as described above.

Example 1:
Input: nums1 = [4,1,2], nums2 = [1,3,4,2]
Output: [- 1,3, - 1]
Explanation: the next larger element of each value in nums1 is as follows:

  • 4. Marked in bold and italics, nums2 = [1,3,4,2]. There is no next larger element, so the answer is - 1.
  • 1. Marked in bold and italics, nums2 = [1,3,4,2]. The next larger element is 3.
  • 2. Marked in bold and italics, nums2 = [1,3,4,2]. There is no next larger element, so the answer is - 1.

Example 2:
Input: nums1 = [2,4], nums2 = [1,2,3,4]
Output: [3, - 1]
Explanation: the next larger element of each value in nums1 is as follows:

  • 2. Marked in bold and italics, nums2 = [1,2,3,4]. The next larger element is 3.
  • 4. Marked in bold and italics, nums2 = [1,2,3,4]. There is no next larger element, so the answer is - 1.

Tips:
1 <= nums1.length <= nums2.length <= 1000
0 <= nums1[i], nums2[i] <= 104
All integers in nums1 and nums2 are different from each other
All integers in nums1 also appear in nums2

Advanced: can you design a solution with time complexity of O(nums1.length + nums2.length)?

Source: LeetCode
Link: https://leetcode-cn.com/problems/next-greater-element-i

//Violent solution
class Solution {
public:
    vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
        vector<int> result; //The length of the array recording results is the same as that of nums1
        int max=0;
        for(int i=0;i<nums1.size();i++){
            for(int j=0;j<nums2.size();j++){
                if(nums1[i]==nums2[j]){
                    int r=j+1;
                    while(r<nums2.size()){
                        if(nums2[r]>nums1[i]){
                            max=nums2[r];
                            result.push_back(nums2[r]);
                            break;
                        }
                        r++;
                    }
                    if(max==0)
                        result.push_back(-1);
                }
                max=0;
            }
        }
        return result;
    }
   
};

Question 2: the next bigger Element II
Given a circular array (the next element of the last element is the first element of the array), the next larger element of each element is output. The next larger element of the number x is in the array traversal order, and the first number after this number is larger than it, which means that you should cycle through its next larger number. If not, output - 1.

Example 1:
Input: [1,2,1]
Output: [2, - 1,2]
Explanation: the next larger number of the first 1 is 2;
The number 2 cannot find the next larger number;
The next maximum number of the second 1 needs a circular search, and the result is also 2.
Note: the length of the input array will not exceed 10000.

Source: LeetCode
Link: https://leetcode-cn.com/problems/next-greater-element-ii

class Solution {
public:
    vector<int> nextGreaterElements(vector<int>& nums) {
        //The circular array can be regarded as doubling the length of the original array
        int len=nums.size();
        vector<int> result(len);
        //Method of using monotone stack
        stack<int> s;
        for(int i=2*len-1;i>=0;i--){
            while(!s.empty()&&nums[i%len]>=s.top()){
                s.pop();
            }
            result[i%len]=s.empty()?-1:s.top();
            s.push(nums[i%len]);
        }
        return result;
    }
};

Question 3: fill in the next right node pointer of each node
Given a perfect binary tree, all leaf nodes are in the same layer, and each parent node has two child nodes. Binary tree is defined as follows:
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
Fill in each of its next pointers so that this pointer points to its next right node. If the next right node cannot be found, set the next pointer to NULL.

In the initial state, all next pointers are set to NULL.

Advanced:
You can only use constant level extra space.
Using recursion to solve the problem also meets the requirements. The stack space occupied by the recursive program in this problem is not considered as additional space complexity.

Example:
Input: root = [1,2,3,4,5,6,7]
Output: [1, #, 2,3, #, 4,5,6,7, #]
Explanation: given A binary tree, as shown in figure A, your function should fill in each next pointer to point to its next right node, as shown in Figure B. The serialized output is arranged by sequence traversal, and the nodes of the same layer are connected by the next pointer,'# 'marks the end of each layer.

Tips:
The number of nodes in the tree is less than 4096
-1000 <= node.val <= 1000

Source: LeetCode
Link: https://leetcode-cn.com/problems/populating-next-right-pointers-in-each-node

/*
// Definition for a Node.
class Node {
public:
    int val;
    Node* left;
    Node* right;
    Node* next;

    Node() : val(0), left(NULL), right(NULL), next(NULL) {}

    Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}

    Node(int _val, Node* _left, Node* _right, Node* _next)
        : val(_val), left(_left), right(_right), next(_next) {}
};
*/

class Solution {
public:

    void connectNode(Node *root1,Node *root2){
        if(root1==NULL&&root2==NULL)
            return;
        //Connect two adjacent nodes
        root1->next=root2;
        //Child nodes connecting this node
        connectNode(root1->left,root1->right);
        connectNode(root2->left,root2->right);
        //Adjacent child nodes of adjacent parent nodes also need to be connected
        connectNode(root1->right,root2->left);
        
    }
    Node* connect(Node* root) {
        //Idea: connect two adjacent nodes
        if(root==NULL)
            return root;
        connectNode(root->left,root->right);
        return root;
    }
};

Topics: Algorithm data structure leetcode