Skip to content

Commit 1fd56e0

Browse files
authored
feat: add weekly contest 395 (doocs#2676)
1 parent 685d080 commit 1fd56e0

File tree

23 files changed

+1172
-1
lines changed

23 files changed

+1172
-1
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
# [3131. 找出与数组相加的整数 I](https://leetcode.cn/problems/find-the-integer-added-to-array-i)
2+
3+
[English Version](/solution/3100-3199/3131.Find%20the%20Integer%20Added%20to%20Array%20I/README_EN.md)
4+
5+
<!-- tags: -->
6+
7+
## 题目描述
8+
9+
<!-- 这里写题目描述 -->
10+
11+
<p>给你两个长度相等的数组 <code>nums1</code> 和 <code>nums2</code>。</p>
12+
13+
<p>数组 <code>nums1</code> 中的每个元素都与变量 <code>x</code> 所表示的整数相加。如果 <code>x</code> 为负数,则表现为元素值的减少。</p>
14+
15+
<p>在与 <code>x</code> 相加后,<code>nums1</code> 和 <code>nums2</code> <strong>相等</strong> 。当两个数组中包含相同的整数,并且这些整数出现的频次相同时,两个数组 <strong>相等</strong> 。</p>
16+
17+
<p>返回整数 <code>x</code> 。</p>
18+
19+
<p>&nbsp;</p>
20+
21+
<p><strong class="example">示例 1:</strong></p>
22+
23+
<div class="example-block">
24+
<p><strong>输入:</strong><span class="example-io" style="
25+
font-family: Menlo,sans-serif;
26+
font-size: 0.85rem;
27+
">nums1 = [2,6,4], nums2 = [9,7,5]</span></p>
28+
29+
<p><strong>输出:</strong><span class="example-io" style="
30+
font-family: Menlo,sans-serif;
31+
font-size: 0.85rem;
32+
">3</span></p>
33+
34+
<p><strong>解释:</strong></p>
35+
36+
<p>与 3 相加后,<code>nums1</code> 和 <code>nums2</code> 相等。</p>
37+
</div>
38+
39+
<p><strong class="example">示例 2:</strong></p>
40+
41+
<div class="example-block">
42+
<p><strong>输入:</strong><span class="example-io" style="
43+
font-family: Menlo,sans-serif;
44+
font-size: 0.85rem;
45+
">nums1 = [10], nums2 = [5]</span></p>
46+
47+
<p><strong>输出:</strong><span class="example-io" style="
48+
font-family: Menlo,sans-serif;
49+
font-size: 0.85rem;
50+
">-5</span></p>
51+
52+
<p><strong>解释:</strong></p>
53+
54+
<p>与 <code>-5</code> 相加后,<code>nums1</code> 和 <code>nums2</code> 相等。</p>
55+
</div>
56+
57+
<p><strong class="example">示例 3:</strong></p>
58+
59+
<div class="example-block">
60+
<p><strong>输入:</strong><span class="example-io" style="
61+
font-family: Menlo,sans-serif;
62+
font-size: 0.85rem;
63+
">nums1 = [1,1,1,1], nums2 = [1,1,1,1]</span></p>
64+
65+
<p><strong>输出:</strong><span class="example-io" style="
66+
font-family: Menlo,sans-serif;
67+
font-size: 0.85rem;
68+
">0</span></p>
69+
70+
<p><strong>解释:</strong></p>
71+
72+
<p>与 0 相加后,<code>nums1</code> 和 <code>nums2</code> 相等。</p>
73+
</div>
74+
75+
<p>&nbsp;</p>
76+
77+
<p><strong>提示:</strong></p>
78+
79+
<ul>
80+
<li><code>1 &lt;= nums1.length == nums2.length &lt;= 100</code></li>
81+
<li><code>0 &lt;= nums1[i], nums2[i] &lt;= 1000</code></li>
82+
<li>测试用例以这样的方式生成:存在一个整数 <code>x</code>,使得 <code>nums1</code> 中的每个元素都与 <code>x</code> 相加后,<code>nums1</code> 与 <code>nums2</code> 相等。</li>
83+
</ul>
84+
85+
## 解法
86+
87+
### 方法一:求最小值差
88+
89+
我们可以分别求出两个数组的最小值,然后返回两个最小值的差值即可。
90+
91+
时间复杂度 $O(n)$,其中 $n$ 为数组的长度。空间复杂度 $O(1)$。
92+
93+
<!-- tabs:start -->
94+
95+
```python
96+
class Solution:
97+
def addedInteger(self, nums1: List[int], nums2: List[int]) -> int:
98+
return min(nums2) - min(nums1)
99+
```
100+
101+
```java
102+
class Solution {
103+
public int addedInteger(int[] nums1, int[] nums2) {
104+
return Arrays.stream(nums2).min().getAsInt() - Arrays.stream(nums1).min().getAsInt();
105+
}
106+
}
107+
```
108+
109+
```cpp
110+
class Solution {
111+
public:
112+
int addedInteger(vector<int>& nums1, vector<int>& nums2) {
113+
return *min_element(nums2.begin(), nums2.end()) - *min_element(nums1.begin(), nums1.end());
114+
}
115+
};
116+
```
117+
118+
```go
119+
func addedInteger(nums1 []int, nums2 []int) int {
120+
return slices.Min(nums2) - slices.Min(nums1)
121+
}
122+
```
123+
124+
```ts
125+
function addedInteger(nums1: number[], nums2: number[]): number {
126+
return Math.min(...nums2) - Math.min(...nums1);
127+
}
128+
```
129+
130+
<!-- tabs:end -->
131+
132+
<!-- end -->
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
# [3131. Find the Integer Added to Array I](https://leetcode.com/problems/find-the-integer-added-to-array-i)
2+
3+
[中文文档](/solution/3100-3199/3131.Find%20the%20Integer%20Added%20to%20Array%20I/README.md)
4+
5+
<!-- tags: -->
6+
7+
## Description
8+
9+
<p>You are given two arrays of equal length, <code>nums1</code> and <code>nums2</code>.</p>
10+
11+
<p>Each element in <code>nums1</code> has been increased (or decreased in the case of negative) by an integer, represented by the variable <code>x</code>.</p>
12+
13+
<p>As a result, <code>nums1</code> becomes <strong>equal</strong> to <code>nums2</code>. Two arrays are considered <strong>equal</strong> when they contain the same integers with the same frequencies.</p>
14+
15+
<p>Return the integer <code>x</code>.</p>
16+
17+
<p>&nbsp;</p>
18+
<p><strong class="example">Example 1:</strong></p>
19+
20+
<div class="example-block">
21+
<p><strong>Input:</strong> <span class="example-io" style="
22+
font-family: Menlo,sans-serif;
23+
font-size: 0.85rem;
24+
">nums1 = [2,6,4], nums2 = [9,7,5]</span></p>
25+
26+
<p><strong>Output:</strong> <span class="example-io" style="
27+
font-family: Menlo,sans-serif;
28+
font-size: 0.85rem;
29+
">3</span></p>
30+
31+
<p><strong>Explanation:</strong></p>
32+
33+
<p>The integer added to each element of <code>nums1</code> is 3.</p>
34+
</div>
35+
36+
<p><strong class="example">Example 2:</strong></p>
37+
38+
<div class="example-block">
39+
<p><strong>Input:</strong> <span class="example-io" style="
40+
font-family: Menlo,sans-serif;
41+
font-size: 0.85rem;
42+
">nums1 = [10], nums2 = [5]</span></p>
43+
44+
<p><strong>Output:</strong> <span class="example-io" style="
45+
font-family: Menlo,sans-serif;
46+
font-size: 0.85rem;
47+
">-5</span></p>
48+
49+
<p><strong>Explanation:</strong></p>
50+
51+
<p>The integer added to each element of <code>nums1</code> is -5.</p>
52+
</div>
53+
54+
<p><strong class="example">Example 3:</strong></p>
55+
56+
<div class="example-block">
57+
<p><strong>Input:</strong> <span class="example-io" style="
58+
font-family: Menlo,sans-serif;
59+
font-size: 0.85rem;
60+
">nums1 = [1,1,1,1], nums2 = [1,1,1,1]</span></p>
61+
62+
<p><strong>Output:</strong> <span class="example-io" style="
63+
font-family: Menlo,sans-serif;
64+
font-size: 0.85rem;
65+
">0</span></p>
66+
67+
<p><strong>Explanation:</strong></p>
68+
69+
<p>The integer added to each element of <code>nums1</code> is 0.</p>
70+
</div>
71+
72+
<p>&nbsp;</p>
73+
<p><strong>Constraints:</strong></p>
74+
75+
<ul>
76+
<li><code>1 &lt;= nums1.length == nums2.length &lt;= 100</code></li>
77+
<li><code>0 &lt;= nums1[i], nums2[i] &lt;= 1000</code></li>
78+
<li>The test cases are generated in a way that there is an integer <code>x</code> such that <code>nums1</code> can become equal to <code>nums2</code> by adding <code>x</code> to each element of <code>nums1</code>.</li>
79+
</ul>
80+
81+
## Solutions
82+
83+
### Solution 1: Calculate Minimum Difference
84+
85+
We can find the minimum value of each array, then return the difference between the two minimum values.
86+
87+
The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.
88+
89+
<!-- tabs:start -->
90+
91+
```python
92+
class Solution:
93+
def addedInteger(self, nums1: List[int], nums2: List[int]) -> int:
94+
return min(nums2) - min(nums1)
95+
```
96+
97+
```java
98+
class Solution {
99+
public int addedInteger(int[] nums1, int[] nums2) {
100+
return Arrays.stream(nums2).min().getAsInt() - Arrays.stream(nums1).min().getAsInt();
101+
}
102+
}
103+
```
104+
105+
```cpp
106+
class Solution {
107+
public:
108+
int addedInteger(vector<int>& nums1, vector<int>& nums2) {
109+
return *min_element(nums2.begin(), nums2.end()) - *min_element(nums1.begin(), nums1.end());
110+
}
111+
};
112+
```
113+
114+
```go
115+
func addedInteger(nums1 []int, nums2 []int) int {
116+
return slices.Min(nums2) - slices.Min(nums1)
117+
}
118+
```
119+
120+
```ts
121+
function addedInteger(nums1: number[], nums2: number[]): number {
122+
return Math.min(...nums2) - Math.min(...nums1);
123+
}
124+
```
125+
126+
<!-- tabs:end -->
127+
128+
<!-- end -->
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution {
2+
public:
3+
int addedInteger(vector<int>& nums1, vector<int>& nums2) {
4+
return *min_element(nums2.begin(), nums2.end()) - *min_element(nums1.begin(), nums1.end());
5+
}
6+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
func addedInteger(nums1 []int, nums2 []int) int {
2+
return slices.Min(nums2) - slices.Min(nums1)
3+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution {
2+
public int addedInteger(int[] nums1, int[] nums2) {
3+
return Arrays.stream(nums2).min().getAsInt() - Arrays.stream(nums1).min().getAsInt();
4+
}
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def addedInteger(self, nums1: List[int], nums2: List[int]) -> int:
3+
return min(nums2) - min(nums1)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function addedInteger(nums1: number[], nums2: number[]): number {
2+
return Math.min(...nums2) - Math.min(...nums1);
3+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy