-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path24_Next_Greater_Numerically_Balanced_Number.cpp
More file actions
88 lines (73 loc) · 2.07 KB
/
Copy path24_Next_Greater_Numerically_Balanced_Number.cpp
File metadata and controls
88 lines (73 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// 2048. Next Greater Numerically Balanced Number
// An integer x is numerically balanced if for every digit d in the number x, there are exactly d occurrences of that digit in x.
// Given an integer n, return the smallest numerically balanced number strictly greater than n.
// Example 1:
// Input: n = 1
// Output: 22
// Explanation:
// 22 is numerically balanced since:
// - The digit 2 occurs 2 times.
// It is also the smallest numerically balanced number strictly greater than 1.
// Example 2:
// Input: n = 1000
// Output: 1333
// Explanation:
// 1333 is numerically balanced since:
// - The digit 1 occurs 1 time.
// - The digit 3 occurs 3 times.
// It is also the smallest numerically balanced number strictly greater than 1000.
// Note that 1022 cannot be the answer because 0 appeared more than 0 times.
// Example 3:
// Input: n = 3000
// Output: 3133
// Explanation:
// 3133 is numerically balanced since:
// - The digit 1 occurs 1 time.
// - The digit 3 occurs 3 times.
// It is also the smallest numerically balanced number strictly greater than 3000.
// Constraints:
// 0 <= n <= 106
class Solution
{
public:
int nextBeautifulNumber(int n)
{
vector<int> list;
vector<int> count(10, 0);
generate(0, count, list);
sort(list.begin(), list.end());
for (int num : list)
{
if (num > n)
return num;
}
return -1;
}
void generate(long num, vector<int> &count, vector<int> &list)
{
if (num > 0 && isBeautiful(count))
{
list.push_back((int)num);
}
if (num > 1224444)
return;
for (int d = 1; d <= 7; ++d)
{
if (count[d] < d)
{
count[d]++;
generate(num * 10 + d, count, list);
count[d]--;
}
}
}
bool isBeautiful(const vector<int> &count)
{
for (int d = 1; d <= 7; ++d)
{
if (count[d] != 0 && count[d] != d)
return false;
}
return true;
}
};