Skip to content

Commit 39a6e50

Browse files
committed
feat: add solutions to lc problem: No.2417
No.2417.Closest Fair Integer
1 parent 2fd1c7b commit 39a6e50

17 files changed

+503
-43
lines changed

solution/1600-1699/1686.Stone Game VI/Solution.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
class Solution:
22
def stoneGameVI(self, aliceValues: List[int], bobValues: List[int]) -> int:
3-
arr = [(a + b, i)
4-
for i, (a, b) in enumerate(zip(aliceValues, bobValues))]
3+
arr = [(a + b, i) for i, (a, b) in enumerate(zip(aliceValues, bobValues))]
54
arr.sort(reverse=True)
65
a = sum(aliceValues[v[1]] for i, v in enumerate(arr) if i % 2 == 0)
76
b = sum(bobValues[v[1]] for i, v in enumerate(arr) if i % 2 == 1)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
# [2417. Closest Fair Integer](https://leetcode.cn/problems/closest-fair-integer)
2+
3+
[English Version](/solution/2400-2499/2417.Closest%20Fair%20Integer/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>You are given a <strong>positive</strong> integer <code>n</code>.</p>
10+
11+
<p>We call an integer <code>k</code> fair if the number of <strong>even</strong> digits in <code>k</code> is equal to the number of <strong>odd</strong> digits in it.</p>
12+
13+
<p>Return <em>the <strong>smallest</strong> fair integer that is <strong>greater than or equal</strong> to </em><code>n</code>.</p>
14+
15+
<p>&nbsp;</p>
16+
<p><strong class="example">Example 1:</strong></p>
17+
18+
<pre>
19+
<strong>Input:</strong> n = 2
20+
<strong>Output:</strong> 10
21+
<strong>Explanation:</strong> The smallest fair integer that is greater than or equal to 2 is 10.
22+
10 is fair because it has an equal number of even and odd digits (one odd digit and one even digit).</pre>
23+
24+
<p><strong class="example">Example 2:</strong></p>
25+
26+
<pre>
27+
<strong>Input:</strong> n = 403
28+
<strong>Output:</strong> 1001
29+
<strong>Explanation:</strong> The smallest fair integer that is greater than or equal to 403 is 1001.
30+
1001 is fair because it has an equal number of even and odd digits (two odd digits and two even digits).
31+
</pre>
32+
33+
<p>&nbsp;</p>
34+
<p><strong>Constraints:</strong></p>
35+
36+
<ul>
37+
<li><code>1 &lt;= n &lt;= 10<sup>9</sup></code></li>
38+
</ul>
39+
40+
## 解法
41+
42+
<!-- 这里可写通用的实现逻辑 -->
43+
44+
**方法一:分类讨论**
45+
46+
我们记 $n$ 的位数为 $k$,奇数位数、偶数位数分别为 $a$ 和 $b$。
47+
48+
- 若 $a=b$,则 $n$ 本身就是 `fair` 的,直接返回 $n$ 即可;
49+
- 否则,若 $k$ 为奇数,那么我们找到 $k+1$ 位的最小 `fair` 数即可,形如 `10000111`;若 $k$ 为偶数,我们直接暴力递归 `closestFair(n+1)` 即可。
50+
51+
时间复杂度 $O(\sqrt{n} \times \log_{10} n)$。
52+
53+
<!-- tabs:start -->
54+
55+
### **Python3**
56+
57+
<!-- 这里可写当前语言的特殊实现逻辑 -->
58+
59+
```python
60+
class Solution:
61+
def closestFair(self, n: int) -> int:
62+
a = b = k = 0
63+
t = n
64+
while t:
65+
if (t % 10) & 1:
66+
a += 1
67+
else:
68+
b += 1
69+
t //= 10
70+
k += 1
71+
if k & 1:
72+
x = 10**k
73+
y = int('1' * (k >> 1) or '0')
74+
return x + y
75+
if a == b:
76+
return n
77+
return self.closestFair(n + 1)
78+
```
79+
80+
### **Java**
81+
82+
<!-- 这里可写当前语言的特殊实现逻辑 -->
83+
84+
```java
85+
class Solution {
86+
public int closestFair(int n) {
87+
int a = 0, b = 0;
88+
int k = 0, t = n;
89+
while (t > 0) {
90+
if ((t % 10) % 2 == 1) {
91+
++a;
92+
} else {
93+
++b;
94+
}
95+
t /= 10;
96+
++k;
97+
}
98+
if (k % 2 == 1) {
99+
int x = (int) Math.pow(10, k);
100+
int y = 0;
101+
for (int i = 0; i < k >> 1; ++i) {
102+
y = y * 10 + 1;
103+
}
104+
return x + y;
105+
}
106+
if (a == b) {
107+
return n;
108+
}
109+
return closestFair(n + 1);
110+
}
111+
}
112+
```
113+
114+
### **C++**
115+
116+
```cpp
117+
class Solution {
118+
public:
119+
int closestFair(int n) {
120+
int a = 0, b = 0;
121+
int t = n, k = 0;
122+
while (t) {
123+
if ((t % 10) & 1) {
124+
++a;
125+
} else {
126+
++b;
127+
}
128+
++k;
129+
t /= 10;
130+
}
131+
if (a == b) {
132+
return n;
133+
}
134+
if (k % 2 == 1) {
135+
int x = pow(10, k);
136+
int y = 0;
137+
for (int i = 0; i < k >> 1; ++i) {
138+
y = y * 10 + 1;
139+
}
140+
return x + y;
141+
}
142+
return closestFair(n + 1);
143+
}
144+
};
145+
```
146+
147+
### **Go**
148+
149+
```go
150+
func closestFair(n int) int {
151+
a, b := 0, 0
152+
t, k := n, 0
153+
for t > 0 {
154+
if (t%10)%2 == 1 {
155+
a++
156+
} else {
157+
b++
158+
}
159+
k++
160+
t /= 10
161+
}
162+
if a == b {
163+
return n
164+
}
165+
if k%2 == 1 {
166+
x := int(math.Pow(10, float64(k)))
167+
y := 0
168+
for i := 0; i < k>>1; i++ {
169+
y = y*10 + 1
170+
}
171+
return x + y
172+
}
173+
return closestFair(n + 1)
174+
}
175+
```
176+
177+
### **TypeScript**
178+
179+
```ts
180+
181+
```
182+
183+
### **...**
184+
185+
```
186+
187+
```
188+
189+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
# [2417. Closest Fair Integer](https://leetcode.com/problems/closest-fair-integer)
2+
3+
[中文文档](/solution/2400-2499/2417.Closest%20Fair%20Integer/README.md)
4+
5+
## Description
6+
7+
<p>You are given a <strong>positive</strong> integer <code>n</code>.</p>
8+
9+
<p>We call an integer <code>k</code> fair if the number of <strong>even</strong> digits in <code>k</code> is equal to the number of <strong>odd</strong> digits in it.</p>
10+
11+
<p>Return <em>the <strong>smallest</strong> fair integer that is <strong>greater than or equal</strong> to </em><code>n</code>.</p>
12+
13+
<p>&nbsp;</p>
14+
<p><strong class="example">Example 1:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> n = 2
18+
<strong>Output:</strong> 10
19+
<strong>Explanation:</strong> The smallest fair integer that is greater than or equal to 2 is 10.
20+
10 is fair because it has an equal number of even and odd digits (one odd digit and one even digit).</pre>
21+
22+
<p><strong class="example">Example 2:</strong></p>
23+
24+
<pre>
25+
<strong>Input:</strong> n = 403
26+
<strong>Output:</strong> 1001
27+
<strong>Explanation:</strong> The smallest fair integer that is greater than or equal to 403 is 1001.
28+
1001 is fair because it has an equal number of even and odd digits (two odd digits and two even digits).
29+
</pre>
30+
31+
<p>&nbsp;</p>
32+
<p><strong>Constraints:</strong></p>
33+
34+
<ul>
35+
<li><code>1 &lt;= n &lt;= 10<sup>9</sup></code></li>
36+
</ul>
37+
38+
## Solutions
39+
40+
<!-- tabs:start -->
41+
42+
### **Python3**
43+
44+
```python
45+
class Solution:
46+
def closestFair(self, n: int) -> int:
47+
a = b = k = 0
48+
t = n
49+
while t:
50+
if (t % 10) & 1:
51+
a += 1
52+
else:
53+
b += 1
54+
t //= 10
55+
k += 1
56+
if k & 1:
57+
x = 10**k
58+
y = int('1' * (k >> 1) or '0')
59+
return x + y
60+
if a == b:
61+
return n
62+
return self.closestFair(n + 1)
63+
```
64+
65+
### **Java**
66+
67+
```java
68+
class Solution {
69+
public int closestFair(int n) {
70+
int a = 0, b = 0;
71+
int k = 0, t = n;
72+
while (t > 0) {
73+
if ((t % 10) % 2 == 1) {
74+
++a;
75+
} else {
76+
++b;
77+
}
78+
t /= 10;
79+
++k;
80+
}
81+
if (k % 2 == 1) {
82+
int x = (int) Math.pow(10, k);
83+
int y = 0;
84+
for (int i = 0; i < k >> 1; ++i) {
85+
y = y * 10 + 1;
86+
}
87+
return x + y;
88+
}
89+
if (a == b) {
90+
return n;
91+
}
92+
return closestFair(n + 1);
93+
}
94+
}
95+
```
96+
97+
### **C++**
98+
99+
```cpp
100+
class Solution {
101+
public:
102+
int closestFair(int n) {
103+
int a = 0, b = 0;
104+
int t = n, k = 0;
105+
while (t) {
106+
if ((t % 10) & 1) {
107+
++a;
108+
} else {
109+
++b;
110+
}
111+
++k;
112+
t /= 10;
113+
}
114+
if (a == b) {
115+
return n;
116+
}
117+
if (k % 2 == 1) {
118+
int x = pow(10, k);
119+
int y = 0;
120+
for (int i = 0; i < k >> 1; ++i) {
121+
y = y * 10 + 1;
122+
}
123+
return x + y;
124+
}
125+
return closestFair(n + 1);
126+
}
127+
};
128+
```
129+
130+
### **Go**
131+
132+
```go
133+
func closestFair(n int) int {
134+
a, b := 0, 0
135+
t, k := n, 0
136+
for t > 0 {
137+
if (t%10)%2 == 1 {
138+
a++
139+
} else {
140+
b++
141+
}
142+
k++
143+
t /= 10
144+
}
145+
if a == b {
146+
return n
147+
}
148+
if k%2 == 1 {
149+
x := int(math.Pow(10, float64(k)))
150+
y := 0
151+
for i := 0; i < k>>1; i++ {
152+
y = y*10 + 1
153+
}
154+
return x + y
155+
}
156+
return closestFair(n + 1)
157+
}
158+
```
159+
160+
### **TypeScript**
161+
162+
```ts
163+
164+
```
165+
166+
### **...**
167+
168+
```
169+
170+
```
171+
172+
<!-- tabs:end -->

0 commit comments

Comments
 (0)