Skip to content

Commit 3aa8afb

Browse files
committed
feat: add new lc problems
1 parent 29f8671 commit 3aa8afb

File tree

27 files changed

+1134
-27
lines changed

27 files changed

+1134
-27
lines changed

solution/1100-1199/1129.Shortest Path with Alternating Colors/README.md

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,14 @@
66

77
<!-- 这里写题目描述 -->
88

9-
<p>在一个有向图中,节点分别标记为&nbsp;<code>0, 1, ..., n-1</code>。图中每条边为红色或者蓝色,且存在自环或平行边。</p>
9+
<p>给定一个整数 <code>n</code>,即有向图中的节点数,其中节点标记为 <code>0</code> 到 <code>n - 1</code>。图中的每条边为红色或者蓝色,并且可能存在自环或平行边。</p>
1010

11-
<p><code>red_edges</code>&nbsp;中的每一个&nbsp;<code>[i, j]</code>&nbsp;对表示从节点 <code>i</code> 到节点 <code>j</code> 的红色有向边。类似地,<code>blue_edges</code>&nbsp;中的每一个&nbsp;<code>[i, j]</code>&nbsp;对表示从节点 <code>i</code> 到节点 <code>j</code> 的蓝色有向边。</p>
11+
<p>给定两个数组&nbsp;<code>redEdges</code>&nbsp;&nbsp;<code>blueEdges</code>,其中:</p>
12+
13+
<ul>
14+
<li><code>redEdges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code>&nbsp;表示图中存在一条从节点&nbsp;<code>a<sub>i</sub></code>&nbsp;到节点&nbsp;<code>b<sub>i</sub></code>&nbsp;的红色有向边,</li>
15+
<li><code>blueEdges[j] = [u<sub>j</sub>, v<sub>j</sub>]</code>&nbsp;表示图中存在一条从节点&nbsp;<code>u<sub>j</sub></code>&nbsp;到节点&nbsp;<code>v<sub>j</sub></code>&nbsp;的蓝色有向边。</li>
16+
</ul>
1217

1318
<p>返回长度为 <code>n</code> 的数组&nbsp;<code>answer</code>,其中&nbsp;<code>answer[X]</code>&nbsp;是从节点&nbsp;<code>0</code>&nbsp;到节点&nbsp;<code>X</code>&nbsp;的红色边和蓝色边交替出现的最短路径的长度。如果不存在这样的路径,那么 <code>answer[x] = -1</code>。</p>
1419

@@ -28,37 +33,15 @@
2833
<strong>输出:</strong>[0,1,-1]
2934
</pre>
3035

31-
<p><strong>示例 3:</strong></p>
32-
33-
<pre>
34-
<strong>输入:</strong>n = 3, red_edges = [[1,0]], blue_edges = [[2,1]]
35-
<strong>输出:</strong>[0,-1,-1]
36-
</pre>
37-
38-
<p><strong>示例 4:</strong></p>
39-
40-
<pre>
41-
<strong>输入:</strong>n = 3, red_edges = [[0,1]], blue_edges = [[1,2]]
42-
<strong>输出:</strong>[0,1,2]
43-
</pre>
44-
45-
<p><strong>示例 5:</strong></p>
46-
47-
<pre>
48-
<strong>输入:</strong>n = 3, red_edges = [[0,1],[0,2]], blue_edges = [[1,0]]
49-
<strong>输出:</strong>[0,1,1]
50-
</pre>
51-
5236
<p>&nbsp;</p>
5337

5438
<p><strong>提示:</strong></p>
5539

5640
<ul>
5741
<li><code>1 &lt;= n &lt;= 100</code></li>
58-
<li><code>red_edges.length &lt;= 400</code></li>
59-
<li><code>blue_edges.length &lt;= 400</code></li>
60-
<li><code>red_edges[i].length == blue_edges[i].length == 2</code></li>
61-
<li><code>0 &lt;= red_edges[i][j], blue_edges[i][j] &lt; n</code></li>
42+
<li><code>0 &lt;= redEdges.length,&nbsp;blueEdges.length &lt;= 400</code></li>
43+
<li><code>redEdges[i].length == blueEdges[j].length == 2</code></li>
44+
<li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub>, u<sub>j</sub>, v<sub>j</sub>&nbsp;&lt; n</code></li>
6245
</ul>
6346

6447
## 解法
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
# [2566. 替换一个数字后的最大差值](https://leetcode.cn/problems/maximum-difference-by-remapping-a-digit)
2+
3+
[English Version](/solution/2500-2599/2566.Maximum%20Difference%20by%20Remapping%20a%20Digit/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个整数&nbsp;<code>num</code>&nbsp;。你知道 Danny Mittal 会偷偷将 <code>0</code>&nbsp;到 <code>9</code>&nbsp;中的一个数字 <strong>替换</strong> 成另一个数字。</p>
10+
11+
<p>请你返回将 <code>num</code>&nbsp;&nbsp;<strong>恰好一个</strong>&nbsp;数字进行替换后,得到的最大值和最小值的差位多少。</p>
12+
13+
<p><strong>注意:</strong></p>
14+
15+
<ul>
16+
<li>当 Danny 将一个数字 <code>d1</code> 替换成另一个数字 <code>d2</code> 时,Danny 需要将&nbsp;<code>nums</code>&nbsp;中所有 <code>d1</code>&nbsp;都替换成&nbsp;<code>d2</code>&nbsp;。</li>
17+
<li>Danny 可以将一个数字替换成它自己,也就是说&nbsp;<code>num</code>&nbsp;可以不变。</li>
18+
<li>Danny 可以将数字分别替换成两个不同的数字分别得到最大值和最小值。</li>
19+
<li>替换后得到的数字可以包含前导 0 。</li>
20+
<li>Danny Mittal 获得周赛 326 前 10 名,让我们恭喜他。</li>
21+
</ul>
22+
23+
<p>&nbsp;</p>
24+
25+
<p><strong>示例 1:</strong></p>
26+
27+
<pre>
28+
<b>输入:</b>num = 11891
29+
<b>输出:</b>99009
30+
<b>解释:</b>
31+
为了得到最大值,我们将数字 1 替换成数字 9 ,得到 99899 。
32+
为了得到最小值,我们将数字 1 替换成数字 0 ,得到 890 。
33+
两个数字的差值为 99009 。
34+
</pre>
35+
36+
<p><strong>示例 2:</strong></p>
37+
38+
<pre>
39+
<b>输入:</b>num = 90
40+
<b>输出:</b>99
41+
<strong>解释:</strong>
42+
可以得到的最大值是 99(将 0 替换成 9),最小值是 0(将 9 替换成 0)。
43+
所以我们得到 99 。</pre>
44+
45+
<p>&nbsp;</p>
46+
47+
<p><strong>提示:</strong></p>
48+
49+
<ul>
50+
<li><code>1 &lt;= num &lt;= 10<sup>8</sup></code></li>
51+
</ul>
52+
53+
## 解法
54+
55+
<!-- 这里可写通用的实现逻辑 -->
56+
57+
<!-- tabs:start -->
58+
59+
### **Python3**
60+
61+
<!-- 这里可写当前语言的特殊实现逻辑 -->
62+
63+
```python
64+
class Solution:
65+
def minMaxDifference(self, num: int) -> int:
66+
s = str(num)
67+
mi = int(s.replace(s[0], '0'))
68+
for c in s:
69+
if c != '9':
70+
return int(s.replace(c, '9')) - mi
71+
return num - mi
72+
```
73+
74+
### **Java**
75+
76+
<!-- 这里可写当前语言的特殊实现逻辑 -->
77+
78+
```java
79+
class Solution {
80+
public int minMaxDifference(int num) {
81+
String s = String.valueOf(num);
82+
int mi = Integer.parseInt(s.replace(s.charAt(0), '0'));
83+
for (char c : s.toCharArray()) {
84+
if (c != '9') {
85+
return Integer.parseInt(s.replace(c, '9')) - mi;
86+
}
87+
}
88+
return num - mi;
89+
}
90+
}
91+
```
92+
93+
### **C++**
94+
95+
```cpp
96+
class Solution {
97+
public:
98+
int minMaxDifference(int num) {
99+
string s = to_string(num);
100+
string t = s;
101+
char first = s[0];
102+
for (char& c : s) {
103+
if (c == first) {
104+
c = '0';
105+
}
106+
}
107+
int mi = stoi(s);
108+
for (int i = 0; i < t.size(); ++i) {
109+
if (t[i] != '9') {
110+
char second = t[i];
111+
for (int j = i; j < t.size(); ++j) {
112+
if (t[j] == second) {
113+
t[j] = '9';
114+
}
115+
}
116+
return stoi(t) - mi;
117+
}
118+
}
119+
return num - mi;
120+
}
121+
};
122+
```
123+
124+
### **Go**
125+
126+
```go
127+
func minMaxDifference(num int) int {
128+
s := []byte(strconv.Itoa(num))
129+
first := s[0]
130+
for i := range s {
131+
if s[i] == first {
132+
s[i] = '0'
133+
}
134+
}
135+
mi, _ := strconv.Atoi(string(s))
136+
t := []byte(strconv.Itoa(num))
137+
for i := range t {
138+
if t[i] != '9' {
139+
second := t[i]
140+
for j := i; j < len(t); j++ {
141+
if t[j] == second {
142+
t[j] = '9'
143+
}
144+
}
145+
mx, _ := strconv.Atoi(string(t))
146+
return mx - mi
147+
}
148+
}
149+
return num - mi
150+
}
151+
```
152+
153+
### **...**
154+
155+
```
156+
157+
```
158+
159+
<!-- tabs:end -->
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
# [2566. Maximum Difference by Remapping a Digit](https://leetcode.com/problems/maximum-difference-by-remapping-a-digit)
2+
3+
[中文文档](/solution/2500-2599/2566.Maximum%20Difference%20by%20Remapping%20a%20Digit/README.md)
4+
5+
## Description
6+
7+
<p>You are given an integer <code>num</code>. You know that Danny Mittal will sneakily <strong>remap</strong> one of the <code>10</code> possible digits (<code>0</code> to <code>9</code>) to another digit.</p>
8+
9+
<p>Return <em>the difference between the maximum and minimum</em><em>&nbsp;values Danny can make by remapping&nbsp;<strong>exactly</strong> <strong>one</strong> digit</em><em> in </em><code>num</code>.</p>
10+
11+
<p><strong>Notes:</strong></p>
12+
13+
<ul>
14+
<li>When Danny remaps a digit <font face="monospace">d1</font>&nbsp;to another digit <font face="monospace">d2</font>, Danny replaces all occurrences of <code>d1</code>&nbsp;in <code>num</code>&nbsp;with <code>d2</code>.</li>
15+
<li>Danny can remap a digit to itself, in which case <code>num</code>&nbsp;does not change.</li>
16+
<li>Danny can remap different digits for obtaining minimum and maximum values respectively.</li>
17+
<li>The resulting number after remapping can contain leading zeroes.</li>
18+
<li>We mentioned &quot;Danny Mittal&quot; to congratulate him on being in the top 10 in Weekly Contest 326.</li>
19+
</ul>
20+
21+
<p>&nbsp;</p>
22+
<p><strong>Example 1:</strong></p>
23+
24+
<pre>
25+
<strong>Input:</strong> num = 11891
26+
<strong>Output:</strong> 99009
27+
<strong>Explanation:</strong>
28+
To achieve the maximum value, Danny can remap the digit 1 to the digit 9 to yield 99899.
29+
To achieve the minimum value, Danny can remap the digit 1 to the digit 0, yielding 890.
30+
The difference between these two numbers is 99009.
31+
</pre>
32+
33+
<p><strong>Example 2:</strong></p>
34+
35+
<pre>
36+
<strong>Input:</strong> num = 90
37+
<strong>Output:</strong> 99
38+
<strong>Explanation:</strong>
39+
The maximum value that can be returned by the function is 99 (if 0 is replaced by 9) and the minimum value that can be returned by the function is 0 (if 9 is replaced by 0).
40+
Thus, we return 99.</pre>
41+
42+
<p>&nbsp;</p>
43+
<p><strong>Constraints:</strong></p>
44+
45+
<ul>
46+
<li><code>1 &lt;= num &lt;= 10<sup>8</sup></code></li>
47+
</ul>
48+
49+
## Solutions
50+
51+
<!-- tabs:start -->
52+
53+
### **Python3**
54+
55+
```python
56+
class Solution:
57+
def minMaxDifference(self, num: int) -> int:
58+
s = str(num)
59+
mi = int(s.replace(s[0], '0'))
60+
for c in s:
61+
if c != '9':
62+
return int(s.replace(c, '9')) - mi
63+
return num - mi
64+
```
65+
66+
### **Java**
67+
68+
```java
69+
class Solution {
70+
public int minMaxDifference(int num) {
71+
String s = String.valueOf(num);
72+
int mi = Integer.parseInt(s.replace(s.charAt(0), '0'));
73+
for (char c : s.toCharArray()) {
74+
if (c != '9') {
75+
return Integer.parseInt(s.replace(c, '9')) - mi;
76+
}
77+
}
78+
return num - mi;
79+
}
80+
}
81+
```
82+
83+
### **C++**
84+
85+
```cpp
86+
class Solution {
87+
public:
88+
int minMaxDifference(int num) {
89+
string s = to_string(num);
90+
string t = s;
91+
char first = s[0];
92+
for (char& c : s) {
93+
if (c == first) {
94+
c = '0';
95+
}
96+
}
97+
int mi = stoi(s);
98+
for (int i = 0; i < t.size(); ++i) {
99+
if (t[i] != '9') {
100+
char second = t[i];
101+
for (int j = i; j < t.size(); ++j) {
102+
if (t[j] == second) {
103+
t[j] = '9';
104+
}
105+
}
106+
return stoi(t) - mi;
107+
}
108+
}
109+
return num - mi;
110+
}
111+
};
112+
```
113+
114+
### **Go**
115+
116+
```go
117+
func minMaxDifference(num int) int {
118+
s := []byte(strconv.Itoa(num))
119+
first := s[0]
120+
for i := range s {
121+
if s[i] == first {
122+
s[i] = '0'
123+
}
124+
}
125+
mi, _ := strconv.Atoi(string(s))
126+
t := []byte(strconv.Itoa(num))
127+
for i := range t {
128+
if t[i] != '9' {
129+
second := t[i]
130+
for j := i; j < len(t); j++ {
131+
if t[j] == second {
132+
t[j] = '9'
133+
}
134+
}
135+
mx, _ := strconv.Atoi(string(t))
136+
return mx - mi
137+
}
138+
}
139+
return num - mi
140+
}
141+
```
142+
143+
### **...**
144+
145+
```
146+
147+
```
148+
149+
<!-- tabs:end -->
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
int minMaxDifference(int num) {
4+
string s = to_string(num);
5+
string t = s;
6+
char first = s[0];
7+
for (char& c : s) {
8+
if (c == first) {
9+
c = '0';
10+
}
11+
}
12+
int mi = stoi(s);
13+
for (int i = 0; i < t.size(); ++i) {
14+
if (t[i] != '9') {
15+
char second = t[i];
16+
for (int j = i; j < t.size(); ++j) {
17+
if (t[j] == second) {
18+
t[j] = '9';
19+
}
20+
}
21+
return stoi(t) - mi;
22+
}
23+
}
24+
return num - mi;
25+
}
26+
};

0 commit comments

Comments
 (0)