Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 3562880

Browse files
committed
Add solution #2048
1 parent 57cfa88 commit 3562880

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,708 LeetCode solutions in JavaScript
1+
# 1,709 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1570,6 +1570,7 @@
15701570
2044|[Count Number of Maximum Bitwise-OR Subsets](./solutions/2044-count-number-of-maximum-bitwise-or-subsets.js)|Medium|
15711571
2045|[Second Minimum Time to Reach Destination](./solutions/2045-second-minimum-time-to-reach-destination.js)|Hard|
15721572
2047|[Number of Valid Words in a Sentence](./solutions/2047-number-of-valid-words-in-a-sentence.js)|Easy|
1573+
2048|[Next Greater Numerically Balanced Number](./solutions/2048-next-greater-numerically-balanced-number.js)|Medium|
15731574
2053|[Kth Distinct String in an Array](./solutions/2053-kth-distinct-string-in-an-array.js)|Medium|
15741575
2071|[Maximum Number of Tasks You Can Assign](./solutions/2071-maximum-number-of-tasks-you-can-assign.js)|Hard|
15751576
2085|[Count Common Words With One Occurrence](./solutions/2085-count-common-words-with-one-occurrence.js)|Easy|
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* 2048. Next Greater Numerically Balanced Number
3+
* https://leetcode.com/problems/next-greater-numerically-balanced-number/
4+
* Difficulty: Medium
5+
*
6+
* An integer x is numerically balanced if for every digit d in the number x, there are exactly
7+
* d occurrences of that digit in x.
8+
*
9+
* Given an integer n, return the smallest numerically balanced number strictly greater than n.
10+
*/
11+
12+
/**
13+
* @param {number} n
14+
* @return {number}
15+
*/
16+
var nextBeautifulNumber = function(n) {
17+
let candidate = n + 1;
18+
while (candidate <= 10000000) {
19+
if (isBalanced(candidate)) return candidate;
20+
candidate++;
21+
}
22+
23+
return -1;
24+
25+
function isBalanced(num) {
26+
const freq = new Array(10).fill(0);
27+
const str = num.toString();
28+
29+
for (const digit of str) {
30+
freq[digit]++;
31+
}
32+
33+
for (const digit of str) {
34+
if (freq[digit] !== parseInt(digit)) return false;
35+
}
36+
37+
return true;
38+
}
39+
};

0 commit comments

Comments
 (0)