Skip to content

Commit

Permalink
feat: add solutions to lc problems: No.1604~1608 (#2478)
Browse files Browse the repository at this point in the history
  • Loading branch information
yanglbme authored Mar 22, 2024
1 parent 8083b7a commit 6465b9b
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@

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

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

<!-- tabs:start -->

Expand Down Expand Up @@ -171,6 +171,38 @@ func alertNames(keyName []string, keyTime []string) (ans []string) {
}
```

```ts
function alertNames(keyName: string[], keyTime: string[]): string[] {
const d: { [name: string]: number[] } = {};
for (let i = 0; i < keyName.length; ++i) {
const name = keyName[i];
const t = keyTime[i];
const minutes = +t.slice(0, 2) * 60 + +t.slice(3);
if (d[name] === undefined) {
d[name] = [];
}
d[name].push(minutes);
}
const ans: string[] = [];
for (const name in d) {
if (d.hasOwnProperty(name)) {
const ts = d[name];
if (ts.length > 2) {
ts.sort((a, b) => a - b);
for (let i = 0; i < ts.length - 2; ++i) {
if (ts[i + 2] - ts[i] <= 60) {
ans.push(name);
break;
}
}
}
}
}
ans.sort();
return ans;
}
```

<!-- tabs:end -->

<!-- end -->
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,15 @@

## Solutions

### Solution 1
### Solution 1: Hash Table + Sorting

First, we use a hash table $d$ to record all the clock-in times of each employee.

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.

Finally, we sort the answer array in lexicographical order to get the answer.

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.

<!-- tabs:start -->

Expand Down Expand Up @@ -159,6 +167,38 @@ func alertNames(keyName []string, keyTime []string) (ans []string) {
}
```

```ts
function alertNames(keyName: string[], keyTime: string[]): string[] {
const d: { [name: string]: number[] } = {};
for (let i = 0; i < keyName.length; ++i) {
const name = keyName[i];
const t = keyTime[i];
const minutes = +t.slice(0, 2) * 60 + +t.slice(3);
if (d[name] === undefined) {
d[name] = [];
}
d[name].push(minutes);
}
const ans: string[] = [];
for (const name in d) {
if (d.hasOwnProperty(name)) {
const ts = d[name];
if (ts.length > 2) {
ts.sort((a, b) => a - b);
for (let i = 0; i < ts.length - 2; ++i) {
if (ts[i + 2] - ts[i] <= 60) {
ans.push(name);
break;
}
}
}
}
}
ans.sort();
return ans;
}
```

<!-- tabs:end -->

<!-- end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
function alertNames(keyName: string[], keyTime: string[]): string[] {
const d: { [name: string]: number[] } = {};
for (let i = 0; i < keyName.length; ++i) {
const name = keyName[i];
const t = keyTime[i];
const minutes = +t.slice(0, 2) * 60 + +t.slice(3);
if (d[name] === undefined) {
d[name] = [];
}
d[name].push(minutes);
}
const ans: string[] = [];
for (const name in d) {
if (d.hasOwnProperty(name)) {
const ts = d[name];
if (ts.length > 2) {
ts.sort((a, b) => a - b);
for (let i = 0; i < ts.length - 2; ++i) {
if (ts[i + 2] - ts[i] <= 60) {
ans.push(name);
break;
}
}
}
}
}
ans.sort();
return ans;
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,19 @@ Another possible matrix is: [[1,2],

## Solutions

### Solution 1
### Solution 1: Greedy + Construction

We can first initialize an $m$ by $n$ answer matrix $ans$.

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.

The correctness of the above strategy is explained as follows:

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.

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.

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.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ x 不能取更大的值,因为 nums 中只有两个元素。</pre>

### 方法一:暴力枚举

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

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

<!-- tabs:start -->

Expand Down Expand Up @@ -165,7 +165,7 @@ impl Solution {

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

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

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ x cannot be greater since there are only 2 numbers in nums.

## Solutions

### Solution 1
### Solution 1: Brute Force Enumeration

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.

The time complexity is $O(n^2)$, where $n$ is the length of the array. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand Down Expand Up @@ -149,7 +153,13 @@ impl Solution {

<!-- tabs:end -->

### Solution 2
### Solution 2: Sorting + Binary Search

We can also sort `nums` first.

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$.

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.

<!-- tabs:start -->

Expand Down

0 comments on commit 6465b9b

Please sign in to comment.