Skip to content

Commit 114f69b

Browse files
committed
feat: add solutions to lc problem: No.2457
No.2457.Minimum Addition to Make Integer Beautiful
1 parent b342f15 commit 114f69b

File tree

7 files changed

+131
-68
lines changed

7 files changed

+131
-68
lines changed

solution/2400-2499/2457.Minimum Addition to Make Integer Beautiful/README.md

+53-25
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,24 @@
5050

5151
**方法一:贪心**
5252

53-
我们定义函数 $f(x)$ 表示一个整数 $x$ 的每一位数字之和,那么题目要求的最小非负整数 $x$ 就是 $f(n + x) \leq target$ 的最小值
53+
我们定义函数 $f(x)$ 表示一个整数 $x$ 的每一位数字之和,那么题目求的是 $f(n + x) \leq target$ 的最小非负整数 $x$
5454

55-
初始化 $x = 0$,循环判断 $f(n+x)$ 是否大于 $target$,如果大于,此时 $n+x$ 的最低一位非 $0$ 的数要置为 $0$,而前一位要加 $1$,然后继续判断。
55+
如果 $y = n+x$ 的每一位数字之和大于 $target$,那么我们可以循环通过以下操作,将 $y$ 的每一位数字之和减小到小于等于 $target$:
56+
57+
- 找到 $y$ 的最低位的非零数字,将其减小到 $0$,并将其高一位的数字加 $1$;
58+
- 更新 $x$,继续上述操作,直到 $n+x$ 的每一位数字之和小于等于 $target$。
5659

5760
循环结束,返回 $x$ 即可。
5861

59-
时间复杂度 $O(\log^2 n)$。
62+
我们可以举个例子,假设 $n=467$, $target=6$,那么 $n$ 的变化过程如下:
63+
64+
$$
65+
\begin{aligned}
66+
& 467 \rightarrow 470 \rightarrow 500 \\
67+
\end{aligned}
68+
$$
69+
70+
时间复杂度 $O(\log^2 n)$,其中 $n$ 给题目给定的整数。空间复杂度 $O(1)$。
6071

6172
<!-- tabs:start -->
6273

@@ -67,12 +78,12 @@
6778
```python
6879
class Solution:
6980
def makeIntegerBeautiful(self, n: int, target: int) -> int:
70-
def f(x):
71-
v = 0
81+
def f(x: int) -> int:
82+
y = 0
7283
while x:
73-
v += x % 10
84+
y += x % 10
7485
x //= 10
75-
return v
86+
return y
7687

7788
x = 0
7889
while f(n + x) > target:
@@ -106,32 +117,32 @@ class Solution {
106117
}
107118

108119
private int f(long x) {
109-
int v = 0;
120+
int y = 0;
110121
while (x > 0) {
111-
v += x % 10;
122+
y += x % 10;
112123
x /= 10;
113124
}
114-
return v;
125+
return y;
115126
}
116127
}
117128
```
118129

119130
### **C++**
120131

121132
```cpp
122-
using ll = long long;
123-
124133
class Solution {
125134
public:
126135
long long makeIntegerBeautiful(long long n, int target) {
136+
using ll = long long;
127137
auto f = [](ll x) {
128-
int v = 0;
138+
int y = 0;
129139
while (x) {
130-
v += x % 10;
140+
y += x % 10;
131141
x /= 10;
132142
}
133-
return v;
143+
return y;
134144
};
145+
135146
ll x = 0;
136147
while (f(n + x) > target) {
137148
ll y = n + x;
@@ -150,16 +161,13 @@ public:
150161
### **Go**
151162

152163
```go
153-
func makeIntegerBeautiful(n int64, target int) int64 {
154-
f := func(x int64) int {
155-
v := 0
156-
for x > 0 {
157-
v += int(x % 10)
158-
x /= 10
164+
func makeIntegerBeautiful(n int64, target int) (x int64) {
165+
f := func(x int64) (y int) {
166+
for ; x > 0; x /= 10 {
167+
y += int(x % 10)
159168
}
160-
return v
169+
return
161170
}
162-
var x int64
163171
for f(n+x) > target {
164172
y := n + x
165173
var p int64 = 10
@@ -169,14 +177,34 @@ func makeIntegerBeautiful(n int64, target int) int64 {
169177
}
170178
x = (y/10+1)*p - n
171179
}
172-
return x
180+
return
173181
}
174182
```
175183

176184
### **TypeScript**
177185

178186
```ts
179-
187+
function makeIntegerBeautiful(n: number, target: number): number {
188+
const f = (x: number): number => {
189+
let y = 0;
190+
for (; x > 0; x = Math.floor(x / 10)) {
191+
y += x % 10;
192+
}
193+
return y;
194+
};
195+
196+
let x = 0;
197+
while (f(n + x) > target) {
198+
let y = n + x;
199+
let p = 10;
200+
while (y % 10 === 0) {
201+
y = Math.floor(y / 10);
202+
p *= 10;
203+
}
204+
x = (Math.floor(y / 10) + 1) * p - n;
205+
}
206+
return x;
207+
}
180208
```
181209

182210
### **...**

solution/2400-2499/2457.Minimum Addition to Make Integer Beautiful/README_EN.md

+39-22
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@
5353
```python
5454
class Solution:
5555
def makeIntegerBeautiful(self, n: int, target: int) -> int:
56-
def f(x):
57-
v = 0
56+
def f(x: int) -> int:
57+
y = 0
5858
while x:
59-
v += x % 10
59+
y += x % 10
6060
x //= 10
61-
return v
61+
return y
6262

6363
x = 0
6464
while f(n + x) > target:
@@ -90,32 +90,32 @@ class Solution {
9090
}
9191

9292
private int f(long x) {
93-
int v = 0;
93+
int y = 0;
9494
while (x > 0) {
95-
v += x % 10;
95+
y += x % 10;
9696
x /= 10;
9797
}
98-
return v;
98+
return y;
9999
}
100100
}
101101
```
102102

103103
### **C++**
104104

105105
```cpp
106-
using ll = long long;
107-
108106
class Solution {
109107
public:
110108
long long makeIntegerBeautiful(long long n, int target) {
109+
using ll = long long;
111110
auto f = [](ll x) {
112-
int v = 0;
111+
int y = 0;
113112
while (x) {
114-
v += x % 10;
113+
y += x % 10;
115114
x /= 10;
116115
}
117-
return v;
116+
return y;
118117
};
118+
119119
ll x = 0;
120120
while (f(n + x) > target) {
121121
ll y = n + x;
@@ -134,16 +134,13 @@ public:
134134
### **Go**
135135

136136
```go
137-
func makeIntegerBeautiful(n int64, target int) int64 {
138-
f := func(x int64) int {
139-
v := 0
140-
for x > 0 {
141-
v += int(x % 10)
142-
x /= 10
137+
func makeIntegerBeautiful(n int64, target int) (x int64) {
138+
f := func(x int64) (y int) {
139+
for ; x > 0; x /= 10 {
140+
y += int(x % 10)
143141
}
144-
return v
142+
return
145143
}
146-
var x int64
147144
for f(n+x) > target {
148145
y := n + x
149146
var p int64 = 10
@@ -153,14 +150,34 @@ func makeIntegerBeautiful(n int64, target int) int64 {
153150
}
154151
x = (y/10+1)*p - n
155152
}
156-
return x
153+
return
157154
}
158155
```
159156

160157
### **TypeScript**
161158

162159
```ts
163-
160+
function makeIntegerBeautiful(n: number, target: number): number {
161+
const f = (x: number): number => {
162+
let y = 0;
163+
for (; x > 0; x = Math.floor(x / 10)) {
164+
y += x % 10;
165+
}
166+
return y;
167+
};
168+
169+
let x = 0;
170+
while (f(n + x) > target) {
171+
let y = n + x;
172+
let p = 10;
173+
while (y % 10 === 0) {
174+
y = Math.floor(y / 10);
175+
p *= 10;
176+
}
177+
x = (Math.floor(y / 10) + 1) * p - n;
178+
}
179+
return x;
180+
}
164181
```
165182

166183
### **...**

solution/2400-2499/2457.Minimum Addition to Make Integer Beautiful/Solution.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
using ll = long long;
2-
31
class Solution {
42
public:
53
long long makeIntegerBeautiful(long long n, int target) {
4+
using ll = long long;
65
auto f = [](ll x) {
7-
int v = 0;
6+
int y = 0;
87
while (x) {
9-
v += x % 10;
8+
y += x % 10;
109
x /= 10;
1110
}
12-
return v;
11+
return y;
1312
};
13+
1414
ll x = 0;
1515
while (f(n + x) > target) {
1616
ll y = n + x;
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
func makeIntegerBeautiful(n int64, target int) int64 {
2-
f := func(x int64) int {
3-
v := 0
4-
for x > 0 {
5-
v += int(x % 10)
6-
x /= 10
1+
func makeIntegerBeautiful(n int64, target int) (x int64) {
2+
f := func(x int64) (y int) {
3+
for ; x > 0; x /= 10 {
4+
y += int(x % 10)
75
}
8-
return v
6+
return
97
}
10-
var x int64
118
for f(n+x) > target {
129
y := n + x
1310
var p int64 = 10
@@ -17,5 +14,5 @@ func makeIntegerBeautiful(n int64, target int) int64 {
1714
}
1815
x = (y/10+1)*p - n
1916
}
20-
return x
17+
return
2118
}

solution/2400-2499/2457.Minimum Addition to Make Integer Beautiful/Solution.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ public long makeIntegerBeautiful(long n, int target) {
1414
}
1515

1616
private int f(long x) {
17-
int v = 0;
17+
int y = 0;
1818
while (x > 0) {
19-
v += x % 10;
19+
y += x % 10;
2020
x /= 10;
2121
}
22-
return v;
22+
return y;
2323
}
2424
}

solution/2400-2499/2457.Minimum Addition to Make Integer Beautiful/Solution.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
class Solution:
22
def makeIntegerBeautiful(self, n: int, target: int) -> int:
3-
def f(x):
4-
v = 0
3+
def f(x: int) -> int:
4+
y = 0
55
while x:
6-
v += x % 10
6+
y += x % 10
77
x //= 10
8-
return v
8+
return y
99

1010
x = 0
1111
while f(n + x) > target:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function makeIntegerBeautiful(n: number, target: number): number {
2+
const f = (x: number): number => {
3+
let y = 0;
4+
for (; x > 0; x = Math.floor(x / 10)) {
5+
y += x % 10;
6+
}
7+
return y;
8+
};
9+
10+
let x = 0;
11+
while (f(n + x) > target) {
12+
let y = n + x;
13+
let p = 10;
14+
while (y % 10 === 0) {
15+
y = Math.floor(y / 10);
16+
p *= 10;
17+
}
18+
x = (Math.floor(y / 10) + 1) * p - n;
19+
}
20+
return x;
21+
}

0 commit comments

Comments
 (0)