Advertisement

Next Greater Element III - LeetCode 556 Solution

Next Greater Element III - Complete Solution Guide

Next Greater Element III is LeetCode problem 556, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given a positive integer n , find the smallest integer which has exactly the same digits existing in the integer n and is greater in value than n . If no such positive integer exists, return -1 . Note that the returned integer should fit in 32-bit integer , if there is a valid answer but it does not fit in 32-bit integer , return -1 . Example 1: Input: n = 12 Output: 21 Example 2: Input: n = 21 Output: -1 Constraints: 1 <= n <= 2 31 - 1

Detailed Explanation

The problem asks us to find the smallest integer that is greater than a given positive integer 'n' and contains the exact same digits as 'n'. If no such integer exists, we should return -1. A crucial constraint is that the resulting integer must fit within a 32-bit signed integer range. This means if the result is valid but exceeds the maximum 32-bit integer (2<sup>31</sup> - 1), we must also return -1.

Solution Approach

The solution involves these steps: First, convert the integer to a string (or char array). Then, starting from the right, find the first decreasing digit. If no such digit is found, the number is already the largest permutation, and no greater element exists. If a decreasing digit is found, find the smallest digit to its right that is larger than the decreasing digit, and swap them. After swapping, reverse the substring of digits to the right of the initially identified decreasing digit. Finally, convert the resulting string back to an integer and check for overflow. If the resulting integer is larger than the maximum 32-bit integer, return -1. Otherwise, return the integer.

Step-by-Step Algorithm

  1. Step 1: Convert the input integer 'n' into a string (or character array).
  2. Step 2: Iterate through the string from right to left, starting from the second-to-last digit (index l-2, where l is the length of the string).
  3. Step 3: Find the first index 'i' where s[i] < s[i+1]. This is the decreasing element from right to left.
  4. Step 4: If no such 'i' is found (i.e., the digits are non-increasing from left to right), it means the number is already the largest permutation, and we return -1.
  5. Step 5: If 'i' is found, iterate through the string from right to left, starting from the last digit (index l-1).
  6. Step 6: Find the first index 'j' where s[j] > s[i]. This is the smallest digit to the right of 'i' that is larger than s[i].
  7. Step 7: Swap s[i] and s[j].
  8. Step 8: Reverse the substring of the string from index i+1 to the end (index l-1). This ensures that the digits to the right of 'i' are in ascending order, creating the smallest possible number after the swap.
  9. Step 9: Convert the modified string back into an integer.
  10. Step 10: Check if the resulting integer is within the 32-bit signed integer range (i.e., less than or equal to 2<sup>31</sup> - 1). If not, return -1. Otherwise, return the integer.

Key Insights

  • Insight 1: The next greater element can be found by identifying the first decreasing element from right to left. This marks the position where a swap needs to occur to increase the number's value.
  • Insight 2: After identifying the decreasing element, find the smallest element to its right that is larger than it and swap them. This ensures the smallest possible increase.
  • Insight 3: Finally, reverse the portion of the number to the right of the swapped element to minimize the magnitude of the increase. This creates the smallest possible next greater element.
  • Insight 4: Need to check for integer overflow since the solution must fit within a 32-bit integer.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Math, Two Pointers, String.

Companies

Asked at: DoorDash, Goldman Sachs, Mitsogo.