Skip to content

Commit 848381c

Browse files
committed
feat: add new lc problems
1 parent 3aa8afb commit 848381c

File tree

14 files changed

+748
-9
lines changed

14 files changed

+748
-9
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# [2570. 合并两个二维数组 - 求和法](https://leetcode.cn/problems/merge-two-2d-arrays-by-summing-values)
2+
3+
[English Version](/solution/2500-2599/2570.Merge%20Two%202D%20Arrays%20by%20Summing%20Values/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你两个 <strong>二维</strong> 整数数组 <code>nums1</code> 和 <code>nums2.</code></p>
10+
11+
<ul>
12+
<li><code>nums1[i] = [id<sub>i</sub>, val<sub>i</sub>]</code> 表示编号为 <code>id<sub>i</sub></code> 的数字对应的值等于 <code>val<sub>i</sub></code> 。</li>
13+
<li><code>nums2[i] = [id<sub>i</sub>, val<sub>i</sub>]</code>&nbsp;表示编号为 <code>id<sub>i</sub></code> 的数字对应的值等于 <code>val<sub>i</sub></code> 。</li>
14+
</ul>
15+
16+
<p>每个数组都包含 <strong>互不相同</strong> 的 id ,并按 id 以 <strong>递增</strong> 顺序排列。</p>
17+
18+
<p>请你将两个数组合并为一个按 id 以递增顺序排列的数组,并符合下述条件:</p>
19+
20+
<ul>
21+
<li>只有在两个数组中至少出现过一次的 id 才能包含在结果数组内。</li>
22+
<li>每个 id 在结果数组中 <strong>只能出现一次</strong> ,并且其对应的值等于两个数组中该 id 所对应的值求和。如果某个数组中不存在该 id ,则认为其对应的值等于 <code>0</code> 。</li>
23+
</ul>
24+
25+
<p>返回结果数组。返回的数组需要按 id 以递增顺序排列。</p>
26+
27+
<p>&nbsp;</p>
28+
29+
<p><strong>示例 1:</strong></p>
30+
31+
<pre><strong>输入:</strong>nums1 = [[1,2],[2,3],[4,5]], nums2 = [[1,4],[3,2],[4,1]]
32+
<strong>输出:</strong>[[1,6],[2,3],[3,2],[4,6]]
33+
<strong>解释:</strong>结果数组中包含以下元素:
34+
- id = 1 ,对应的值等于 2 + 4 = 6 。
35+
- id = 2 ,对应的值等于 3 。
36+
- id = 3 ,对应的值等于 2 。
37+
- id = 4 ,对应的值等于5 + 1 = 6 。
38+
</pre>
39+
40+
<p><strong>示例 2:</strong></p>
41+
42+
<pre><strong>输入:</strong>nums1 = [[2,4],[3,6],[5,5]], nums2 = [[1,3],[4,3]]
43+
<strong>输出:</strong>[[1,3],[2,4],[3,6],[4,3],[5,5]]
44+
<strong>解释:</strong>不存在共同 id ,在结果数组中只需要包含每个 id 和其对应的值。
45+
</pre>
46+
47+
<p>&nbsp;</p>
48+
49+
<p><strong>提示:</strong></p>
50+
51+
<ul>
52+
<li><code>1 &lt;= nums1.length, nums2.length &lt;= 200</code></li>
53+
<li><code>nums1[i].length == nums2[j].length == 2</code></li>
54+
<li><code>1 &lt;= id<sub>i</sub>, val<sub>i</sub> &lt;= 1000</code></li>
55+
<li>数组中的 id 互不相同</li>
56+
<li>数据均按 id 以严格递增顺序排列</li>
57+
</ul>
58+
59+
## 解法
60+
61+
<!-- 这里可写通用的实现逻辑 -->
62+
63+
<!-- tabs:start -->
64+
65+
### **Python3**
66+
67+
<!-- 这里可写当前语言的特殊实现逻辑 -->
68+
69+
```python
70+
71+
```
72+
73+
### **Java**
74+
75+
<!-- 这里可写当前语言的特殊实现逻辑 -->
76+
77+
```java
78+
79+
```
80+
81+
### **C++**
82+
83+
```cpp
84+
85+
```
86+
87+
### **Go**
88+
89+
```go
90+
91+
```
92+
93+
### **...**
94+
95+
```
96+
97+
```
98+
99+
<!-- tabs:end -->
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# [2570. Merge Two 2D Arrays by Summing Values](https://leetcode.com/problems/merge-two-2d-arrays-by-summing-values)
2+
3+
[中文文档](/solution/2500-2599/2570.Merge%20Two%202D%20Arrays%20by%20Summing%20Values/README.md)
4+
5+
## Description
6+
7+
<p>You are given two <strong>2D</strong> integer arrays <code>nums1</code> and <code>nums2.</code></p>
8+
9+
<ul>
10+
<li><code>nums1[i] = [id<sub>i</sub>, val<sub>i</sub>]</code>&nbsp;indicate that the number with the id <code>id<sub>i</sub></code> has a value equal to <code>val<sub>i</sub></code>.</li>
11+
<li><code>nums2[i] = [id<sub>i</sub>, val<sub>i</sub>]</code>&nbsp;indicate that the number with the id <code>id<sub>i</sub></code> has a value equal to <code>val<sub>i</sub></code>.</li>
12+
</ul>
13+
14+
<p>Each array contains <strong>unique</strong> ids and is sorted in <strong>ascending</strong> order by id.</p>
15+
16+
<p>Merge the two arrays into one array that is sorted in ascending order by id, respecting the following conditions:</p>
17+
18+
<ul>
19+
<li>Only ids that appear in at least one of the two arrays should be included in the resulting array.</li>
20+
<li>Each id should be included <strong>only once</strong> and its value should be the sum of the values of this id in the two arrays. If the id does not exist in one of the two arrays then its value in that array is considered to be <code>0</code>.</li>
21+
</ul>
22+
23+
<p>Return <em>the resulting array</em>. The returned array must be sorted in ascending order by id.</p>
24+
25+
<p>&nbsp;</p>
26+
<p><strong class="example">Example 1:</strong></p>
27+
28+
<pre>
29+
<strong>Input:</strong> nums1 = [[1,2],[2,3],[4,5]], nums2 = [[1,4],[3,2],[4,1]]
30+
<strong>Output:</strong> [[1,6],[2,3],[3,2],[4,6]]
31+
<strong>Explanation:</strong> The resulting array contains the following:
32+
- id = 1, the value of this id is 2 + 4 = 6.
33+
- id = 2, the value of this id is 3.
34+
- id = 3, the value of this id is 2.
35+
- id = 4, the value of this id is 5 + 1 = 6.
36+
</pre>
37+
38+
<p><strong class="example">Example 2:</strong></p>
39+
40+
<pre>
41+
<strong>Input:</strong> nums1 = [[2,4],[3,6],[5,5]], nums2 = [[1,3],[4,3]]
42+
<strong>Output:</strong> [[1,3],[2,4],[3,6],[4,3],[5,5]]
43+
<strong>Explanation:</strong> There are no common ids, so we just include each id with its value in the resulting list.
44+
</pre>
45+
46+
<p>&nbsp;</p>
47+
<p><strong>Constraints:</strong></p>
48+
49+
<ul>
50+
<li><code>1 &lt;= nums1.length, nums2.length &lt;= 200</code></li>
51+
<li><code>nums1[i].length == nums2[j].length == 2</code></li>
52+
<li><code>1 &lt;= id<sub>i</sub>, val<sub>i</sub> &lt;= 1000</code></li>
53+
<li>Both arrays contain unique ids.</li>
54+
<li>Both arrays are in&nbsp;strictly ascending order by id.</li>
55+
</ul>
56+
57+
## Solutions
58+
59+
<!-- tabs:start -->
60+
61+
### **Python3**
62+
63+
```python
64+
65+
```
66+
67+
### **Java**
68+
69+
```java
70+
71+
```
72+
73+
### **C++**
74+
75+
```cpp
76+
77+
```
78+
79+
### **Go**
80+
81+
```go
82+
83+
```
84+
85+
### **...**
86+
87+
```
88+
89+
```
90+
91+
<!-- tabs:end -->
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# [2571. 将整数减少到零需要的最少操作数](https://leetcode.cn/problems/minimum-operations-to-reduce-an-integer-to-0)
2+
3+
[English Version](/solution/2500-2599/2571.Minimum%20Operations%20to%20Reduce%20an%20Integer%20to%200/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个正整数 <code>n</code> ,你可以执行下述操作 <strong>任意</strong> 次:</p>
10+
11+
<ul>
12+
<li><code>n</code> 加上或减去 <code>2</code> 的某个 <strong>幂</strong></li>
13+
</ul>
14+
15+
<p>返回使 <code>n</code> 等于 <code>0</code> 需要执行的 <strong>最少</strong> 操作数。</p>
16+
17+
<p>如果 <code>x == 2<sup>i</sup></code> 且其中 <code>i &gt;= 0</code> ,则数字 <code>x</code> 是 <code>2</code> 的幂。</p>
18+
19+
<p>&nbsp;</p>
20+
21+
<p><strong>示例 1:</strong></p>
22+
23+
<pre>
24+
<strong>输入:</strong>n = 39
25+
<strong>输出:</strong>3
26+
<strong>解释:</strong>我们可以执行下述操作:
27+
- n 加上 2<sup>0</sup> = 1 ,得到 n = 40 。
28+
- n 减去 2<sup>3</sup> = 8 ,得到 n = 32 。
29+
- n 减去 2<sup>5</sup> = 32 ,得到 n = 0 。
30+
可以证明使 n 等于 0 需要执行的最少操作数是 3 。
31+
</pre>
32+
33+
<p><strong>示例 2:</strong></p>
34+
35+
<pre>
36+
<strong>输入:</strong>n = 54
37+
<strong>输出:</strong>3
38+
<strong>解释:</strong>我们可以执行下述操作:
39+
- n 加上 2<sup>1</sup> = 2 ,得到 n = 56 。
40+
- n 加上 2<sup>3</sup> = 8 ,得到 n = 64 。
41+
- n 减去 2<sup>6</sup> = 64 ,得到 n = 0 。
42+
使 n 等于 0 需要执行的最少操作数是 3 。
43+
</pre>
44+
45+
<p>&nbsp;</p>
46+
47+
<p><strong>提示:</strong></p>
48+
49+
<ul>
50+
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
51+
</ul>
52+
53+
## 解法
54+
55+
<!-- 这里可写通用的实现逻辑 -->
56+
57+
<!-- tabs:start -->
58+
59+
### **Python3**
60+
61+
<!-- 这里可写当前语言的特殊实现逻辑 -->
62+
63+
```python
64+
65+
```
66+
67+
### **Java**
68+
69+
<!-- 这里可写当前语言的特殊实现逻辑 -->
70+
71+
```java
72+
73+
```
74+
75+
### **C++**
76+
77+
```cpp
78+
79+
```
80+
81+
### **Go**
82+
83+
```go
84+
85+
```
86+
87+
### **...**
88+
89+
```
90+
91+
```
92+
93+
<!-- tabs:end -->
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# [2571. Minimum Operations to Reduce an Integer to 0](https://leetcode.com/problems/minimum-operations-to-reduce-an-integer-to-0)
2+
3+
[中文文档](/solution/2500-2599/2571.Minimum%20Operations%20to%20Reduce%20an%20Integer%20to%200/README.md)
4+
5+
## Description
6+
7+
<p>You are given a positive integer <code>n</code>, you can do the following operation <strong>any</strong> number of times:</p>
8+
9+
<ul>
10+
<li>Add or subtract a <strong>power</strong> of <code>2</code> from <code>n</code>.</li>
11+
</ul>
12+
13+
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>n</code><em> equal to </em><code>0</code>.</p>
14+
15+
<p>A number <code>x</code> is power of <code>2</code> if <code>x == 2<sup>i</sup></code>&nbsp;where <code>i &gt;= 0</code><em>.</em></p>
16+
17+
<p>&nbsp;</p>
18+
<p><strong class="example">Example 1:</strong></p>
19+
20+
<pre>
21+
<strong>Input:</strong> n = 39
22+
<strong>Output:</strong> 3
23+
<strong>Explanation:</strong> We can do the following operations:
24+
- Add 2<sup>0</sup> = 1 to n, so now n = 40.
25+
- Subtract 2<sup>3</sup> = 8 from n, so now n = 32.
26+
- Subtract 2<sup>5</sup> = 32 from n, so now n = 0.
27+
It can be shown that 3 is the minimum number of operations we need to make n equal to 0.
28+
</pre>
29+
30+
<p><strong class="example">Example 2:</strong></p>
31+
32+
<pre>
33+
<strong>Input:</strong> n = 54
34+
<strong>Output:</strong> 3
35+
<strong>Explanation:</strong> We can do the following operations:
36+
- Add 2<sup>1</sup> = 2 to n, so now n = 56.
37+
- Add 2<sup>3</sup> = 8 to n, so now n = 64.
38+
- Subtract 2<sup>6</sup> = 64 from n, so now n = 0.
39+
So the minimum number of operations is 3.
40+
</pre>
41+
42+
<p>&nbsp;</p>
43+
<p><strong>Constraints:</strong></p>
44+
45+
<ul>
46+
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
47+
</ul>
48+
49+
## Solutions
50+
51+
<!-- tabs:start -->
52+
53+
### **Python3**
54+
55+
```python
56+
57+
```
58+
59+
### **Java**
60+
61+
```java
62+
63+
```
64+
65+
### **C++**
66+
67+
```cpp
68+
69+
```
70+
71+
### **Go**
72+
73+
```go
74+
75+
```
76+
77+
### **...**
78+
79+
```
80+
81+
```
82+
83+
<!-- tabs:end -->

0 commit comments

Comments
 (0)