Skip to content

Commit 6465b9b

Browse files
authored
feat: add solutions to lc problems: No.1604~1608 (#2478)
1 parent 8083b7a commit 6465b9b

File tree

6 files changed

+131
-8
lines changed

6 files changed

+131
-8
lines changed

solution/1600-1699/1604.Alert Using Same Key-Card Three or More Times in a One Hour Period/README.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959

6060
最后,将答案数组按照字典序排序,即可得到答案。
6161

62-
时间复杂度 $O(n \times \log n)$,其中 $n$ 是数组 $keyName$ 的长度
62+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 是打卡记录的数量
6363

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

@@ -171,6 +171,38 @@ func alertNames(keyName []string, keyTime []string) (ans []string) {
171171
}
172172
```
173173

174+
```ts
175+
function alertNames(keyName: string[], keyTime: string[]): string[] {
176+
const d: { [name: string]: number[] } = {};
177+
for (let i = 0; i < keyName.length; ++i) {
178+
const name = keyName[i];
179+
const t = keyTime[i];
180+
const minutes = +t.slice(0, 2) * 60 + +t.slice(3);
181+
if (d[name] === undefined) {
182+
d[name] = [];
183+
}
184+
d[name].push(minutes);
185+
}
186+
const ans: string[] = [];
187+
for (const name in d) {
188+
if (d.hasOwnProperty(name)) {
189+
const ts = d[name];
190+
if (ts.length > 2) {
191+
ts.sort((a, b) => a - b);
192+
for (let i = 0; i < ts.length - 2; ++i) {
193+
if (ts[i + 2] - ts[i] <= 60) {
194+
ans.push(name);
195+
break;
196+
}
197+
}
198+
}
199+
}
200+
}
201+
ans.sort();
202+
return ans;
203+
}
204+
```
205+
174206
<!-- tabs:end -->
175207

176208
<!-- end -->

solution/1600-1699/1604.Alert Using Same Key-Card Three or More Times in a One Hour Period/README_EN.md

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,15 @@
4747

4848
## Solutions
4949

50-
### Solution 1
50+
### Solution 1: Hash Table + Sorting
51+
52+
First, we use a hash table $d$ to record all the clock-in times of each employee.
53+
54+
Then we traverse the hash table. For each employee, we first check whether the number of clock-in times is greater than or equal to 3. If not, we skip this employee. Otherwise, we sort all the clock-in times of this employee in chronological order, and then traverse the sorted clock-in times to check whether the two times at a distance of 2 indices are within the same hour. If so, we add this employee to the answer array.
55+
56+
Finally, we sort the answer array in lexicographical order to get the answer.
57+
58+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Where $n$ is the number of clock-in records.
5159

5260
<!-- tabs:start -->
5361

@@ -159,6 +167,38 @@ func alertNames(keyName []string, keyTime []string) (ans []string) {
159167
}
160168
```
161169

170+
```ts
171+
function alertNames(keyName: string[], keyTime: string[]): string[] {
172+
const d: { [name: string]: number[] } = {};
173+
for (let i = 0; i < keyName.length; ++i) {
174+
const name = keyName[i];
175+
const t = keyTime[i];
176+
const minutes = +t.slice(0, 2) * 60 + +t.slice(3);
177+
if (d[name] === undefined) {
178+
d[name] = [];
179+
}
180+
d[name].push(minutes);
181+
}
182+
const ans: string[] = [];
183+
for (const name in d) {
184+
if (d.hasOwnProperty(name)) {
185+
const ts = d[name];
186+
if (ts.length > 2) {
187+
ts.sort((a, b) => a - b);
188+
for (let i = 0; i < ts.length - 2; ++i) {
189+
if (ts[i + 2] - ts[i] <= 60) {
190+
ans.push(name);
191+
break;
192+
}
193+
}
194+
}
195+
}
196+
}
197+
ans.sort();
198+
return ans;
199+
}
200+
```
201+
162202
<!-- tabs:end -->
163203

164204
<!-- end -->
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
function alertNames(keyName: string[], keyTime: string[]): string[] {
2+
const d: { [name: string]: number[] } = {};
3+
for (let i = 0; i < keyName.length; ++i) {
4+
const name = keyName[i];
5+
const t = keyTime[i];
6+
const minutes = +t.slice(0, 2) * 60 + +t.slice(3);
7+
if (d[name] === undefined) {
8+
d[name] = [];
9+
}
10+
d[name].push(minutes);
11+
}
12+
const ans: string[] = [];
13+
for (const name in d) {
14+
if (d.hasOwnProperty(name)) {
15+
const ts = d[name];
16+
if (ts.length > 2) {
17+
ts.sort((a, b) => a - b);
18+
for (let i = 0; i < ts.length - 2; ++i) {
19+
if (ts[i + 2] - ts[i] <= 60) {
20+
ans.push(name);
21+
break;
22+
}
23+
}
24+
}
25+
}
26+
}
27+
ans.sort();
28+
return ans;
29+
}

solution/1600-1699/1605.Find Valid Matrix Given Row and Column Sums/README_EN.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,19 @@ Another possible matrix is: [[1,2],
4949

5050
## Solutions
5151

52-
### Solution 1
52+
### Solution 1: Greedy + Construction
53+
54+
We can first initialize an $m$ by $n$ answer matrix $ans$.
55+
56+
Next, we traverse each position $(i, j)$ in the matrix, set the element at this position to $x = \min(rowSum[i], colSum[j])$, and subtract $x$ from $rowSum[i]$ and $colSum[j]$ respectively. After traversing all positions, we can get a matrix $ans$ that meets the requirements of the problem.
57+
58+
The correctness of the above strategy is explained as follows:
59+
60+
According to the requirements of the problem, we know that the sum of $rowSum$ and $colSum$ is equal, so $rowSum[0]$ must be less than or equal to $\sum_{j = 0}^{n - 1} colSum[j]$. Therefore, after $n$ operations, $rowSum[0]$ can definitely be made $0$, and for any $j \in [0, n - 1]$, $colSum[j] \geq 0$ is guaranteed.
61+
62+
Therefore, we reduce the original problem to a subproblem with $m-1$ rows and $n$ columns, continue the above operations, until all elements in $rowSum$ and $colSum$ are $0$, we can get a matrix $ans$ that meets the requirements of the problem.
63+
64+
The time complexity is $O(m \times n)$, and the space complexity is $O(m \times n)$. Where $m$ and $n$ are the lengths of $rowSum$ and $colSum$ respectively.
5365

5466
<!-- tabs:start -->
5567

solution/1600-1699/1608.Special Array With X Elements Greater Than or Equal X/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ x 不能取更大的值,因为 nums 中只有两个元素。</pre>
5959

6060
### 方法一:暴力枚举
6161

62-
$[1..n]$ 范围内枚举 $x$,然后统计数组中大于等于 $x$ 的元素个数,记为 $cnt$。若存在 $cnt$ 与 $x$ 相等,直接返回 $x$。
62+
我们在 $[1..n]$ 范围内枚举 $x$,然后统计数组中大于等于 $x$ 的元素个数,记为 $cnt$。若存在 $cnt$ 与 $x$ 相等,直接返回 $x$。
6363

64-
时间复杂度 $O(n^2)$。
64+
时间复杂度 $O(n^2)$,其中 $n$ 是数组的长度。空间复杂度 $O(1)$
6565

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

@@ -165,7 +165,7 @@ impl Solution {
165165

166166
接下来同样枚举 $x$,利用二分查找,找到 `nums` 中第一个大于等于 $x$ 的元素,快速统计出 `nums` 中大于等于 $x$ 的元素个数。
167167

168-
时间复杂度 $O(n\log n)$。
168+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 是数组的长度
169169

170170
<!-- tabs:start -->
171171

solution/1600-1699/1608.Special Array With X Elements Greater Than or Equal X/README_EN.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ x cannot be greater since there are only 2 numbers in nums.
5151

5252
## Solutions
5353

54-
### Solution 1
54+
### Solution 1: Brute Force Enumeration
55+
56+
We enumerate $x$ in the range of $[1..n]$, and then count the number of elements in the array that are greater than or equal to $x$, denoted as $cnt$. If there exists $cnt$ equal to $x$, return $x$ directly.
57+
58+
The time complexity is $O(n^2)$, where $n$ is the length of the array. The space complexity is $O(1)$.
5559

5660
<!-- tabs:start -->
5761

@@ -149,7 +153,13 @@ impl Solution {
149153

150154
<!-- tabs:end -->
151155

152-
### Solution 2
156+
### Solution 2: Sorting + Binary Search
157+
158+
We can also sort `nums` first.
159+
160+
Next, we still enumerate $x$, and use binary search to find the first element in `nums` that is greater than or equal to $x$, quickly counting the number of elements in `nums` that are greater than or equal to $x$.
161+
162+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$. Where $n$ is the length of the array.
153163

154164
<!-- tabs:start -->
155165

0 commit comments

Comments
 (0)