Skip to content

Commit cd6eed5

Browse files
authored
feat: add solutions to lc problem: No.1311 (#3406)
No.1311.Get Watched Videos by Your Friends
1 parent 248c0a7 commit cd6eed5

File tree

11 files changed

+509
-189
lines changed

11 files changed

+509
-189
lines changed

Diff for: solution/1300-1399/1310.XOR Queries of a Subarray/README.md

+15-23
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,17 @@ tags:
3232

3333
<pre>
3434
<strong>输入:</strong>arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
35-
<strong>输出:</strong>[2,7,14,8]
35+
<strong>输出:</strong>[2,7,14,8]
3636
<strong>解释:</strong>
3737
数组中元素的二进制表示形式是:
38-
1 = 0001
39-
3 = 0011
40-
4 = 0100
41-
8 = 1000
38+
1 = 0001
39+
3 = 0011
40+
4 = 0100
41+
8 = 1000
4242
查询的 XOR 值为:
43-
[0,1] = 1 xor 3 = 2
44-
[1,2] = 3 xor 4 = 7
45-
[0,3] = 1 xor 3 xor 4 xor 8 = 14
43+
[0,1] = 1 xor 3 = 2
44+
[1,2] = 3 xor 4 = 7
45+
[0,3] = 1 xor 3 xor 4 xor 8 = 14
4646
[3,3] = 8
4747
</pre>
4848

@@ -73,18 +73,18 @@ tags:
7373

7474
### 方法一:前缀异或
7575

76-
我们可以用一个长度为 $n+1$ 的前缀异或数组 $s$ 来存储数组 $arr$ 的前缀异或结果,其中 $s[i] = s[i-1] \oplus arr[i-1]$,即 $s[i]$ 表示 $arr$ 中下标 $[0,i-1]$ 的元素的异或结果
76+
我们可以用一个长度为 $n+1$ 的前缀异或数组 $s$ 来存储数组 $\textit{arr}$ 的前缀异或结果,其中 $s[i] = s[i-1] \oplus \textit{arr}[i-1]$,即 $s[i]$ 表示 $\textit{arr}$ 的前 $i$ 个元素的异或结果
7777

7878
那么对于一个查询 $[l,r]$,我们可以得到:
7979

8080
$$
8181
\begin{aligned}
82-
arr[l] \oplus arr[l+1] \oplus \cdots \oplus arr[r] &= (arr[0] \oplus arr[1] \oplus \cdots \oplus arr[l-1]) \oplus (arr[0] \oplus arr[1] \oplus \cdots \oplus arr[r]) \\
82+
\textit{arr}[l] \oplus \textit{arr}[l+1] \oplus \cdots \oplus \textit{arr}[r] &= (\textit{arr}[0] \oplus \textit{arr}[1] \oplus \cdots \oplus \textit{arr}[l-1]) \oplus (\textit{arr}[0] \oplus \textit{arr}[1] \oplus \cdots \oplus \textit{arr}[r]) \\
8383
&= s[l] \oplus s[r+1]
8484
\end{aligned}
8585
$$
8686

87-
时间复杂度 $O(n+m)$,空间复杂度 $O(n)$。其中 $n$ 和 $m$ 分别是数组 $arr$ 和查询数组 $queries$ 的长度。
87+
时间复杂度 $O(n+m)$,空间复杂度 $O(n)$。其中 $n$ 和 $m$ 分别是数组 $\textit{arr}$ 的长度和查询数组 $\textit{queries}$ 的长度。
8888

8989
<!-- tabs:start -->
9090

@@ -162,15 +162,11 @@ func xorQueries(arr []int, queries [][]int) (ans []int) {
162162
```ts
163163
function xorQueries(arr: number[], queries: number[][]): number[] {
164164
const n = arr.length;
165-
const s: number[] = new Array(n + 1).fill(0);
165+
const s: number[] = Array(n + 1).fill(0);
166166
for (let i = 0; i < n; ++i) {
167167
s[i + 1] = s[i] ^ arr[i];
168168
}
169-
const ans: number[] = [];
170-
for (const [l, r] of queries) {
171-
ans.push(s[r + 1] ^ s[l]);
172-
}
173-
return ans;
169+
return queries.map(([l, r]) => s[r + 1] ^ s[l]);
174170
}
175171
```
176172

@@ -184,15 +180,11 @@ function xorQueries(arr: number[], queries: number[][]): number[] {
184180
*/
185181
var xorQueries = function (arr, queries) {
186182
const n = arr.length;
187-
const s = new Array(n + 1).fill(0);
183+
const s = Array(n + 1).fill(0);
188184
for (let i = 0; i < n; ++i) {
189185
s[i + 1] = s[i] ^ arr[i];
190186
}
191-
const ans = [];
192-
for (const [l, r] of queries) {
193-
ans.push(s[r + 1] ^ s[l]);
194-
}
195-
return ans;
187+
return queries.map(([l, r]) => s[r + 1] ^ s[l]);
196188
};
197189
```
198190

Diff for: solution/1300-1399/1310.XOR Queries of a Subarray/README_EN.md

+27-22
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@ tags:
3131

3232
<pre>
3333
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
34-
<strong>Output:</strong> [2,7,14,8]
35-
<strong>Explanation:</strong>
34+
<strong>Output:</strong> [2,7,14,8]
35+
<strong>Explanation:</strong>
3636
The binary representation of the elements in the array are:
37-
1 = 0001
38-
3 = 0011
39-
4 = 0100
40-
8 = 1000
37+
1 = 0001
38+
3 = 0011
39+
4 = 0100
40+
8 = 1000
4141
The XOR values for queries are:
42-
[0,1] = 1 xor 3 = 2
43-
[1,2] = 3 xor 4 = 7
44-
[0,3] = 1 xor 3 xor 4 xor 8 = 14
42+
[0,1] = 1 xor 3 = 2
43+
[1,2] = 3 xor 4 = 7
44+
[0,3] = 1 xor 3 xor 4 xor 8 = 14
4545
[3,3] = 8
4646
</pre>
4747

@@ -68,7 +68,20 @@ The XOR values for queries are:
6868

6969
<!-- solution:start -->
7070

71-
### Solution 1
71+
### Solution 1: Prefix XOR
72+
73+
We can use a prefix XOR array $s$ of length $n+1$ to store the prefix XOR results of the array $\textit{arr}$, where $s[i] = s[i-1] \oplus \textit{arr}[i-1]$. That is, $s[i]$ represents the XOR result of the first $i$ elements of $\textit{arr}$.
74+
75+
For a query $[l, r]$, we can obtain:
76+
77+
$$
78+
\begin{aligned}
79+
\textit{arr}[l] \oplus \textit{arr}[l+1] \oplus \cdots \oplus \textit{arr}[r] &= (\textit{arr}[0] \oplus \textit{arr}[1] \oplus \cdots \oplus \textit{arr}[l-1]) \oplus (\textit{arr}[0] \oplus \textit{arr}[1] \oplus \cdots \oplus \textit{arr}[r]) \\
80+
&= s[l] \oplus s[r+1]
81+
\end{aligned}
82+
$$
83+
84+
Time complexity is $O(n+m)$, and space complexity is $O(n)$. Here, $n$ and $m$ are the lengths of the array $\textit{arr}$ and the query array $\textit{queries}$, respectively.
7285

7386
<!-- tabs:start -->
7487

@@ -146,15 +159,11 @@ func xorQueries(arr []int, queries [][]int) (ans []int) {
146159
```ts
147160
function xorQueries(arr: number[], queries: number[][]): number[] {
148161
const n = arr.length;
149-
const s: number[] = new Array(n + 1).fill(0);
162+
const s: number[] = Array(n + 1).fill(0);
150163
for (let i = 0; i < n; ++i) {
151164
s[i + 1] = s[i] ^ arr[i];
152165
}
153-
const ans: number[] = [];
154-
for (const [l, r] of queries) {
155-
ans.push(s[r + 1] ^ s[l]);
156-
}
157-
return ans;
166+
return queries.map(([l, r]) => s[r + 1] ^ s[l]);
158167
}
159168
```
160169

@@ -168,15 +177,11 @@ function xorQueries(arr: number[], queries: number[][]): number[] {
168177
*/
169178
var xorQueries = function (arr, queries) {
170179
const n = arr.length;
171-
const s = new Array(n + 1).fill(0);
180+
const s = Array(n + 1).fill(0);
172181
for (let i = 0; i < n; ++i) {
173182
s[i + 1] = s[i] ^ arr[i];
174183
}
175-
const ans = [];
176-
for (const [l, r] of queries) {
177-
ans.push(s[r + 1] ^ s[l]);
178-
}
179-
return ans;
184+
return queries.map(([l, r]) => s[r + 1] ^ s[l]);
180185
};
181186
```
182187

Diff for: solution/1300-1399/1310.XOR Queries of a Subarray/Solution.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,9 @@
55
*/
66
var xorQueries = function (arr, queries) {
77
const n = arr.length;
8-
const s = new Array(n + 1).fill(0);
8+
const s = Array(n + 1).fill(0);
99
for (let i = 0; i < n; ++i) {
1010
s[i + 1] = s[i] ^ arr[i];
1111
}
12-
const ans = [];
13-
for (const [l, r] of queries) {
14-
ans.push(s[r + 1] ^ s[l]);
15-
}
16-
return ans;
12+
return queries.map(([l, r]) => s[r + 1] ^ s[l]);
1713
};
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
function xorQueries(arr: number[], queries: number[][]): number[] {
22
const n = arr.length;
3-
const s: number[] = new Array(n + 1).fill(0);
3+
const s: number[] = Array(n + 1).fill(0);
44
for (let i = 0; i < n; ++i) {
55
s[i + 1] = s[i] ^ arr[i];
66
}
7-
const ans: number[] = [];
8-
for (const [l, r] of queries) {
9-
ans.push(s[r + 1] ^ s[l]);
10-
}
11-
return ans;
7+
return queries.map(([l, r]) => s[r + 1] ^ s[l]);
128
}

0 commit comments

Comments
 (0)