Skip to content

Commit fd9dbb2

Browse files
committed
feat: add solutions to lc problem: No.1714
No.1714.Sum Of Special Evenly-Spaced Elements In Array
1 parent 651e031 commit fd9dbb2

File tree

8 files changed

+442
-4
lines changed

8 files changed

+442
-4
lines changed

solution/1700-1799/1714.Sum Of Special Evenly-Spaced Elements In Array/README.md

+159-1
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,180 @@
4747

4848
<!-- 这里可写通用的实现逻辑 -->
4949

50+
**方法一:分块**
51+
52+
这道题是一道比较典型的分块题目,对于步长较大的查询,我们可以直接暴力求解;对于步长较小的查询,我们可以预处理出每个位置的后缀和,然后直接查询。
53+
54+
本题中,我们将步长较大的查询的步长限制为 $\sqrt{n}$,这样就可以保证每个查询的时间复杂度为 $O(\sqrt{n})$。
55+
56+
我们定义一个二维数组 $suf$,其中 $suf[i][j]$ be 表示从位置 $j$ 开始,步长为 $i$ 的后缀和。那么对于每个查询 $[x, y]$,我们可以分为两种情况:
57+
58+
- 如果 $y \le \sqrt{n}$,那么我们可以直接查询 $suf[y][x]$;
59+
- 如果 $y \gt \sqrt{n}$,那么我们可以直接暴力求解。
60+
61+
时间复杂度 $O((n + m) \times \sqrt{n})$,空间复杂度 $O(n \times \sqrt{n})$。其中 $n$ 是数组的长度,而 $m$ 是查询的个数。
62+
5063
<!-- tabs:start -->
5164

5265
### **Python3**
5366

5467
<!-- 这里可写当前语言的特殊实现逻辑 -->
5568

5669
```python
57-
70+
class Solution:
71+
def solve(self, nums: List[int], queries: List[List[int]]) -> List[int]:
72+
mod = 10**9 + 7
73+
n = len(nums)
74+
m = int(sqrt(n))
75+
suf = [[0] * (n + 1) for _ in range(m + 1)]
76+
for i in range(1, m + 1):
77+
for j in range(n - 1, -1, -1):
78+
suf[i][j] = suf[i][min(n, j + i)] + nums[j]
79+
ans = []
80+
for x, y in queries:
81+
if y <= m:
82+
ans.append(suf[y][x] % mod)
83+
else:
84+
ans.append(sum(nums[x::y]) % mod)
85+
return ans
5886
```
5987

6088
### **Java**
6189

6290
<!-- 这里可写当前语言的特殊实现逻辑 -->
6391

6492
```java
93+
class Solution {
94+
public int[] solve(int[] nums, int[][] queries) {
95+
int n = nums.length;
96+
int m = (int) Math.sqrt(n);
97+
final int mod = (int) 1e9 + 7;
98+
int[][] suf = new int[m + 1][n + 1];
99+
for (int i = 1; i <= m; ++i) {
100+
for (int j = n - 1; j >= 0; --j) {
101+
suf[i][j] = (suf[i][Math.min(n, j + i)] + nums[j]) % mod;
102+
}
103+
}
104+
int k = queries.length;
105+
int[] ans = new int[k];
106+
for (int i = 0; i < k; ++i) {
107+
int x = queries[i][0];
108+
int y = queries[i][1];
109+
if (y <= m) {
110+
ans[i] = suf[y][x];
111+
} else {
112+
int s = 0;
113+
for (int j = x; j < n; j += y) {
114+
s = (s + nums[j]) % mod;
115+
}
116+
ans[i] = s;
117+
}
118+
}
119+
return ans;
120+
}
121+
}
122+
```
123+
124+
### **C++**
125+
126+
```cpp
127+
class Solution {
128+
public:
129+
vector<int> solve(vector<int>& nums, vector<vector<int>>& queries) {
130+
int n = nums.size();
131+
int m = (int) sqrt(n);
132+
const int mod = 1e9 + 7;
133+
int suf[m + 1][n + 1];
134+
memset(suf, 0, sizeof(suf));
135+
for (int i = 1; i <= m; ++i) {
136+
for (int j = n - 1; ~j; --j) {
137+
suf[i][j] = (suf[i][min(n, j + i)] + nums[j]) % mod;
138+
}
139+
}
140+
vector<int> ans;
141+
for (auto& q : queries) {
142+
int x = q[0], y = q[1];
143+
if (y <= m) {
144+
ans.push_back(suf[y][x]);
145+
} else {
146+
int s = 0;
147+
for (int i = x; i < n; i += y) {
148+
s = (s + nums[i]) % mod;
149+
}
150+
ans.push_back(s);
151+
}
152+
}
153+
return ans;
154+
}
155+
};
156+
```
157+
158+
### **Go**
159+
160+
```go
161+
func solve(nums []int, queries [][]int) (ans []int) {
162+
n := len(nums)
163+
m := int(math.Sqrt(float64(n)))
164+
const mod int = 1e9 + 7
165+
suf := make([][]int, m+1)
166+
for i := range suf {
167+
suf[i] = make([]int, n+1)
168+
for j := n - 1; j >= 0; j-- {
169+
suf[i][j] = (suf[i][min(n, j+i)] + nums[j]) % mod
170+
}
171+
}
172+
for _, q := range queries {
173+
x, y := q[0], q[1]
174+
if y <= m {
175+
ans = append(ans, suf[y][x])
176+
} else {
177+
s := 0
178+
for i := x; i < n; i += y {
179+
s = (s + nums[i]) % mod
180+
}
181+
ans = append(ans, s)
182+
}
183+
}
184+
return
185+
}
186+
187+
func min(a, b int) int {
188+
if a < b {
189+
return a
190+
}
191+
return b
192+
}
193+
```
65194

195+
### **TypeScript**
196+
197+
```ts
198+
function solve(nums: number[], queries: number[][]): number[] {
199+
const n = nums.length;
200+
const m = Math.floor(Math.sqrt(n));
201+
const mod = 10 ** 9 + 7;
202+
const suf: number[][] = Array(m + 1)
203+
.fill(0)
204+
.map(() => Array(n + 1).fill(0));
205+
for (let i = 1; i <= m; ++i) {
206+
for (let j = n - 1; j >= 0; --j) {
207+
suf[i][j] = (suf[i][Math.min(n, j + i)] + nums[j]) % mod;
208+
}
209+
}
210+
const ans: number[] = [];
211+
for (const [x, y] of queries) {
212+
if (y <= m) {
213+
ans.push(suf[y][x]);
214+
} else {
215+
let s = 0;
216+
for (let i = x; i < n; i += y) {
217+
s = (s + nums[i]) % mod;
218+
}
219+
ans.push(s);
220+
}
221+
}
222+
return ans;
223+
}
66224
```
67225

68226
### **...**

solution/1700-1799/1714.Sum Of Special Evenly-Spaced Elements In Array/README_EN.md

+146-1
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,158 @@
4848
### **Python3**
4949

5050
```python
51-
51+
class Solution:
52+
def solve(self, nums: List[int], queries: List[List[int]]) -> List[int]:
53+
mod = 10**9 + 7
54+
n = len(nums)
55+
m = int(sqrt(n))
56+
suf = [[0] * (n + 1) for _ in range(m + 1)]
57+
for i in range(1, m + 1):
58+
for j in range(n - 1, -1, -1):
59+
suf[i][j] = suf[i][min(n, j + i)] + nums[j]
60+
ans = []
61+
for x, y in queries:
62+
if y <= m:
63+
ans.append(suf[y][x] % mod)
64+
else:
65+
ans.append(sum(nums[x::y]) % mod)
66+
return ans
5267
```
5368

5469
### **Java**
5570

5671
```java
72+
class Solution {
73+
public int[] solve(int[] nums, int[][] queries) {
74+
int n = nums.length;
75+
int m = (int) Math.sqrt(n);
76+
final int mod = (int) 1e9 + 7;
77+
int[][] suf = new int[m + 1][n + 1];
78+
for (int i = 1; i <= m; ++i) {
79+
for (int j = n - 1; j >= 0; --j) {
80+
suf[i][j] = (suf[i][Math.min(n, j + i)] + nums[j]) % mod;
81+
}
82+
}
83+
int k = queries.length;
84+
int[] ans = new int[k];
85+
for (int i = 0; i < k; ++i) {
86+
int x = queries[i][0];
87+
int y = queries[i][1];
88+
if (y <= m) {
89+
ans[i] = suf[y][x];
90+
} else {
91+
int s = 0;
92+
for (int j = x; j < n; j += y) {
93+
s = (s + nums[j]) % mod;
94+
}
95+
ans[i] = s;
96+
}
97+
}
98+
return ans;
99+
}
100+
}
101+
```
102+
103+
### **C++**
104+
105+
```cpp
106+
class Solution {
107+
public:
108+
vector<int> solve(vector<int>& nums, vector<vector<int>>& queries) {
109+
int n = nums.size();
110+
int m = (int) sqrt(n);
111+
const int mod = 1e9 + 7;
112+
int suf[m + 1][n + 1];
113+
memset(suf, 0, sizeof(suf));
114+
for (int i = 1; i <= m; ++i) {
115+
for (int j = n - 1; ~j; --j) {
116+
suf[i][j] = (suf[i][min(n, j + i)] + nums[j]) % mod;
117+
}
118+
}
119+
vector<int> ans;
120+
for (auto& q : queries) {
121+
int x = q[0], y = q[1];
122+
if (y <= m) {
123+
ans.push_back(suf[y][x]);
124+
} else {
125+
int s = 0;
126+
for (int i = x; i < n; i += y) {
127+
s = (s + nums[i]) % mod;
128+
}
129+
ans.push_back(s);
130+
}
131+
}
132+
return ans;
133+
}
134+
};
135+
```
136+
137+
### **Go**
138+
139+
```go
140+
func solve(nums []int, queries [][]int) (ans []int) {
141+
n := len(nums)
142+
m := int(math.Sqrt(float64(n)))
143+
const mod int = 1e9 + 7
144+
suf := make([][]int, m+1)
145+
for i := range suf {
146+
suf[i] = make([]int, n+1)
147+
for j := n - 1; j >= 0; j-- {
148+
suf[i][j] = (suf[i][min(n, j+i)] + nums[j]) % mod
149+
}
150+
}
151+
for _, q := range queries {
152+
x, y := q[0], q[1]
153+
if y <= m {
154+
ans = append(ans, suf[y][x])
155+
} else {
156+
s := 0
157+
for i := x; i < n; i += y {
158+
s = (s + nums[i]) % mod
159+
}
160+
ans = append(ans, s)
161+
}
162+
}
163+
return
164+
}
165+
166+
func min(a, b int) int {
167+
if a < b {
168+
return a
169+
}
170+
return b
171+
}
172+
```
57173

174+
### **TypeScript**
175+
176+
```ts
177+
function solve(nums: number[], queries: number[][]): number[] {
178+
const n = nums.length;
179+
const m = Math.floor(Math.sqrt(n));
180+
const mod = 10 ** 9 + 7;
181+
const suf: number[][] = Array(m + 1)
182+
.fill(0)
183+
.map(() => Array(n + 1).fill(0));
184+
for (let i = 1; i <= m; ++i) {
185+
for (let j = n - 1; j >= 0; --j) {
186+
suf[i][j] = (suf[i][Math.min(n, j + i)] + nums[j]) % mod;
187+
}
188+
}
189+
const ans: number[] = [];
190+
for (const [x, y] of queries) {
191+
if (y <= m) {
192+
ans.push(suf[y][x]);
193+
} else {
194+
let s = 0;
195+
for (let i = x; i < n; i += y) {
196+
s = (s + nums[i]) % mod;
197+
}
198+
ans.push(s);
199+
}
200+
}
201+
return ans;
202+
}
58203
```
59204

60205
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
vector<int> solve(vector<int>& nums, vector<vector<int>>& queries) {
4+
int n = nums.size();
5+
int m = (int) sqrt(n);
6+
const int mod = 1e9 + 7;
7+
int suf[m + 1][n + 1];
8+
memset(suf, 0, sizeof(suf));
9+
for (int i = 1; i <= m; ++i) {
10+
for (int j = n - 1; ~j; --j) {
11+
suf[i][j] = (suf[i][min(n, j + i)] + nums[j]) % mod;
12+
}
13+
}
14+
vector<int> ans;
15+
for (auto& q : queries) {
16+
int x = q[0], y = q[1];
17+
if (y <= m) {
18+
ans.push_back(suf[y][x]);
19+
} else {
20+
int s = 0;
21+
for (int i = x; i < n; i += y) {
22+
s = (s + nums[i]) % mod;
23+
}
24+
ans.push_back(s);
25+
}
26+
}
27+
return ans;
28+
}
29+
};

0 commit comments

Comments
 (0)