File tree 1 file changed +8
-7
lines changed
scripts/algorithms/N/Next Greater Numerically Balanced Number
1 file changed +8
-7
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 207 ms (Top 46.27%) | Memory: 117.2 MB (Top 26.87%)
1
2
class Solution {
2
3
public int nextBeautifulNumber (int n ) {
3
4
4
5
while (true ){
5
6
n ++;
6
7
int num = n ; //test this number
7
8
int [] freq = new int [10 ]; // 0 to 9
8
-
9
- while (num > 0 ){ //calculate freq of each digit in the num
9
+
10
+ while (num > 0 ){ //calculate freq of each digit in the num
10
11
int rem = num % 10 ; //this is remainder
11
- num = num / 10 ; //this is quotient
12
+ num = num / 10 ; //this is quotient
12
13
freq [rem ] = freq [rem ] + 1 ; //increase its frequency
13
14
if (freq [rem ] > rem ) break ;
14
15
}
15
-
16
+
16
17
boolean ans = true ;
17
-
18
+
18
19
for (int i = 0 ;i <10 ;i ++){ //check frequency of each digit
19
20
if (freq [i ] != i && freq [i ] != 0 ){
20
21
ans = false ;
21
22
break ;
22
23
}
23
24
}
24
-
25
+
25
26
if (ans == true ){
26
27
return n ;
27
28
}
28
29
}
29
30
}
30
- }
31
+ }
You can’t perform that action at this time.
0 commit comments