Skip to content

Commit ccd8b51

Browse files
committed
feat: add new lc problems
1 parent 1b53d4d commit ccd8b51

File tree

32 files changed

+2486
-1
lines changed

32 files changed

+2486
-1
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
---
2+
comments: true
3+
difficulty: 中等
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3618.Split%20Array%20by%20Prime%20Indices/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3618. 根据质数下标分割数组](https://leetcode.cn/problems/split-array-by-prime-indices)
10+
11+
[English Version](/solution/3600-3699/3618.Split%20Array%20by%20Prime%20Indices/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p>给你一个整数数组 <code>nums</code>。</p>
18+
19+
<p>根据以下规则将 <code>nums</code> 分割成两个数组 <code>A</code> 和 <code>B</code>:</p>
20+
21+
<ul>
22+
<li><code>nums</code> 中位于&nbsp;<strong>质数 </strong>下标的元素必须放入数组 <code>A</code>。</li>
23+
<li>所有其他元素必须放入数组 <code>B</code>。</li>
24+
</ul>
25+
26+
<p>返回两个数组和的&nbsp;<strong>绝对&nbsp;</strong>差值:<code>|sum(A) - sum(B)|</code>。</p>
27+
28+
<p><strong>质数&nbsp;</strong>是一个大于 1 的自然数,它只有两个因子,1和它本身。</p>
29+
30+
<p><strong>注意</strong>:空数组的和为 0。</p>
31+
32+
<p>&nbsp;</p>
33+
34+
<p><strong class="example">示例 1:</strong></p>
35+
36+
<div class="example-block">
37+
<p><strong>输入:</strong> <span class="example-io">nums = [2,3,4]</span></p>
38+
39+
<p><strong>输出:</strong> <span class="example-io">1</span></p>
40+
41+
<p><strong>解释:</strong></p>
42+
43+
<ul>
44+
<li>数组中唯一的质数下标是 2,所以 <code>nums[2] = 4</code> 被放入数组 <code>A</code>。</li>
45+
<li>其余元素 <code>nums[0] = 2</code> 和 <code>nums[1] = 3</code> 被放入数组 <code>B</code>。</li>
46+
<li><code>sum(A) = 4</code>,<code>sum(B) = 2 + 3 = 5</code>。</li>
47+
<li>绝对差值是 <code>|4 - 5| = 1</code>。</li>
48+
</ul>
49+
</div>
50+
51+
<p><strong class="example">示例 2:</strong></p>
52+
53+
<div class="example-block">
54+
<p><strong>输入:</strong> <span class="example-io">nums = [-1,5,7,0]</span></p>
55+
56+
<p><strong>输出:</strong> <span class="example-io">3</span></p>
57+
58+
<p><strong>解释:</strong></p>
59+
60+
<ul>
61+
<li>数组中的质数下标是 2 和 3,所以 <code>nums[2] = 7</code> 和 <code>nums[3] = 0</code> 被放入数组 <code>A</code>。</li>
62+
<li>其余元素 <code>nums[0] = -1</code> 和 <code>nums[1] = 5</code> 被放入数组 <code>B</code>。</li>
63+
<li><code>sum(A) = 7 + 0 = 7</code>,<code>sum(B) = -1 + 5 = 4</code>。</li>
64+
<li>绝对差值是 <code>|7 - 4| = 3</code>。</li>
65+
</ul>
66+
</div>
67+
68+
<p>&nbsp;</p>
69+
70+
<p><strong>提示:</strong></p>
71+
72+
<ul>
73+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
74+
<li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
75+
</ul>
76+
77+
<!-- description:end -->
78+
79+
## 解法
80+
81+
<!-- solution:start -->
82+
83+
### 方法一
84+
85+
<!-- tabs:start -->
86+
87+
#### Python3
88+
89+
```python
90+
91+
```
92+
93+
#### Java
94+
95+
```java
96+
97+
```
98+
99+
#### C++
100+
101+
```cpp
102+
103+
```
104+
105+
#### Go
106+
107+
```go
108+
109+
```
110+
111+
<!-- tabs:end -->
112+
113+
<!-- solution:end -->
114+
115+
<!-- problem:end -->
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
---
2+
comments: true
3+
difficulty: Medium
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3618.Split%20Array%20by%20Prime%20Indices/README_EN.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3618. Split Array by Prime Indices](https://leetcode.com/problems/split-array-by-prime-indices)
10+
11+
[中文文档](/solution/3600-3699/3618.Split%20Array%20by%20Prime%20Indices/README.md)
12+
13+
## Description
14+
15+
<!-- description:start -->
16+
17+
<p>You are given an integer array <code>nums</code>.</p>
18+
19+
<p>Split <code>nums</code> into two arrays <code>A</code> and <code>B</code> using the following rule:</p>
20+
21+
<ul>
22+
<li>Elements at <strong>prime</strong> indices in <code>nums</code> must go into array <code>A</code>.</li>
23+
<li>All other elements must go into array <code>B</code>.</li>
24+
</ul>
25+
26+
<p>Return the <strong>absolute</strong> difference between the sums of the two arrays: <code>|sum(A) - sum(B)|</code>.</p>
27+
28+
<p>A <strong>prime</strong> number is a natural number greater than 1 with only two factors, 1 and itself.</p>
29+
30+
<p><strong>Note:</strong> An empty array has a sum of 0.</p>
31+
32+
<p>&nbsp;</p>
33+
<p><strong class="example">Example 1:</strong></p>
34+
35+
<div class="example-block">
36+
<p><strong>Input:</strong> <span class="example-io">nums = [2,3,4]</span></p>
37+
38+
<p><strong>Output:</strong> <span class="example-io">1</span></p>
39+
40+
<p><strong>Explanation:</strong></p>
41+
42+
<ul>
43+
<li>The only prime index in the array is 2, so <code>nums[2] = 4</code> is placed in array <code>A</code>.</li>
44+
<li>The remaining elements, <code>nums[0] = 2</code> and <code>nums[1] = 3</code> are placed in array <code>B</code>.</li>
45+
<li><code>sum(A) = 4</code>, <code>sum(B) = 2 + 3 = 5</code>.</li>
46+
<li>The absolute difference is <code>|4 - 5| = 1</code>.</li>
47+
</ul>
48+
</div>
49+
50+
<p><strong class="example">Example 2:</strong></p>
51+
52+
<div class="example-block">
53+
<p><strong>Input:</strong> <span class="example-io">nums = [-1,5,7,0]</span></p>
54+
55+
<p><strong>Output:</strong> <span class="example-io">3</span></p>
56+
57+
<p><strong>Explanation:</strong></p>
58+
59+
<ul>
60+
<li>The prime indices in the array are 2 and 3, so <code>nums[2] = 7</code> and <code>nums[3] = 0</code> are placed in array <code>A</code>.</li>
61+
<li>The remaining elements, <code>nums[0] = -1</code> and <code>nums[1] = 5</code> are placed in array <code>B</code>.</li>
62+
<li><code>sum(A) = 7 + 0 = 7</code>, <code>sum(B) = -1 + 5 = 4</code>.</li>
63+
<li>The absolute difference is <code>|7 - 4| = 3</code>.</li>
64+
</ul>
65+
</div>
66+
67+
<p>&nbsp;</p>
68+
<p><strong>Constraints:</strong></p>
69+
70+
<ul>
71+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
72+
<li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
73+
</ul>
74+
75+
<!-- description:end -->
76+
77+
## Solutions
78+
79+
<!-- solution:start -->
80+
81+
### Solution 1
82+
83+
<!-- tabs:start -->
84+
85+
#### Python3
86+
87+
```python
88+
89+
```
90+
91+
#### Java
92+
93+
```java
94+
95+
```
96+
97+
#### C++
98+
99+
```cpp
100+
101+
```
102+
103+
#### Go
104+
105+
```go
106+
107+
```
108+
109+
<!-- tabs:end -->
110+
111+
<!-- solution:end -->
112+
113+
<!-- problem:end -->
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
---
2+
comments: true
3+
difficulty: 中等
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3619.Count%20Islands%20With%20Total%20Value%20Divisible%20by%20K/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3619. 总价值可以被 K 整除的岛屿数目](https://leetcode.cn/problems/count-islands-with-total-value-divisible-by-k)
10+
11+
[English Version](/solution/3600-3699/3619.Count%20Islands%20With%20Total%20Value%20Divisible%20by%20K/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p>给你一个 <code>m x n</code> 的矩阵 <code>grid</code> 和一个正整数 <code>k</code>。一个&nbsp;<strong>岛屿&nbsp;</strong>是由&nbsp;<strong>正&nbsp;</strong>整数(表示陆地)组成的,并且陆地间&nbsp;<strong>四周&nbsp;</strong>连通(水平或垂直)。</p>
18+
19+
<p>一个岛屿的总价值是该岛屿中所有单元格的值之和。</p>
20+
21+
<p>返回总价值可以被 <code>k</code> <strong>整除&nbsp;</strong>的岛屿数量。</p>
22+
23+
<p>&nbsp;</p>
24+
25+
<p><strong class="example">示例 1:</strong></p>
26+
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3600-3699/3619.Count%20Islands%20With%20Total%20Value%20Divisible%20by%20K/images/example1griddrawio-1.png" style="width: 200px; height: 200px;" />
27+
<div class="example-block">
28+
<p><strong>输入:</strong> <span class="example-io">grid = [[0,2,1,0,0],[0,5,0,0,5],[0,0,1,0,0],[0,1,4,7,0],[0,2,0,0,8]], k = 5</span></p>
29+
30+
<p><strong>输出:</strong> <span class="example-io">2</span></p>
31+
32+
<p><strong>解释:</strong></p>
33+
34+
<p>网格中包含四个岛屿。蓝色高亮显示的岛屿的总价值可以被 5 整除,而红色高亮显示的岛屿则不能。</p>
35+
</div>
36+
37+
<p><strong class="example">示例 2:</strong></p>
38+
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3600-3699/3619.Count%20Islands%20With%20Total%20Value%20Divisible%20by%20K/images/example2griddrawio.png" style="width: 200px; height: 150px;" />
39+
<div class="example-block">
40+
<p><strong>输入:</strong> <span class="example-io">grid = [[3,0,3,0], [0,3,0,3], [3,0,3,0]], k = 3</span></p>
41+
42+
<p><strong>输出:</strong> <span class="example-io">6</span></p>
43+
44+
<p><strong>解释:</strong></p>
45+
46+
<p>网格中包含六个岛屿,每个岛屿的总价值都可以被 3 整除。</p>
47+
</div>
48+
49+
<p>&nbsp;</p>
50+
51+
<p><strong>提示:</strong></p>
52+
53+
<ul>
54+
<li><code>m == grid.length</code></li>
55+
<li><code>n == grid[i].length</code></li>
56+
<li><code>1 &lt;= m, n &lt;= 1000</code></li>
57+
<li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li>
58+
<li><code>0 &lt;= grid[i][j] &lt;= 10<sup>6</sup></code></li>
59+
<li><code>1 &lt;= k &lt; = 10<sup>6</sup></code></li>
60+
</ul>
61+
62+
<!-- description:end -->
63+
64+
## 解法
65+
66+
<!-- solution:start -->
67+
68+
### 方法一
69+
70+
<!-- tabs:start -->
71+
72+
#### Python3
73+
74+
```python
75+
76+
```
77+
78+
#### Java
79+
80+
```java
81+
82+
```
83+
84+
#### C++
85+
86+
```cpp
87+
88+
```
89+
90+
#### Go
91+
92+
```go
93+
94+
```
95+
96+
<!-- tabs:end -->
97+
98+
<!-- solution:end -->
99+
100+
<!-- problem:end -->

0 commit comments

Comments
 (0)