1. 程式人生 > >[leetcode] Next Greater Element

[leetcode] Next Greater Element

you mean The won 運行時間 ++ num put 但是

今天處理一下一系列題目: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