Skip to content

Commit c9e7bdb

Browse files
committed
feat: add solutions to lc problems: No.2605~2608
* No.2605.Form Smallest Number From Two Digit Arrays * No.2606.Find the Substring With Maximum Cost * No.2607.Make K-Subarray Sums Equal * No.2608.Shortest Cycle in a Graph
1 parent 37fb2dc commit c9e7bdb

File tree

35 files changed

+1968
-3
lines changed

35 files changed

+1968
-3
lines changed

solution/0700-0799/0789.Escape The Ghosts/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
<p>如果你可以在任何阻碍者抓住你 <strong>之前</strong> 到达目的地(阻碍者可以采取任意行动方式),则被视为逃脱成功。如果你和阻碍者 <strong>同时</strong> 到达了一个位置(包括目的地)&nbsp;<strong>都不算</strong>&nbsp;是逃脱成功。</p>
1414

15-
<p>只有在你有可能成功逃脱时,输出 <code>true</code> ;否则,输出 <code>false</code> 。</p>
15+
<p>如果不管阻碍者怎么移动都可以成功逃脱时,输出 <code>true</code> ;否则,输出 <code>false</code> 。</p>
1616
&nbsp;
1717

1818
<p><strong>示例 1:</strong></p>

solution/1300-1399/1312.Minimum Insertion Steps to Make a String Palindrome/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class Solution:
5959
if s[i] == s[j]:
6060
return dfs(i + 1, j - 1)
6161
return 1 + min(dfs(i + 1, j), dfs(i, j - 1))
62-
62+
6363
return dfs(0, len(s) - 1)
6464
```
6565

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# [2605. 从两个数字数组里生成最小数字](https://leetcode.cn/problems/form-smallest-number-from-two-digit-arrays)
2+
3+
[English Version](/solution/2600-2699/2605.Form%20Smallest%20Number%20From%20Two%20Digit%20Arrays/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
给你两个只包含 1 到 9 之间数字的数组&nbsp;<code>nums1</code> 和&nbsp;<code>nums2</code>&nbsp;,每个数组中的元素 <strong>互不相同</strong>&nbsp;,请你返回 <strong>最小</strong> 的数字,两个数组都 <strong>至少</strong> 包含这个数字的某个数位。
10+
11+
<p>&nbsp;</p>
12+
13+
<p><strong>示例 1:</strong></p>
14+
15+
<pre><b>输入:</b>nums1 = [4,1,3], nums2 = [5,7]
16+
<b>输出:</b>15
17+
<b>解释:</b>数字 15 的数位 1 在 nums1 中出现,数位 5 在 nums2 中出现。15 是我们能得到的最小数字。
18+
</pre>
19+
20+
<p><strong>示例 2:</strong></p>
21+
22+
<pre><b>输入:</b>nums1 = [3,5,2,6], nums2 = [3,1,7]
23+
<b>输出:</b>3
24+
<b>解释:</b>数字 3 的数位 3 在两个数组中都出现了。
25+
</pre>
26+
27+
<p>&nbsp;</p>
28+
29+
<p><strong>提示:</strong></p>
30+
31+
<ul>
32+
<li><code>1 &lt;= nums1.length, nums2.length &lt;= 9</code></li>
33+
<li><code>1 &lt;= nums1[i], nums2[i] &lt;= 9</code></li>
34+
<li>每个数组中,元素 <strong>互不相同</strong>&nbsp;。</li>
35+
</ul>
36+
37+
## 解法
38+
39+
<!-- 这里可写通用的实现逻辑 -->
40+
41+
<!-- tabs:start -->
42+
43+
### **Python3**
44+
45+
<!-- 这里可写当前语言的特殊实现逻辑 -->
46+
47+
```python
48+
class Solution:
49+
def minNumber(self, nums1: List[int], nums2: List[int]) -> int:
50+
ans = 100
51+
for a in nums1:
52+
for b in nums2:
53+
if a == b:
54+
ans = min(ans, a)
55+
else:
56+
ans = min(ans, 10 * a + b, 10 * b + a)
57+
return ans
58+
```
59+
60+
### **Java**
61+
62+
<!-- 这里可写当前语言的特殊实现逻辑 -->
63+
64+
```java
65+
class Solution {
66+
public int minNumber(int[] nums1, int[] nums2) {
67+
int ans = 100;
68+
for (int a : nums1) {
69+
for (int b : nums2) {
70+
if (a == b) {
71+
ans = Math.min(ans, a);
72+
} else {
73+
ans = Math.min(ans, Math.min(a * 10 + b, b * 10 + a));
74+
}
75+
}
76+
}
77+
return ans;
78+
}
79+
}
80+
```
81+
82+
### **C++**
83+
84+
```cpp
85+
class Solution {
86+
public:
87+
int minNumber(vector<int>& nums1, vector<int>& nums2) {
88+
int ans = 100;
89+
for (int a : nums1) {
90+
for (int b : nums2) {
91+
if (a == b) {
92+
ans = min(ans, a);
93+
} else {
94+
ans = min({ans, a * 10 + b, b * 10 + a});
95+
}
96+
}
97+
}
98+
return ans;
99+
}
100+
};
101+
```
102+
103+
### **Go**
104+
105+
```go
106+
func minNumber(nums1 []int, nums2 []int) int {
107+
ans := 100
108+
for _, a := range nums1 {
109+
for _, b := range nums2 {
110+
if a == b {
111+
ans = min(ans, a)
112+
} else {
113+
ans = min(ans, min(a*10+b, b*10+a))
114+
}
115+
}
116+
}
117+
return ans
118+
}
119+
120+
func min(a, b int) int {
121+
if a < b {
122+
return a
123+
}
124+
return b
125+
}
126+
```
127+
128+
### **...**
129+
130+
```
131+
132+
```
133+
134+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# [2605. Form Smallest Number From Two Digit Arrays](https://leetcode.com/problems/form-smallest-number-from-two-digit-arrays)
2+
3+
[中文文档](/solution/2600-2699/2605.Form%20Smallest%20Number%20From%20Two%20Digit%20Arrays/README.md)
4+
5+
## Description
6+
7+
Given two arrays of <strong>unique</strong> digits <code>nums1</code> and <code>nums2</code>, return <em>the <strong>smallest</strong> number that contains <strong>at least</strong> one digit from each array</em>.
8+
9+
<p>&nbsp;</p>
10+
<p><strong class="example">Example 1:</strong></p>
11+
12+
<pre>
13+
<strong>Input:</strong> nums1 = [4,1,3], nums2 = [5,7]
14+
<strong>Output:</strong> 15
15+
<strong>Explanation:</strong> The number 15 contains the digit 1 from nums1 and the digit 5 from nums2. It can be proven that 15 is the smallest number we can have.
16+
</pre>
17+
18+
<p><strong class="example">Example 2:</strong></p>
19+
20+
<pre>
21+
<strong>Input:</strong> nums1 = [3,5,2,6], nums2 = [3,1,7]
22+
<strong>Output:</strong> 3
23+
<strong>Explanation:</strong> The number 3 contains the digit 3 which exists in both arrays.
24+
</pre>
25+
26+
<p>&nbsp;</p>
27+
<p><strong>Constraints:</strong></p>
28+
29+
<ul>
30+
<li><code>1 &lt;= nums1.length, nums2.length &lt;= 9</code></li>
31+
<li><code>1 &lt;= nums1[i], nums2[i] &lt;= 9</code></li>
32+
<li>All digits in each array are <strong>unique</strong>.</li>
33+
</ul>
34+
35+
## Solutions
36+
37+
<!-- tabs:start -->
38+
39+
### **Python3**
40+
41+
```python
42+
class Solution:
43+
def minNumber(self, nums1: List[int], nums2: List[int]) -> int:
44+
ans = 100
45+
for a in nums1:
46+
for b in nums2:
47+
if a == b:
48+
ans = min(ans, a)
49+
else:
50+
ans = min(ans, 10 * a + b, 10 * b + a)
51+
return ans
52+
```
53+
54+
### **Java**
55+
56+
```java
57+
class Solution {
58+
public int minNumber(int[] nums1, int[] nums2) {
59+
int ans = 100;
60+
for (int a : nums1) {
61+
for (int b : nums2) {
62+
if (a == b) {
63+
ans = Math.min(ans, a);
64+
} else {
65+
ans = Math.min(ans, Math.min(a * 10 + b, b * 10 + a));
66+
}
67+
}
68+
}
69+
return ans;
70+
}
71+
}
72+
```
73+
74+
### **C++**
75+
76+
```cpp
77+
class Solution {
78+
public:
79+
int minNumber(vector<int>& nums1, vector<int>& nums2) {
80+
int ans = 100;
81+
for (int a : nums1) {
82+
for (int b : nums2) {
83+
if (a == b) {
84+
ans = min(ans, a);
85+
} else {
86+
ans = min({ans, a * 10 + b, b * 10 + a});
87+
}
88+
}
89+
}
90+
return ans;
91+
}
92+
};
93+
```
94+
95+
### **Go**
96+
97+
```go
98+
func minNumber(nums1 []int, nums2 []int) int {
99+
ans := 100
100+
for _, a := range nums1 {
101+
for _, b := range nums2 {
102+
if a == b {
103+
ans = min(ans, a)
104+
} else {
105+
ans = min(ans, min(a*10+b, b*10+a))
106+
}
107+
}
108+
}
109+
return ans
110+
}
111+
112+
func min(a, b int) int {
113+
if a < b {
114+
return a
115+
}
116+
return b
117+
}
118+
```
119+
120+
### **...**
121+
122+
```
123+
124+
```
125+
126+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
int minNumber(vector<int>& nums1, vector<int>& nums2) {
4+
int ans = 100;
5+
for (int a : nums1) {
6+
for (int b : nums2) {
7+
if (a == b) {
8+
ans = min(ans, a);
9+
} else {
10+
ans = min({ans, a * 10 + b, b * 10 + a});
11+
}
12+
}
13+
}
14+
return ans;
15+
}
16+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
func minNumber(nums1 []int, nums2 []int) int {
2+
ans := 100
3+
for _, a := range nums1 {
4+
for _, b := range nums2 {
5+
if a == b {
6+
ans = min(ans, a)
7+
} else {
8+
ans = min(ans, min(a*10+b, b*10+a))
9+
}
10+
}
11+
}
12+
return ans
13+
}
14+
15+
func min(a, b int) int {
16+
if a < b {
17+
return a
18+
}
19+
return b
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int minNumber(int[] nums1, int[] nums2) {
3+
int ans = 100;
4+
for (int a : nums1) {
5+
for (int b : nums2) {
6+
if (a == b) {
7+
ans = Math.min(ans, a);
8+
} else {
9+
ans = Math.min(ans, Math.min(a * 10 + b, b * 10 + a));
10+
}
11+
}
12+
}
13+
return ans;
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def minNumber(self, nums1: List[int], nums2: List[int]) -> int:
3+
ans = 100
4+
for a in nums1:
5+
for b in nums2:
6+
if a == b:
7+
ans = min(ans, a)
8+
else:
9+
ans = min(ans, 10 * a + b, 10 * b + a)
10+
return ans

0 commit comments

Comments
 (0)