Next Greater Numerically Balanced Number LeetCode solution in Python
1class Solution:2def nextBeautifulNumber(self, n: int) -> int:3n_digits = len(str(n))45next_max = {61: [1],72: [22],83: [122, 333],94: [1333, 4444],105: [14444, 22333, 55555],116: [122333, 224444, 666666, 155555],127: [1224444, 2255555, 3334444, 1666666, 7777777],13}1415if n >= int(str(n_digits) * n_digits):16n_digits += 117return min(next_max[n_digits])1819ans = float("inf")20for num in sorted(next_max[n_digits]):21cands = set(permutations(str(num)))22cands = sorted(map(lambda x: int("".join(x)), cands))2324loc = bisect.bisect(cands, n)25if loc < len(cands):26ans = min(ans, cands[loc])2728return ans