Leetcode Problem 556. Next Greater Element III
556. Next Greater Element III
AI Mock Interview
Leetcode Solutions
Next Greater Element with Single Pass and Reversal
Solution Idea
Algorithm Steps
Code Implementation
Complexity Analysis
Convert the number to a list of its digits.
Traverse the list from right to left to find the first pair of digits where the right digit is greater than the left digit.
If no such pair is found, return -1 as no greater permutation is possible.
Find the smallest digit to the right of the found pair that is larger than the left digit of the pair.
Swap this digit with the left digit of the pair.
Reverse the sublist to the right of the original left digit's position to get the smallest permutation.
Convert the list back to an integer.
Check if the result fits in a 32-bit integer, if not return -1.
Brute Force Permutation Approach
Ask Question
Programming Language
Purpose:
General Question
Debug My Code
image/screenshot of info
(optional)
[+]
Full Screen
Loading...
Get Answer
Suggested Answer
Answer
Full Screen
Copy Answer Code
Loading...