[leetcode] Next Greater Element

Posted Lin.B

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[leetcode] Next Greater Element相关的知识,希望对你有一定的参考价值。

今天处理一下一系列题目:Next Greater Element系列。


Next Greater Element I

You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nums1‘s elements in the corresponding places of nums2.

The Next Greater Number of a number x in nums1 is the first greater number to its right in nums2. If it does not exist, output -1 for this number.

Example 1:

Input: nums1 = [4,1,2], nums2 = [1,3,4,2].
Output: [-1,3,-1]
Explanation:
    For number 4 in the first array, you cannot find the next greater number for it in the second array, so output -1.
    For number 1 in the first array, the next greater number for it in the second array is 3.
    For number 2 in the first array, there is no next greater number for it in the second array, so output -1.

Example 2:

Input: nums1 = [2,4], nums2 = [1,2,3,4].
Output: [3,-1]
Explanation:
    For number 2 in the first array, the next greater number for it in the second array is 3.
    For number 4 in the first array, there is no next greater number for it in the second array, so output -1.

Note:

  1. All elements in nums1 and nums2 are unique.
  2. The length of both nums1 and nums2 would not exceed 1000.

问题一是基础,翻译一下是这样:两个数组nums1是nums2的子集,而且nums2中各个数字都不重复。然后要求找nums1中的数字在nums2中的位置以右,是否还有比这个数字更大的数,要是有保存下来,要是没有就是-1。

思路也比较清楚,第一个想法肯定是两次遍历,但是肯定是最差的想法,我也就没有写。第二个思路,因为提到nums2中每个数字不重复,很容易联想到map,所以这个思路就是用map记录nums2中每个元素的位置,然后nums1中的元素去找的时候起始点就比较明确了。代码如下:

 1 class Solution {
 2     public int[] nextGreaterElement(int[] nums1, int[] nums2) {
 3         int[] res = new int[nums1.length];
 4         Map<Integer,Integer> map = new HashMap<>();
 5         for ( int i = 0 ; i < nums2.length ; i ++ ) map.put(nums2[i],i);
 6         for ( int i = 0  ; i < nums1.length ; i ++ ){
 7             for ( int j = map.get(nums1[i]) ; j < nums2.length ; j ++ ){
 8                 if ( nums2[j] > nums1[i] ) {
 9                     res[i] = nums2[j];
10                     break;
11                 }else res[i] = -1;
12             }
13         }
14         return res;
15     }
16 }

      运行时间3ms,击败99.95%的提交。应该是现在能优化的最优的方法了。


Next Greater Element II

Given a circular array (the next element of the last element is the first element of the array), print the Next Greater Number for every element. The Next Greater Number of a number x is the first greater number to its traversing-order next in the array, which means you could search circularly to find its next greater number. If it doesn‘t exist, output -1 for this number.

Example 1:

Input: [1,2,1]
Output: [2,-1,2]
Explanation: The first 1‘s next greater number is 2; 
The number 2 can‘t find next greater number;
The second 1‘s next greater number needs to search circularly, which is also 2.

Note: The length of given array won‘t exceed 10000.

现在题目变了一下,翻译一下:给一个数组nums,这个数组是一个循环数组,也就是最后一个元素的下一个元素是第一个元素。要求找数组中每个元素的next greater element(定义和上面题目一样)。

 第一个思路:根据循环数组的定义,我们可以再定义一个长度为nums.length*2的数组,然后问题就简单了。代码如下:

 1 class Solution {
 2    public int[] nextGreaterElements(int[] nums) {
 3         int[] array = new int[nums.length*2];
 4         int[] res = new int[nums.length];
 5         for ( int i = 0 ; i < nums.length ; i ++ ) {
 6             array[i] = nums[i];
 7             array[i+nums.length] = nums[i];
 8         }
 9         for ( int i = 0 ; i < nums.length ; i ++ ){
10                 for (int j = i; j < array.length; j++) {
11                     if (array[j] > nums[i]) {
12                         res[i] = array[j];
13                         break;
14                     } else{
15                         res[i] = -1;
16                     }
17                 }
18         }
19         return res;
20     }
21 }

      运行时间46ms,击败74.06%提交。


 



以上是关于[leetcode] Next Greater Element的主要内容,如果未能解决你的问题,请参考以下文章

[栈] leetcode 503 Next Greater Element II

[leetcode] Next Greater Element

[leetcode-556-Next Greater Element III]

LeetCode 1019. Next Greater Node In Linked List

leetcode算法:Next Greater Element I

[LeetCode] Next Greater Element I