#556

Next Greater Element III

expert · 1070 · lc medium +32 · verified · 35.2% accepted · 3,932 likes · top 13%

play →

Description

Given a positive integer n, find the smallest integer that uses exactly the same set of digits as n but has a greater value. Return -1 if no such integer exists, or if the answer would exceed the 32-bit signed integer range.

Example 1:

Input: n = 12
Output: 21

Example 2:

Input: n = 21
Output: -1

Code

1
2
3