Skip to content

Commit 34c3429

Browse files
committed
feat: add solutions to lc problems: No.2574~2577
* No.2574.Left and Right Sum Differences * No.2575.Find the Divisibility Array of a String * No.2576.Find the Maximum Number of Marked Indices * No.2577.Minimum Time to Visit a Cell In a Grid
1 parent b2e987f commit 34c3429

File tree

34 files changed

+1673
-4
lines changed

34 files changed

+1673
-4
lines changed

solution/1800-1899/1836.Remove Duplicates From an Unsorted Linked List/README_EN.md

+3
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,11 @@
5757
<p><strong>Constraints:</strong></p>
5858

5959
<ul>
60+
6061
<li>The number of nodes in the list is in the range&nbsp;<code>[1, 10<sup>5</sup>]</code></li>
62+
6163
<li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li>
64+
6265
</ul>
6366

6467
## Solutions

solution/2000-2099/2013.Detect Squares/README_EN.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,12 @@ class DetectSquares {
9898
public DetectSquares() {
9999

100100
}
101-
101+
102102
public void add(int[] point) {
103103
int x = point[0], y = point[1];
104104
cnt.computeIfAbsent(x, k -> new HashMap<>()).merge(y, 1, Integer::sum);
105105
}
106-
106+
107107
public int count(int[] point) {
108108
int x1 = point[0], y1 = point[1];
109109
if (!cnt.containsKey(x1)) {
@@ -140,12 +140,12 @@ public:
140140
DetectSquares() {
141141

142142
}
143-
143+
144144
void add(vector<int> point) {
145145
int x = point[0], y = point[1];
146146
++cnt[x][y];
147147
}
148-
148+
149149
int count(vector<int> point) {
150150
int x1 = point[0], y1 = point[1];
151151
if (!cnt.count(x1)) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
# [2574. 左右元素和的差值](https://leetcode.cn/problems/left-and-right-sum-differences)
2+
3+
[English Version](/solution/2500-2599/2574.Left%20and%20Right%20Sum%20Differences/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> ,请你找出一个下标从 <strong>0</strong> 开始的整数数组 <code>answer</code> ,其中:</p>
10+
11+
<ul>
12+
<li><code>answer.length == nums.length</code></li>
13+
<li><code>answer[i] = |leftSum[i] - rightSum[i]|</code></li>
14+
</ul>
15+
16+
<p>其中:</p>
17+
18+
<ul>
19+
<li><code>leftSum[i]</code> 是数组 <code>nums</code> 中下标 <code>i</code> 左侧元素之和。如果不存在对应的元素,<code>leftSum[i] = 0</code> 。</li>
20+
<li><code>rightSum[i]</code> 是数组 <code>nums</code> 中下标 <code>i</code> 右侧元素之和。如果不存在对应的元素,<code>rightSum[i] = 0</code> 。</li>
21+
</ul>
22+
23+
<p>返回数组 <code>answer</code> 。</p>
24+
25+
<p>&nbsp;</p>
26+
27+
<p><strong>示例 1:</strong></p>
28+
29+
<pre><strong>输入:</strong>nums = [10,4,8,3]
30+
<strong>输出:</strong>[15,1,11,22]
31+
<strong>解释:</strong>数组 leftSum 为 [0,10,14,22] 且数组 rightSum 为 [15,11,3,0] 。
32+
数组 answer 为 [|0 - 15|,|10 - 11|,|14 - 3|,|22 - 0|] = [15,1,11,22] 。
33+
</pre>
34+
35+
<p><strong>示例 2:</strong></p>
36+
37+
<pre><strong>输入:</strong>nums = [1]
38+
<strong>输出:</strong>[0]
39+
<strong>解释:</strong>数组 leftSum 为 [0] 且数组 rightSum 为 [0] 。
40+
数组 answer 为 [|0 - 0|] = [0] 。
41+
</pre>
42+
43+
<p>&nbsp;</p>
44+
45+
<p><strong>提示:</strong></p>
46+
47+
<ul>
48+
<li><code>1 &lt;= nums.length &lt;= 1000</code></li>
49+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
50+
</ul>
51+
52+
## 解法
53+
54+
<!-- 这里可写通用的实现逻辑 -->
55+
56+
**方法一:前缀和**
57+
58+
我们定义变量 $left$ 表示数组 `nums` 中下标 $i$ 左侧元素之和,变量 $right$ 表示数组 `nums` 中下标 $i$ 右侧元素之和。初始时 $left = 0$, $right = \sum_{i = 0}^{n - 1} nums[i]$。
59+
60+
遍历数组 `nums`,对于当前遍历到的数字 $x$,我们更新 $right = right - x$,此时 $left$ 和 $right$ 分别表示数组 `nums` 中下标 $i$ 左侧元素之和和右侧元素之和。我们将 $left$ 和 $right$ 的差的绝对值加入答案数组 `ans` 中,然后更新 $left = left + x$。
61+
62+
遍历完成后,返回答案数组 `ans` 即可。
63+
64+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 `nums` 的长度。
65+
66+
<!-- tabs:start -->
67+
68+
### **Python3**
69+
70+
<!-- 这里可写当前语言的特殊实现逻辑 -->
71+
72+
```python
73+
class Solution:
74+
def leftRigthDifference(self, nums: List[int]) -> List[int]:
75+
left, right = 0, sum(nums)
76+
ans = []
77+
for x in nums:
78+
right -= x
79+
ans.append(abs(left - right))
80+
left += x
81+
return ans
82+
```
83+
84+
### **Java**
85+
86+
<!-- 这里可写当前语言的特殊实现逻辑 -->
87+
88+
```java
89+
class Solution {
90+
public int[] leftRigthDifference(int[] nums) {
91+
int left = 0, right = Arrays.stream(nums).sum();
92+
int n = nums.length;
93+
int[] ans = new int[n];
94+
for (int i = 0; i < n; ++i) {
95+
right -= nums[i];
96+
ans[i] = Math.abs(left - right);
97+
left += nums[i];
98+
}
99+
return ans;
100+
}
101+
}
102+
```
103+
104+
### **C++**
105+
106+
```cpp
107+
class Solution {
108+
public:
109+
vector<int> leftRigthDifference(vector<int>& nums) {
110+
int left = 0, right = accumulate(nums.begin(), nums.end(), 0);
111+
vector<int> ans;
112+
for (int& x : nums) {
113+
right -= x;
114+
ans.push_back(abs(left - right));
115+
left += x;
116+
}
117+
return ans;
118+
}
119+
};
120+
```
121+
122+
### **Go**
123+
124+
```go
125+
func leftRigthDifference(nums []int) (ans []int) {
126+
var left, right int
127+
for _, x := range nums {
128+
right += x
129+
}
130+
for _, x := range nums {
131+
right -= x
132+
ans = append(ans, abs(left-right))
133+
left += x
134+
}
135+
return
136+
}
137+
138+
func abs(x int) int {
139+
if x < 0 {
140+
return -x
141+
}
142+
return x
143+
}
144+
```
145+
146+
### **...**
147+
148+
```
149+
150+
```
151+
152+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# [2574. Left and Right Sum Differences](https://leetcode.com/problems/left-and-right-sum-differences)
2+
3+
[中文文档](/solution/2500-2599/2574.Left%20and%20Right%20Sum%20Differences/README.md)
4+
5+
## Description
6+
7+
<p>Given a <strong>0-indexed</strong> integer array <code>nums</code>, find a <strong>0-indexed </strong>integer array <code>answer</code> where:</p>
8+
9+
<ul>
10+
<li><code>answer.length == nums.length</code>.</li>
11+
<li><code>answer[i] = |leftSum[i] - rightSum[i]|</code>.</li>
12+
</ul>
13+
14+
<p>Where:</p>
15+
16+
<ul>
17+
<li><code>leftSum[i]</code> is the sum of elements to the left of the index <code>i</code> in the array <code>nums</code>. If there is no such element, <code>leftSum[i] = 0</code>.</li>
18+
<li><code>rightSum[i]</code> is the sum of elements to the right of the index <code>i</code> in the array <code>nums</code>. If there is no such element, <code>rightSum[i] = 0</code>.</li>
19+
</ul>
20+
21+
<p>Return <em>the array</em> <code>answer</code>.</p>
22+
23+
<p>&nbsp;</p>
24+
<p><strong class="example">Example 1:</strong></p>
25+
26+
<pre>
27+
<strong>Input:</strong> nums = [10,4,8,3]
28+
<strong>Output:</strong> [15,1,11,22]
29+
<strong>Explanation:</strong> The array leftSum is [0,10,14,22] and the array rightSum is [15,11,3,0].
30+
The array answer is [|0 - 15|,|10 - 11|,|14 - 3|,|22 - 0|] = [15,1,11,22].
31+
</pre>
32+
33+
<p><strong class="example">Example 2:</strong></p>
34+
35+
<pre>
36+
<strong>Input:</strong> nums = [1]
37+
<strong>Output:</strong> [0]
38+
<strong>Explanation:</strong> The array leftSum is [0] and the array rightSum is [0].
39+
The array answer is [|0 - 0|] = [0].
40+
</pre>
41+
42+
<p>&nbsp;</p>
43+
<p><strong>Constraints:</strong></p>
44+
45+
<ul>
46+
<li><code>1 &lt;= nums.length &lt;= 1000</code></li>
47+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
48+
</ul>
49+
50+
## Solutions
51+
52+
<!-- tabs:start -->
53+
54+
### **Python3**
55+
56+
```python
57+
class Solution:
58+
def leftRigthDifference(self, nums: List[int]) -> List[int]:
59+
left, right = 0, sum(nums)
60+
ans = []
61+
for x in nums:
62+
right -= x
63+
ans.append(abs(left - right))
64+
left += x
65+
return ans
66+
```
67+
68+
### **Java**
69+
70+
```java
71+
class Solution {
72+
public int[] leftRigthDifference(int[] nums) {
73+
int left = 0, right = Arrays.stream(nums).sum();
74+
int n = nums.length;
75+
int[] ans = new int[n];
76+
for (int i = 0; i < n; ++i) {
77+
right -= nums[i];
78+
ans[i] = Math.abs(left - right);
79+
left += nums[i];
80+
}
81+
return ans;
82+
}
83+
}
84+
```
85+
86+
### **C++**
87+
88+
```cpp
89+
class Solution {
90+
public:
91+
vector<int> leftRigthDifference(vector<int>& nums) {
92+
int left = 0, right = accumulate(nums.begin(), nums.end(), 0);
93+
vector<int> ans;
94+
for (int& x : nums) {
95+
right -= x;
96+
ans.push_back(abs(left - right));
97+
left += x;
98+
}
99+
return ans;
100+
}
101+
};
102+
```
103+
104+
### **Go**
105+
106+
```go
107+
func leftRigthDifference(nums []int) (ans []int) {
108+
var left, right int
109+
for _, x := range nums {
110+
right += x
111+
}
112+
for _, x := range nums {
113+
right -= x
114+
ans = append(ans, abs(left-right))
115+
left += x
116+
}
117+
return
118+
}
119+
120+
func abs(x int) int {
121+
if x < 0 {
122+
return -x
123+
}
124+
return x
125+
}
126+
```
127+
128+
### **...**
129+
130+
```
131+
132+
```
133+
134+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
vector<int> leftRigthDifference(vector<int>& nums) {
4+
int left = 0, right = accumulate(nums.begin(), nums.end(), 0);
5+
vector<int> ans;
6+
for (int& x : nums) {
7+
right -= x;
8+
ans.push_back(abs(left - right));
9+
left += x;
10+
}
11+
return ans;
12+
}
13+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
func leftRigthDifference(nums []int) (ans []int) {
2+
var left, right int
3+
for _, x := range nums {
4+
right += x
5+
}
6+
for _, x := range nums {
7+
right -= x
8+
ans = append(ans, abs(left-right))
9+
left += x
10+
}
11+
return
12+
}
13+
14+
func abs(x int) int {
15+
if x < 0 {
16+
return -x
17+
}
18+
return x
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public int[] leftRigthDifference(int[] nums) {
3+
int left = 0, right = Arrays.stream(nums).sum();
4+
int n = nums.length;
5+
int[] ans = new int[n];
6+
for (int i = 0; i < n; ++i) {
7+
right -= nums[i];
8+
ans[i] = Math.abs(left - right);
9+
left += nums[i];
10+
}
11+
return ans;
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def leftRigthDifference(self, nums: List[int]) -> List[int]:
3+
left, right = 0, sum(nums)
4+
ans = []
5+
for x in nums:
6+
right -= x
7+
ans.append(abs(left - right))
8+
left += x
9+
return ans

0 commit comments

Comments
 (0)