Skip to content

Commit 2ade258

Browse files
authored
feat: add solutions to lc problem: No.2048 (#2067)
No.2048.Next Greater Numerically Balanced Number
1 parent ba7bb98 commit 2ade258

File tree

7 files changed

+213
-239
lines changed

7 files changed

+213
-239
lines changed

solution/2000-2099/2048.Next Greater Numerically Balanced Number/README.md

+63-67
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@
5959

6060
<!-- 这里可写通用的实现逻辑 -->
6161

62+
**方法一:枚举**
63+
64+
我们注意到,题目中 $n$ 的范围是 $[0, 10^6]$,而大于 $10^6$ 的其中一个数值平衡数是 $1224444$,因此我们直接枚举 $x \in [n + 1, ..]$,然后判断 $x$ 是否是数值平衡数即可。枚举的 $x$ 一定不会超过 $1224444$。
65+
66+
时间复杂度 $O(M - n)$,其中 $M = 1224444$。空间复杂度 $O(1)$。
67+
6268
<!-- tabs:start -->
6369

6470
### **Python3**
@@ -68,20 +74,14 @@
6874
```python
6975
class Solution:
7076
def nextBeautifulNumber(self, n: int) -> int:
71-
def check(num):
72-
counter = [0] * 10
73-
for c in str(num):
74-
counter[int(c)] += 1
75-
76-
for c in str(num):
77-
if counter[int(c)] != int(c):
78-
return False
79-
return True
80-
81-
for i in range(n + 1, 10**7):
82-
if check(i):
83-
return i
84-
return -1
77+
for x in count(n + 1):
78+
y = x
79+
cnt = [0] * 10
80+
while y:
81+
y, v = divmod(y, 10)
82+
cnt[v] += 1
83+
if all(v == 0 or i == v for i, v in enumerate(cnt)):
84+
return x
8585
```
8686

8787
### **Java**
@@ -115,51 +115,28 @@ class Solution {
115115
}
116116
```
117117

118-
## **TypeScript**
119-
120-
```ts
121-
function nextBeautifulNumber(n: number): number {
122-
for (let ans = n + 1; ; ans++) {
123-
if (isValid(ans)) {
124-
return ans;
125-
}
126-
}
127-
}
128-
129-
function isValid(n: number): boolean {
130-
let record = new Array(10).fill(0);
131-
while (n > 0) {
132-
const idx = n % 10;
133-
record[idx]++;
134-
n = Math.floor(n / 10);
135-
}
136-
for (let i = 0; i < 10; i++) {
137-
if (record[i] && record[i] != i) return false;
138-
}
139-
return true;
140-
}
141-
```
142-
143118
### **C++**
144119

145120
```cpp
146121
class Solution {
147122
public:
148123
int nextBeautifulNumber(int n) {
149-
for (int i = n + 1; i < 10000000; ++i) {
150-
if (check(i)) return i;
151-
}
152-
return -1;
153-
}
154-
155-
bool check(int num) {
156-
string s = to_string(num);
157-
vector<int> counter(10);
158-
for (char c : s) ++counter[c - '0'];
159-
for (char c : s) {
160-
if (counter[c - '0'] != c - '0') return false;
124+
for (int x = n + 1;; ++x) {
125+
int cnt[10]{};
126+
for (int y = x; y > 0; y /= 10) {
127+
++cnt[y % 10];
128+
}
129+
bool ok = true;
130+
for (int y = x; y > 0; y /= 10) {
131+
if (y % 10 != cnt[y % 10]) {
132+
ok = false;
133+
break;
134+
}
135+
}
136+
if (ok) {
137+
return x;
138+
}
161139
}
162-
return true;
163140
}
164141
};
165142
```
@@ -168,26 +145,45 @@ public:
168145
169146
```go
170147
func nextBeautifulNumber(n int) int {
171-
check := func(num int) bool {
172-
s := strconv.Itoa(num)
173-
counter := make([]int, 10)
174-
for _, c := range s {
175-
counter[int(c-'0')]++
148+
for x := n + 1; ; x++ {
149+
cnt := [10]int{}
150+
for y := x; y > 0; y /= 10 {
151+
cnt[y%10]++
176152
}
177-
for _, c := range s {
178-
if counter[int(c-'0')] != int(c-'0') {
179-
return false
153+
ok := true
154+
for y := x; y > 0; y /= 10 {
155+
if y%10 != cnt[y%10] {
156+
ok = false
157+
break
180158
}
181159
}
182-
return true
183-
}
184-
185-
for i := n + 1; i <= 10000000; i++ {
186-
if check(i) {
187-
return i
160+
if ok {
161+
return x
188162
}
189163
}
190-
return -1
164+
}
165+
```
166+
167+
### **TypeScript**
168+
169+
```ts
170+
function nextBeautifulNumber(n: number): number {
171+
for (let x = n + 1; ; ++x) {
172+
const cnt: number[] = Array(10).fill(0);
173+
for (let y = x; y > 0; y = (y / 10) | 0) {
174+
cnt[y % 10]++;
175+
}
176+
let ok = true;
177+
for (let i = 0; i < 10; ++i) {
178+
if (cnt[i] && cnt[i] !== i) {
179+
ok = false;
180+
break;
181+
}
182+
}
183+
if (ok) {
184+
return x;
185+
}
186+
}
191187
}
192188
```
193189

solution/2000-2099/2048.Next Greater Numerically Balanced Number/README_EN.md

+76-84
Original file line numberDiff line numberDiff line change
@@ -54,80 +54,51 @@ It is also the smallest numerically balanced number strictly greater than 3000.
5454

5555
## Solutions
5656

57+
**Solution 1: Enumeration**
58+
59+
We note that the range of $n$ in the problem is $[0, 10^6]$, and one of the balanced numbers greater than $10^6$ is $1224444$. Therefore, we directly enumerate $x \in [n + 1, ..]$ and then judge whether $x$ is a balanced number. The enumerated $x$ will definitely not exceed $1224444$.
60+
61+
The time complexity is $O(M - n)$, where $M = 1224444$. The space complexity is $O(1)$.
62+
5763
<!-- tabs:start -->
5864

5965
### **Python3**
6066

6167
```python
6268
class Solution:
6369
def nextBeautifulNumber(self, n: int) -> int:
64-
def check(num):
65-
counter = [0] * 10
66-
for c in str(num):
67-
counter[int(c)] += 1
68-
69-
for c in str(num):
70-
if counter[int(c)] != int(c):
71-
return False
72-
return True
73-
74-
for i in range(n + 1, 10**7):
75-
if check(i):
76-
return i
77-
return -1
70+
for x in count(n + 1):
71+
y = x
72+
cnt = [0] * 10
73+
while y:
74+
y, v = divmod(y, 10)
75+
cnt[v] += 1
76+
if all(v == 0 or i == v for i, v in enumerate(cnt)):
77+
return x
7878
```
7979

8080
### **Java**
8181

8282
```java
8383
class Solution {
8484
public int nextBeautifulNumber(int n) {
85-
for (int i = n + 1; i < 10000000; ++i) {
86-
if (check(i)) {
87-
return i;
85+
for (int x = n + 1;; ++x) {
86+
int[] cnt = new int[10];
87+
for (int y = x; y > 0; y /= 10) {
88+
++cnt[y % 10];
8889
}
89-
}
90-
return -1;
91-
}
92-
93-
private boolean check(int num) {
94-
int[] counter = new int[10];
95-
char[] chars = String.valueOf(num).toCharArray();
96-
for (char c : chars) {
97-
++counter[c - '0'];
98-
}
99-
for (char c : chars) {
100-
if (counter[c - '0'] != c - '0') {
101-
return false;
90+
boolean ok = true;
91+
for (int y = x; y > 0; y /= 10) {
92+
if (y % 10 != cnt[y % 10]) {
93+
ok = false;
94+
break;
95+
}
96+
}
97+
if (ok) {
98+
return x;
10299
}
103100
}
104-
return true;
105-
}
106-
}
107-
```
108-
109-
### **TypeScript**
110-
111-
```ts
112-
function nextBeautifulNumber(n: number): number {
113-
for (let ans = n + 1; ; ans++) {
114-
if (isValid(ans)) {
115-
return ans;
116-
}
117-
}
118-
}
119-
120-
function isValid(n: number): boolean {
121-
let record = new Array(10).fill(0);
122-
while (n > 0) {
123-
const idx = n % 10;
124-
record[idx]++;
125-
n = Math.floor(n / 10);
126-
}
127-
for (let i = 0; i < 10; i++) {
128-
if (record[i] && record[i] != i) return false;
129101
}
130-
return true;
131102
}
132103
```
133104

@@ -137,20 +108,22 @@ function isValid(n: number): boolean {
137108
class Solution {
138109
public:
139110
int nextBeautifulNumber(int n) {
140-
for (int i = n + 1; i < 10000000; ++i) {
141-
if (check(i)) return i;
142-
}
143-
return -1;
144-
}
145-
146-
bool check(int num) {
147-
string s = to_string(num);
148-
vector<int> counter(10);
149-
for (char c : s) ++counter[c - '0'];
150-
for (char c : s) {
151-
if (counter[c - '0'] != c - '0') return false;
111+
for (int x = n + 1;; ++x) {
112+
int cnt[10]{};
113+
for (int y = x; y > 0; y /= 10) {
114+
++cnt[y % 10];
115+
}
116+
bool ok = true;
117+
for (int y = x; y > 0; y /= 10) {
118+
if (y % 10 != cnt[y % 10]) {
119+
ok = false;
120+
break;
121+
}
122+
}
123+
if (ok) {
124+
return x;
125+
}
152126
}
153-
return true;
154127
}
155128
};
156129
```
@@ -159,26 +132,45 @@ public:
159132
160133
```go
161134
func nextBeautifulNumber(n int) int {
162-
check := func(num int) bool {
163-
s := strconv.Itoa(num)
164-
counter := make([]int, 10)
165-
for _, c := range s {
166-
counter[int(c-'0')]++
135+
for x := n + 1; ; x++ {
136+
cnt := [10]int{}
137+
for y := x; y > 0; y /= 10 {
138+
cnt[y%10]++
167139
}
168-
for _, c := range s {
169-
if counter[int(c-'0')] != int(c-'0') {
170-
return false
140+
ok := true
141+
for y := x; y > 0; y /= 10 {
142+
if y%10 != cnt[y%10] {
143+
ok = false
144+
break
171145
}
172146
}
173-
return true
174-
}
175-
176-
for i := n + 1; i <= 10000000; i++ {
177-
if check(i) {
178-
return i
147+
if ok {
148+
return x
179149
}
180150
}
181-
return -1
151+
}
152+
```
153+
154+
### **TypeScript**
155+
156+
```ts
157+
function nextBeautifulNumber(n: number): number {
158+
for (let x = n + 1; ; ++x) {
159+
const cnt: number[] = Array(10).fill(0);
160+
for (let y = x; y > 0; y = (y / 10) | 0) {
161+
cnt[y % 10]++;
162+
}
163+
let ok = true;
164+
for (let i = 0; i < 10; ++i) {
165+
if (cnt[i] && cnt[i] !== i) {
166+
ok = false;
167+
break;
168+
}
169+
}
170+
if (ok) {
171+
return x;
172+
}
173+
}
182174
}
183175
```
184176

0 commit comments

Comments
 (0)