Skip to content

Commit 16041e1

Browse files
committed
chore: update 2048. Next Greater Numerically Balanced Number
1 parent 6a6c1ac commit 16041e1

4 files changed

Lines changed: 126 additions & 0 deletions

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
---
2+
title: 2048. Next Greater Numerically Balanced Number
3+
description: 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.
4+
keywords:
5+
[
6+
LeetCode,
7+
2048. Next Greater Numerically Balanced Number,
8+
Medium,
9+
Hash Table,
10+
Math,
11+
Backtracking,
12+
Counting,
13+
Enumeration,
14+
]
15+
tags: [LeetCode, Medium, Hash Table, Math, Backtracking, Counting, Enumeration]
16+
---
17+
18+
# [{frontMatter.title}](https://leetcode.com/problems/next-greater-numerically-balanced-number/)
19+
20+
import Tabs from "@theme/Tabs";
21+
import TabItem from "@theme/TabItem";
22+
import CodeBlock from "@theme/CodeBlock";
23+
import DifficultyBadge from "@site/src/components/Badges/DifficultyBadge";
24+
import Solution from "!!raw-loader!./solution.js";
25+
import Description from "./_Description.md";
26+
import Examples from "./_Examples.md";
27+
28+
<DifficultyBadge difficulty="Medium" />
29+
30+
<Tabs>
31+
<TabItem value="description" label="題目描述" default>
32+
<details open>
33+
<summary>Description</summary>
34+
<Description />
35+
<Examples />
36+
</details>
37+
</TabItem>
38+
39+
<TabItem value="solution" label="解答">
40+
## Solution
41+
<CodeBlock language="js">{Solution}</CodeBlock>
42+
</TabItem>
43+
</Tabs>
44+
45+
## 解題思路
46+
47+
Discussion 有個人貼了所有的 ["beautiful" numbers 的清單](https://leetcode.com/problems/next-greater-numerically-balanced-number/description/comments/3211016/)
48+
所以就用 for 迴圈找出最小且符合條件的數
49+
50+
51+
```js
52+
for (let i of beautifulNumbers) {
53+
if (i > n) {
54+
return i;
55+
}
56+
}
57+
```
58+
59+
## 心得
60+
61+
![](https://media1.tenor.com/m/N_bXE4MWdEMAAAAd/big-brain-markiplier.gif)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
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.
2+
3+
Given an integer n, return the smallest numerically balanced number strictly greater than n.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#### Example 1:
2+
3+
> **Input:** n = 1
4+
> **Output:** 22
5+
> **Explanation:**
6+
> 22 is numerically balanced since:
7+
>
8+
> - The digit 2 occurs 2 times.
9+
> It is also the smallest numerically balanced number strictly greater than 1.
10+
11+
#### Example 2:
12+
13+
> **Input:** n = 1000
14+
> **Output:** 1333
15+
> **Explanation:**
16+
> 1333 is numerically balanced since:
17+
>
18+
> - The digit 1 occurs 1 time.
19+
> - The digit 3 occurs 3 times.
20+
> It is also the smallest numerically balanced number strictly greater than 1000.
21+
> Note that 1022 cannot be the answer because 0 appeared more than 0 times.
22+
23+
#### Example 3:
24+
25+
> **Input:** n = 3000
26+
> **Output:** 3133
27+
> **Explanation:**
28+
> 3133 is numerically balanced since:
29+
>
30+
> - The digit 1 occurs 1 time.
31+
> - The digit 3 occurs 3 times.
32+
> It is also the smallest numerically balanced number strictly greater than 3000.
33+
34+
## Constraints:
35+
36+
- 0 `<=` n `<=` $10^6$
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {number} n
3+
* @return {number}
4+
*/
5+
var nextBeautifulNumber = function (n) {
6+
const beautifulNumbers = [
7+
1, 22, 122, 212, 221, 333, 1333, 3133, 3313, 3331, 4444, 14444, 22333,
8+
23233, 23323, 23332, 32233, 32323, 32332, 33223, 33232, 33322, 41444, 44144,
9+
44414, 44441, 55555, 122333, 123233, 123323, 123332, 132233, 132323, 132332,
10+
133223, 133232, 133322, 155555, 212333, 213233, 213323, 213332, 221333,
11+
223133, 223313, 223331, 224444, 231233, 231323, 231332, 232133, 232313,
12+
232331, 233123, 233132, 233213, 233231, 233312, 233321, 242444, 244244,
13+
244424, 244442, 312233, 312323, 312332, 313223, 313232, 313322, 321233,
14+
321323, 321332, 322133, 322313, 322331, 323123, 323132, 323213, 323231,
15+
323312, 323321, 331223, 331232, 331322, 332123, 332132, 332213, 332231,
16+
332312, 332321, 333122, 333212, 333221, 422444, 424244, 424424, 424442,
17+
442244, 442424, 442442, 444224, 444242, 444422, 515555, 551555, 555155,
18+
555515, 555551, 666666, 1224444,
19+
];
20+
21+
for (let i of beautifulNumbers) {
22+
if (i > n) {
23+
return i;
24+
}
25+
}
26+
};

0 commit comments

Comments
 (0)