From 6465b9b30c2fe9c3dc93935a5fbef77e118fdea5 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 22 Mar 2024 20:22:55 +0800 Subject: [PATCH] feat: add solutions to lc problems: No.1604~1608 (#2478) --- .../README.md | 34 ++++++++++++++- .../README_EN.md | 42 ++++++++++++++++++- .../Solution.ts | 29 +++++++++++++ .../README_EN.md | 14 ++++++- .../README.md | 6 +-- .../README_EN.md | 14 ++++++- 6 files changed, 131 insertions(+), 8 deletions(-) create mode 100644 solution/1600-1699/1604.Alert Using Same Key-Card Three or More Times in a One Hour Period/Solution.ts diff --git a/solution/1600-1699/1604.Alert Using Same Key-Card Three or More Times in a One Hour Period/README.md b/solution/1600-1699/1604.Alert Using Same Key-Card Three or More Times in a One Hour Period/README.md index 87954a89d9ae3..cbaa3e3af1403 100644 --- a/solution/1600-1699/1604.Alert Using Same Key-Card Three or More Times in a One Hour Period/README.md +++ b/solution/1600-1699/1604.Alert Using Same Key-Card Three or More Times in a One Hour Period/README.md @@ -59,7 +59,7 @@ 最后,将答案数组按照字典序排序,即可得到答案。 -时间复杂度 $O(n \times \log n)$,其中 $n$ 是数组 $keyName$ 的长度。 +时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 是打卡记录的数量。 @@ -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; +} +``` + diff --git a/solution/1600-1699/1604.Alert Using Same Key-Card Three or More Times in a One Hour Period/README_EN.md b/solution/1600-1699/1604.Alert Using Same Key-Card Three or More Times in a One Hour Period/README_EN.md index b974248d356d2..a7649ef5bdec5 100644 --- a/solution/1600-1699/1604.Alert Using Same Key-Card Three or More Times in a One Hour Period/README_EN.md +++ b/solution/1600-1699/1604.Alert Using Same Key-Card Three or More Times in a One Hour Period/README_EN.md @@ -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. @@ -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; +} +``` + diff --git a/solution/1600-1699/1604.Alert Using Same Key-Card Three or More Times in a One Hour Period/Solution.ts b/solution/1600-1699/1604.Alert Using Same Key-Card Three or More Times in a One Hour Period/Solution.ts new file mode 100644 index 0000000000000..f3de6985f1fa7 --- /dev/null +++ b/solution/1600-1699/1604.Alert Using Same Key-Card Three or More Times in a One Hour Period/Solution.ts @@ -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; +} diff --git a/solution/1600-1699/1605.Find Valid Matrix Given Row and Column Sums/README_EN.md b/solution/1600-1699/1605.Find Valid Matrix Given Row and Column Sums/README_EN.md index 99837797ef5d6..3d2deb9b6f01e 100644 --- a/solution/1600-1699/1605.Find Valid Matrix Given Row and Column Sums/README_EN.md +++ b/solution/1600-1699/1605.Find Valid Matrix Given Row and Column Sums/README_EN.md @@ -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. diff --git a/solution/1600-1699/1608.Special Array With X Elements Greater Than or Equal X/README.md b/solution/1600-1699/1608.Special Array With X Elements Greater Than or Equal X/README.md index b39b2742638d7..3761acc6ec132 100644 --- a/solution/1600-1699/1608.Special Array With X Elements Greater Than or Equal X/README.md +++ b/solution/1600-1699/1608.Special Array With X Elements Greater Than or Equal X/README.md @@ -59,9 +59,9 @@ x 不能取更大的值,因为 nums 中只有两个元素。 ### 方法一:暴力枚举 -在 $[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)$。 @@ -165,7 +165,7 @@ impl Solution { 接下来同样枚举 $x$,利用二分查找,找到 `nums` 中第一个大于等于 $x$ 的元素,快速统计出 `nums` 中大于等于 $x$ 的元素个数。 -时间复杂度 $O(n\log n)$。 +时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 是数组的长度。 diff --git a/solution/1600-1699/1608.Special Array With X Elements Greater Than or Equal X/README_EN.md b/solution/1600-1699/1608.Special Array With X Elements Greater Than or Equal X/README_EN.md index e8764250df16f..98a7478dfd9c4 100644 --- a/solution/1600-1699/1608.Special Array With X Elements Greater Than or Equal X/README_EN.md +++ b/solution/1600-1699/1608.Special Array With X Elements Greater Than or Equal X/README_EN.md @@ -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)$. @@ -149,7 +153,13 @@ impl Solution { -### 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.