Skip to content

Commit 70538c7

Browse files
authored
Create 2048-next-greater-numerically-balanced-number.js
1 parent 57f1c62 commit 70538c7

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {number} n
3+
* @return {number}
4+
*/
5+
const nextBeautifulNumber = function(n) {
6+
while (true) {
7+
++n;
8+
if (balance(n)) return n;
9+
}
10+
function balance(n) {
11+
let cnt = Array(10).fill(0);
12+
while (n) {
13+
if (n % 10 == 0) return false; // no 0 allowed
14+
cnt[n % 10]++;
15+
n = ~~(n / 10);
16+
}
17+
for (let i = 1; i < 10; ++i) {
18+
if (cnt[i] && cnt[i] !== i) return false;
19+
}
20+
return true;
21+
}
22+
};
23+

0 commit comments

Comments
 (0)