Skip to content

Commit 28646b0

Browse files
authored
feat: add solutions to lc problems: No.1817,1826,1833,1835,1836 (doocs#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

Lines changed: 20 additions & 1 deletion
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

Lines changed: 25 additions & 0 deletions
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
```
Lines changed: 14 additions & 0 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 25 additions & 0 deletions
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

Lines changed: 25 additions & 0 deletions
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
```
Lines changed: 20 additions & 0 deletions
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

Lines changed: 16 additions & 0 deletions
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

Lines changed: 23 additions & 1 deletion
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
Lines changed: 11 additions & 0 deletions
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+
}

0 commit comments

Comments
 (0)