Got asked below question, only difference was the input was provided as a string. I hadn't practiced it before but wanted to give back to the community. All the best!
https://leetcode.com/problems/next-greater-element-iii/description/
Working solution:
class Solution:
def nextGreaterElement(self, n: int) -> int:
s = [x for x in str(n)]
n_list = [int(i) for i in s]
end = len(n_list)-2
while end >= 0 and n_list[end] >= n_list[end + 1]:
end -= 1
if end < 0:
return -1
j = len(n_list) - 1
while n_list[end] >= n_list[j]:
j -= 1
# swap
n_list[end], n_list[j] = n_list[j], n_list[end]
# reverse
n_list[end+1:] = n_list[end+1:][::-1]
s = [str(x) for x in n_list]
next_greater = int("".join(s))
return next_greater if next_greater <= 2**31 - 1 else -1