[LeetCode 496/503/556] Next Grea

2019-06-03  本文已影响0人  灰睛眼蓝

#496 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:

Solution1. Brutal force // Solution 2. Stack + Map

class Solution {
    public int[] nextGreaterElement(int[] nums1, int[] nums2) {
        if (nums1 == null || nums1.length == 0 || nums2 == null || nums2.length == 0) {
            return new int[0];
        }
        
        // Brutal force solution
//         int[] result = new int [nums1.length];
//         for (int i = 0; i < nums1.length; i++) {
//             int currentNum = nums1 [i];
            
//             int j = 0;
//             for (; j < nums2.length; j++) {
//                 if (nums2[j] == currentNum) {
//                     break;
//                 }
//             }
            
//             int nextGreaterIndex = -1;
            
//             for (; j < nums2.length; j++) {
//                 //System.out.println ("nums2[j]: " + nums2[j] + " currentNum:" + currentNum);
//                 if (nums2[j] > currentNum) {
//                     nextGreaterIndex = j;
//                     break;
//                 }
//             }
            
//             result[i] = nextGreaterIndex == -1 ? -1 : nums2[nextGreaterIndex];
//         }
        
//         return result;
        
        // Stack + Map solution, O (n)
        
        Map<Integer, Integer> numAndNextGreaterInNums2 = new HashMap <> ();
        Stack<Integer> tracker = new Stack<> ();
        for (int num : nums2) {
            while (!tracker.isEmpty () && tracker.peek () < num) {
                numAndNextGreaterInNums2.put (tracker.pop (), num);
            }
            tracker.push (num);
        }
        
        int[] result = new int [nums1.length];
        int index = 0;
        // find result
        for (int num : nums1) {
            result [index ++] = numAndNextGreaterInNums2.getOrDefault (num, -1);
        }
        
        return result;
    }
}

#503 Next Greater Element II

Solution 循环找

class Solution {
    public int[] nextGreaterElements(int[] nums) {
        if (nums == null || nums.length == 0) {
            return new int[0];
        }
        
        int[] result = new int [nums.length];
        
        for (int i = 0; i < nums.length; i++) {
            int currentNum = nums [i];
            for (int j = i + 1; j <= i + nums.length; j++) {
                if (currentNum < nums [j % nums.length]) {
                    result [i] = nums [j % nums.length];
                    break;
                } else {
                    result [i] = -1;
                }
            }
        }
        
        return result;
    }
}

#556 Next Greater Element III

Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly the same digits existing in the integer n and is greater in value than n. If no such positive 32-bit integer exists, you need to return -1.

Example 1:

Input: 12
Output: 21

Example 2:

Input: 21
Output: -1

Solution: 可以转成Next Permutation 来做

class Solution {
    public int nextGreaterElement(int n) {
        List<Integer> nums = new ArrayList<> ();
        while (n != 0) {
            nums.add (n % 10);
            n = n / 10;
        }
        
        Collections.reverse (nums);

        // 1. find the first number which is smaller then previous one from the end
        int firstReplaceIndex = nums.size () - 2;
        while (firstReplaceIndex >= 0) {
            if (nums.get (firstReplaceIndex) < nums.get (firstReplaceIndex + 1)) {
                break;
            }
            
            firstReplaceIndex --;
        }
        
        if (firstReplaceIndex == -1) {
            return -1;
        }
        
        //2. find the second number to swap
        int i = firstReplaceIndex + 1;
        while (i < nums.size () && nums.get (i) > nums.get (firstReplaceIndex)) {
            i++;
        }
        
        int temp = nums.get (firstReplaceIndex);
        nums.set (firstReplaceIndex, nums.get (i - 1));
        nums.set (i - 1, temp);
        
        Collections.sort (nums.subList(firstReplaceIndex + 1, nums.size ()));
        long result  = 0;
        for (int num : nums) {
            result = result * 10 + num;
        }
        
        return result > Integer.MAX_VALUE ? -1 : (int)result;
    }
}
上一篇 下一篇

猜你喜欢

热点阅读