Skip to content

Commit 28646b0

Browse files
authored
feat: add solutions to lc problems: No.1817,1826,1833,1835,1836 (#1759)
* No.1817.Find the Users Active Minutes * No.1826.Faulty Sensor * No.1833.Maximum Ice Cream Bars * No.1835.Find XOR Sum of All Pairs Bitwise AND * No.1836.Remove Duplicates From an Unsorted Linked List
1 parent d1b1de9 commit 28646b0

File tree

16 files changed

+335
-10
lines changed

16 files changed

+335
-10
lines changed

solution/1800-1899/1817.Finding the Users Active Minutes/README.md

+20-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ ID=2 的用户执行操作的分钟分别是:2 和 3 。因此,该用户的
6060

6161
我们用哈希表 $d$ 记录每个用户的所有去重操作时间,然后遍历哈希表,统计每个用户的用户活跃分钟数,最后统计每个用户活跃分钟数的分布情况。
6262

63-
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 `logs` 的长度。
63+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $logs$ 的长度。
6464

6565
<!-- tabs:start -->
6666

@@ -141,6 +141,25 @@ func findingUsersActiveMinutes(logs [][]int, k int) []int {
141141
}
142142
```
143143

144+
### **TypeScript**
145+
146+
```ts
147+
function findingUsersActiveMinutes(logs: number[][], k: number): number[] {
148+
const d: Map<number, Set<number>> = new Map();
149+
for (const [i, t] of logs) {
150+
if (!d.has(i)) {
151+
d.set(i, new Set<number>());
152+
}
153+
d.get(i)!.add(t);
154+
}
155+
const ans: number[] = Array(k).fill(0);
156+
for (const [_, ts] of d) {
157+
++ans[ts.size - 1];
158+
}
159+
return ans;
160+
}
161+
```
162+
144163
### **...**
145164

146165
```

solution/1800-1899/1817.Finding the Users Active Minutes/README_EN.md

+25
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ Hence, answer[1] = 1, answer[2] = 1, and the remaining values are 0.
5050

5151
## Solutions
5252

53+
**Solution 1: Hash Table**
54+
55+
We use a hash table $d$ to record all the unique operation times of each user, and then traverse the hash table to count the number of active minutes for each user. Finally, we count the distribution of the number of active minutes for each user.
56+
57+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the $logs$ array.
58+
5359
<!-- tabs:start -->
5460

5561
### **Python3**
@@ -125,6 +131,25 @@ func findingUsersActiveMinutes(logs [][]int, k int) []int {
125131
}
126132
```
127133

134+
### **TypeScript**
135+
136+
```ts
137+
function findingUsersActiveMinutes(logs: number[][], k: number): number[] {
138+
const d: Map<number, Set<number>> = new Map();
139+
for (const [i, t] of logs) {
140+
if (!d.has(i)) {
141+
d.set(i, new Set<number>());
142+
}
143+
d.get(i)!.add(t);
144+
}
145+
const ans: number[] = Array(k).fill(0);
146+
for (const [_, ts] of d) {
147+
++ans[ts.size - 1];
148+
}
149+
return ans;
150+
}
151+
```
152+
128153
### **...**
129154

130155
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function findingUsersActiveMinutes(logs: number[][], k: number): number[] {
2+
const d: Map<number, Set<number>> = new Map();
3+
for (const [i, t] of logs) {
4+
if (!d.has(i)) {
5+
d.set(i, new Set<number>());
6+
}
7+
d.get(i)!.add(t);
8+
}
9+
const ans: number[] = Array(k).fill(0);
10+
for (const [_, ts] of d) {
11+
++ans[ts.size - 1];
12+
}
13+
return ans;
14+
}

solution/1800-1899/1819.Number of Different Subsequences GCDs/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@
5555

5656
**方法一:枚举 + 数学**
5757

58-
对于数组 `nums` 的所有子序列,其最大公约数一定不超过数组中的最大值 $mx$。
58+
对于数组 $nums$ 的所有子序列,其最大公约数一定不超过数组中的最大值 $mx$。
5959

60-
因此我们可以枚举 $[1,.. mx]$ 中的每个数 $x$,判断 $x$ 是否为数组 `nums` 的子序列的最大公约数,如果是,则答案加一。
60+
因此我们可以枚举 $[1,.. mx]$ 中的每个数 $x$,判断 $x$ 是否为数组 $nums$ 的子序列的最大公约数,如果是,则答案加一。
6161

62-
那么问题转换为:判断 $x$ 是否为数组 `nums` 的子序列的最大公约数。我们可以通过枚举 $x$ 的倍数 $y$,判断 $y$ 是否在数组 `nums` 中,如果 $y$ 在数组 `nums` 中,则计算 $y$ 的最大公约数 $g$,如果出现 $g = x$,则 $x$ 是数组 `nums` 的子序列的最大公约数。
62+
那么问题转换为:判断 $x$ 是否为数组 $nums$ 的子序列的最大公约数。我们可以通过枚举 $x$ 的倍数 $y$,判断 $y$ 是否在数组 $nums$ 中,如果 $y$ 在数组 $nums$ 中,则计算 $y$ 的最大公约数 $g$,如果出现 $g = x$,则 $x$ 是数组 $nums$ 的子序列的最大公约数。
6363

64-
时间复杂度 $O(n + M \times \log M)$,空间复杂度 $O(M)$。其中 $n$ 和 $M$ 分别是数组 `nums` 的长度和数组 `nums` 中的最大值。
64+
时间复杂度 $O(n + M \times \log M)$,空间复杂度 $O(M)$。其中 $n$ 和 $M$ 分别是数组 $nums$ 的长度和数组 $nums$ 中的最大值。
6565

6666
<!-- tabs:start -->
6767

solution/1800-1899/1826.Faulty Sensor/README.md

+25
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,31 @@ func badSensor(sensor1 []int, sensor2 []int) int {
152152
}
153153
```
154154

155+
### **TypeScript**
156+
157+
```ts
158+
function badSensor(sensor1: number[], sensor2: number[]): number {
159+
let i = 0;
160+
const n = sensor1.length;
161+
while (i < n - 1) {
162+
if (sensor1[i] !== sensor2[i]) {
163+
break;
164+
}
165+
++i;
166+
}
167+
while (i < n - 1) {
168+
if (sensor1[i + 1] !== sensor2[i]) {
169+
return 1;
170+
}
171+
if (sensor1[i] !== sensor2[i + 1]) {
172+
return 2;
173+
}
174+
++i;
175+
}
176+
return -1;
177+
}
178+
```
179+
155180
### **...**
156181

157182
```

solution/1800-1899/1826.Faulty Sensor/README_EN.md

+25
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,31 @@ func badSensor(sensor1 []int, sensor2 []int) int {
133133
}
134134
```
135135

136+
### **TypeScript**
137+
138+
```ts
139+
function badSensor(sensor1: number[], sensor2: number[]): number {
140+
let i = 0;
141+
const n = sensor1.length;
142+
while (i < n - 1) {
143+
if (sensor1[i] !== sensor2[i]) {
144+
break;
145+
}
146+
++i;
147+
}
148+
while (i < n - 1) {
149+
if (sensor1[i + 1] !== sensor2[i]) {
150+
return 1;
151+
}
152+
if (sensor1[i] !== sensor2[i + 1]) {
153+
return 2;
154+
}
155+
++i;
156+
}
157+
return -1;
158+
}
159+
```
160+
136161
### **...**
137162

138163
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function badSensor(sensor1: number[], sensor2: number[]): number {
2+
let i = 0;
3+
const n = sensor1.length;
4+
while (i < n - 1) {
5+
if (sensor1[i] !== sensor2[i]) {
6+
break;
7+
}
8+
++i;
9+
}
10+
while (i < n - 1) {
11+
if (sensor1[i + 1] !== sensor2[i]) {
12+
return 1;
13+
}
14+
if (sensor1[i] !== sensor2[i + 1]) {
15+
return 2;
16+
}
17+
++i;
18+
}
19+
return -1;
20+
}

solution/1800-1899/1833.Maximum Ice Cream Bars/README.md

+16
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,22 @@ func maxIceCream(costs []int, coins int) int {
134134
}
135135
```
136136

137+
### **TypeScript**
138+
139+
```ts
140+
function maxIceCream(costs: number[], coins: number): number {
141+
costs.sort((a, b) => a - b);
142+
const n = costs.length;
143+
for (let i = 0; i < n; ++i) {
144+
if (coins < costs[i]) {
145+
return i;
146+
}
147+
coins -= costs[i];
148+
}
149+
return n;
150+
}
151+
```
152+
137153
### **JavaScript**
138154

139155
```js

solution/1800-1899/1833.Maximum Ice Cream Bars/README_EN.md

+23-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,13 @@
5151

5252
## Solutions
5353

54-
Pay attention to the data range. The question can easily mislead us to use the 01 backpack (it will overtime). In fact, this question is a simple "greedy problem" (choose low-priced ice cream first)
54+
**Solution 1: Greedy + Sorting**
55+
56+
To buy as many ice creams as possible, and they can be purchased in any order, we should prioritize choosing ice creams with lower prices.
57+
58+
Sort the $costs$ array, and then start buying from the ice cream with the lowest price, one by one, until it is no longer possible to buy, and return the number of ice creams that can be bought.
59+
60+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$, where $n$ is the length of the $costs$ array.
5561

5662
<!-- tabs:start -->
5763

@@ -118,6 +124,22 @@ func maxIceCream(costs []int, coins int) int {
118124
}
119125
```
120126

127+
### **TypeScript**
128+
129+
```ts
130+
function maxIceCream(costs: number[], coins: number): number {
131+
costs.sort((a, b) => a - b);
132+
const n = costs.length;
133+
for (let i = 0; i < n; ++i) {
134+
if (coins < costs[i]) {
135+
return i;
136+
}
137+
coins -= costs[i];
138+
}
139+
return n;
140+
}
141+
```
142+
121143
### **JavaScript**
122144

123145
```js
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function maxIceCream(costs: number[], coins: number): number {
2+
costs.sort((a, b) => a - b);
3+
const n = costs.length;
4+
for (let i = 0; i < n; ++i) {
5+
if (coins < costs[i]) {
6+
return i;
7+
}
8+
coins -= costs[i];
9+
}
10+
return n;
11+
}

solution/1800-1899/1835.Find XOR Sum of All Pairs Bitwise AND/README.md

+13-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949

5050
**方法一:位运算**
5151

52-
假设数组 `arr1` 的元素分别为 $a_1, a_2, \cdots, a_n$,数组 `arr2` 的元素分别为 $b_1, b_2, \cdots, b_m$,那么题目答案为:
52+
假设数组 $arr1$ 的元素分别为 $a_1, a_2, \cdots, a_n$,数组 $arr2$ 的元素分别为 $b_1, b_2, \cdots, b_m$,那么题目答案为:
5353

5454
$$
5555
\begin{aligned}
@@ -66,9 +66,9 @@ $$
6666
\text{ans} = (a_1 \oplus a_2 \oplus \cdots \oplus a_n) \wedge (b_1 \oplus b_2 \oplus \cdots \oplus b_m)
6767
$$
6868

69-
即,数组 `arr1` 的异或和与数组 `arr2` 的异或和的与运算结果。
69+
即,数组 $arr1$ 的异或和与数组 $arr2$ 的异或和的与运算结果。
7070

71-
时间复杂度 $O(n + m)$,空间复杂度 $O(1)$。其中 $n$ 和 $m$ 分别为数组 `arr1``arr2` 的长度。
71+
时间复杂度 $O(n + m)$,其中 $n$ 和 $m$ 分别为数组 $arr1$$arr2$ 的长度。空间复杂度 $O(1)$
7272

7373
<!-- tabs:start -->
7474

@@ -131,6 +131,16 @@ func getXORSum(arr1 []int, arr2 []int) int {
131131
}
132132
```
133133

134+
### **TypeScript**
135+
136+
```ts
137+
function getXORSum(arr1: number[], arr2: number[]): number {
138+
const a = arr1.reduce((acc, x) => acc ^ x);
139+
const b = arr2.reduce((acc, x) => acc ^ x);
140+
return a & b;
141+
}
142+
```
143+
134144
### **...**
135145

136146
```

solution/1800-1899/1835.Find XOR Sum of All Pairs Bitwise AND/README_EN.md

+33
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,29 @@ The XOR sum = 0 XOR 1 XOR 2 XOR 0 XOR 2 XOR 1 = 0.
4444

4545
## Solutions
4646

47+
**Solution 1: Bitwise Operation**
48+
49+
Assume that the elements of array $arr1$ are $a_1, a_2, ..., a_n$, and the elements of array $arr2$ are $b_1, b_2, ..., b_m$. Then, the answer to the problem is:
50+
51+
$$
52+
\begin{aligned}
53+
\text{ans} &= (a_1 \wedge b_1) \oplus (a_1 \wedge b_2) ... (a_1 \wedge b_m) \\
54+
&\quad \oplus (a_2 \wedge b_1) \oplus (a_2 \wedge b_2) ... (a_2 \wedge b_m) \\
55+
&\quad \oplus \cdots \\
56+
&\quad \oplus (a_n \wedge b_1) \oplus (a_n \wedge b_2) ... (a_n \wedge b_m) \\
57+
\end{aligned}
58+
$$
59+
60+
Since in Boolean algebra, the XOR operation is addition without carry, and the AND operation is multiplication, the above formula can be simplified as:
61+
62+
$$
63+
\text{ans} = (a_1 \oplus a_2 \oplus \cdots \oplus a_n) \wedge (b_1 \oplus b_2 \oplus \cdots \oplus b_m)
64+
$$
65+
66+
That is, the bitwise AND of the XOR sum of array $arr1$ and the XOR sum of array $arr2$.
67+
68+
The time complexity is $O(n + m)$, where $n$ and $m$ are the lengths of arrays $arr1$ and $arr2$, respectively. The space complexity is $O(1)$.
69+
4770
<!-- tabs:start -->
4871

4972
### **Python3**
@@ -101,6 +124,16 @@ func getXORSum(arr1 []int, arr2 []int) int {
101124
}
102125
```
103126

127+
### **TypeScript**
128+
129+
```ts
130+
function getXORSum(arr1: number[], arr2: number[]): number {
131+
const a = arr1.reduce((acc, x) => acc ^ x);
132+
const b = arr2.reduce((acc, x) => acc ^ x);
133+
return a & b;
134+
}
135+
```
136+
104137
### **...**
105138

106139
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function getXORSum(arr1: number[], arr2: number[]): number {
2+
const a = arr1.reduce((acc, x) => acc ^ x);
3+
const b = arr2.reduce((acc, x) => acc ^ x);
4+
return a & b;
5+
}

0 commit comments

Comments
 (0)