leetcode 503. The Next Greater Element II

Given a looping array (the element next to the last element is the first element of the array), output the next larger element of each element. The next greater element of number x is the first greater number after this number in array traversal order, which means you should loop through it to search for its next greater number. If not present, output -1.

Example 1:

Input: [1,2,1]
Output: [2,-1,2]
Explanation: The next greater number of the first 1 is 2;
The number 2 cannot find the next larger number;
The next largest number of the second 1 needs to be searched in a loop, and the result is also 2.

Note: The length of the input array will not exceed 10000.

Violent film, the time complexity is n*n;

 1 class Solution {
 2 public:
 3     vector<int> nextGreaterElements(vector<int>& nums) {
 4         int len = nums.size();
 5         vector<int> ans;
 6         int a = -1;
 7         for(int i = 0; i < len; i++){
 8             int j = (i+1) % len;
 9             while(1){
10                 if(nums[j] > nums[i]) {//If it finds a larger value than nums[i], push the result and exit
 11                      ans.push_back(nums[j]);
 12                      break ;
 13                  }
 14                  ++ j;
 15                  j = j % len;
 16                  if (j == i){//If the loop comes back, it means that there is no
 17                      ans.push_back(a);
 18                      break ;
 19                  }
 20              }
 21          }
 22          return ans;
 23      }
 24 };

 

It is implemented by stacking. In the above algorithm, there are a lot of repeated traversals, resulting in low efficiency. For each array element, it is necessary to compare the elements behind it until an element larger than him is found, which is an efficient low way

The thinking implemented by the stack is roughly as follows: push the index i that does not find an element larger than itself on the stack, and pop the stack if the value of the updated index i is greater than the value at the top of the stack

It can be understood in this way that the value of the element pointed to by the index pushed into the stack is decremented. When an element larger than the value of the element pointed to by the index in the stack appears, all the values ​​that satisfy the conditions in the stack are popped;

Explain with nums[] = {1,4,2,3,5}

i = 0:    index ={0};

i = 1:    nums[1] > nums[0] -> ans[0] = nums[1]=4;    index = {1}

i = 2:    index = {1,2}

i = 3:   nums[3] > nums[2] -> ans[2] = nums[3]=3;  index={1,3}

i = 4: nums[4] > nums[3] -> ans[3] = nums[4]=5; nums[4] > nums[1] -> ans[1] = 5; index={5} Note that before i=4 in this step, all the numbers smaller than nums[4] have found numbers larger than themselves

...... 

1  class Solution {
 2  public :
 3      vector< int > nextGreaterElements(vector< int >& nums) {
 4          int n = nums.size();
 5          vector< int > ans(n, - 1 ); //initialize the answer , by default each element cannot find an element larger than him
 6          stack< int > index;
 7          for ( int i= 0 ; i < 2 *n; i++ ){
 8              while (!index.empty() && nums[i%n]>nums[index.top()% n]){
 9                 ans[index.top()%n] = nums[i%n];
10                 index.pop();
11             }
12             index.push(i);
13         }
14         return ans;
15     }
16 };

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325646676&siteId=291194637