Skip to content

feat: update solutions to lc problems: No.1454,1455 #3356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ node_modules/
/solution/0100-0199/0177.Nth Highest Salary/Solution.sql
/solution/0100-0199/0178.Rank Scores/Solution2.sql
/solution/0500-0599/0586.Customer Placing the Largest Number of Orders/Solution2.sql
/solution/1400-1499/1454.Active Users/Solution.sql
/solution/1600-1699/1635.Hopper Company Queries I/Solution.sql
/solution/2100-2199/2118.Build the Equation/Solution.sql
/solution/2100-2199/2175.The Change in Global Rankings/Solution.sql
Expand All @@ -25,4 +24,4 @@ node_modules/
/solution/2200-2299/2252.Dynamic Pivoting of a Table/Solution.sql
/solution/2200-2299/2253.Dynamic Unpivoting of a Table/Solution.sql
/solution/3100-3199/3150.Invalid Tweets II/Solution.sql
/solution/3100-3199/3198.Find Cities in Each State/Solution.sql
/solution/3100-3199/3198.Find Cities in Each State/Solution.sql
44 changes: 31 additions & 13 deletions solution/1400-1499/1454.Active Users/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,26 +104,44 @@ id = 7 的用户 Jonathon 在不同的 6 天内登录了 7 次, , 6 天中有 5

<!-- solution:start -->

### 方法一
### 方法一: 使用窗口函数

我们先将 `Logins` 表和 `Accounts` 表连接起来,并且去重,得到临时表 `T`。

然后我们使用窗口函数 `ROW_NUMBER()`,计算出每个用户 `id` 的登录日期的基准日期 `g`,如果用户连续登录 5 天,那么他们的 `g` 值是相同的。

最后,我们按照 `id` 和 `g` 进行分组,统计每个用户的登录次数,如果登录次数大于等于 5,那么这个用户就是活跃用户。

<!-- tabs:start -->

#### MySQL

```sql
# Write your MySQL query statement below
WITH t AS
(SELECT *,
SUM(id) over(partition by id
ORDER BY login_date range interval 4 day preceding)/id cnt
FROM
(SELECT DISTINCT *
FROM Accounts
JOIN Logins using(id) ) tt )
SELECT DISTINCT id,
name
FROM t
WHERE cnt=5;
WITH
T AS (
SELECT DISTINCT *
FROM
Logins
JOIN Accounts USING (id)
),
P AS (
SELECT
*,
DATE_SUB(
login_date,
INTERVAL ROW_NUMBER() OVER (
PARTITION BY id
ORDER BY login_date
) DAY
) g
FROM T
)
SELECT DISTINCT id, name
FROM P
GROUP BY id, g
HAVING COUNT(*) >= 5
ORDER BY 1;
```

<!-- tabs:end -->
Expand Down
50 changes: 34 additions & 16 deletions solution/1400-1499/1454.Active Users/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ This table contains the account id of the user who logged in and the login date.
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong>
<strong>Input:</strong>
Accounts table:
+----+----------+
| id | name |
Expand All @@ -80,13 +80,13 @@ Logins table:
| 1 | 2020-06-07 |
| 7 | 2020-06-10 |
+----+------------+
<strong>Output:</strong>
<strong>Output:</strong>
+----+----------+
| id | name |
+----+----------+
| 7 | Jonathan |
+----+----------+
<strong>Explanation:</strong>
<strong>Explanation:</strong>
User Winston with id = 1 logged in 2 times only in 2 different days, so, Winston is not an active user.
User Jonathan with id = 7 logged in 7 times in 6 different days, five of them were consecutive days, so, Jonathan is an active user.
</pre>
Expand All @@ -100,26 +100,44 @@ User Jonathan with id = 7 logged in 7 times in 6 different days, five of them we

<!-- solution:start -->

### Solution 1
### Solution 1: Using Window Functions

First, we join the `Logins` table and the `Accounts` table, and remove duplicates to get the temporary table `T`.

Then, we use the window function `ROW_NUMBER()` to calculate the base login date `g` for each user `id`. If a user logs in for 5 consecutive days, their `g` values are the same.

Finally, we group by `id` and `g` to count the number of logins for each user. If the number of logins is greater than or equal to 5, then the user is considered active.

<!-- tabs:start -->

#### MySQL

```sql
# Write your MySQL query statement below
WITH t AS
(SELECT *,
SUM(id) over(partition by id
ORDER BY login_date range interval 4 day preceding)/id cnt
FROM
(SELECT DISTINCT *
FROM Accounts
JOIN Logins using(id) ) tt )
SELECT DISTINCT id,
name
FROM t
WHERE cnt=5;
WITH
T AS (
SELECT DISTINCT *
FROM
Logins
JOIN Accounts USING (id)
),
P AS (
SELECT
*,
DATE_SUB(
login_date,
INTERVAL ROW_NUMBER() OVER (
PARTITION BY id
ORDER BY login_date
) DAY
) g
FROM T
)
SELECT DISTINCT id, name
FROM P
GROUP BY id, g
HAVING COUNT(*) >= 5
ORDER BY 1;
```

<!-- tabs:end -->
Expand Down
36 changes: 24 additions & 12 deletions solution/1400-1499/1454.Active Users/Solution.sql
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
# Write your MySQL query statement below
WITH t AS
(SELECT *,
SUM(id) over(partition by id
ORDER BY login_date range interval 4 day preceding)/id cnt
FROM
(SELECT DISTINCT *
FROM Accounts
JOIN Logins using(id) ) tt )
SELECT DISTINCT id,
name
FROM t
WHERE cnt=5;
WITH
T AS (
SELECT DISTINCT *
FROM
Logins
JOIN Accounts USING (id)
),
P AS (
SELECT
*,
DATE_SUB(
login_date,
INTERVAL ROW_NUMBER() OVER (
PARTITION BY id
ORDER BY login_date
) DAY
) g
FROM T
)
SELECT DISTINCT id, name
FROM P
GROUP BY id, g
HAVING COUNT(*) >= 5
ORDER BY 1;
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ tags:

### 方法一:字符串分割

将 $sentence$ 按空格分割为 $words$,然后遍历 $words$,检查 $words[i]$ 是否是 $searchWord$ 的前缀,是则返回 $i+1$。若遍历结束,所有单词都不满足,返回 $-1$。
我们将 $\textit{sentence}$ 按空格分割为 $\textit{words}$,然后遍历 $\textit{words}$,检查 $\textit{words}[i]$ 是否是 $\textit{searchWord}$ 的前缀,是则返回 $i+1$。若遍历结束,所有单词都不满足,返回 $-1$。

时间复杂度 $O(mn)$。其中 $m$ 是 $sentence$ 的长度,而 $n$ 是 $searchWord$ 的长度。
时间复杂度 $O(m \times n)$,空间复杂度 $O(m)$。其中 $m$ 和 $n$ 分别是 $\textit{sentence}$ 和 $\textit{searchWord}$ 的长度。

<!-- tabs:start -->

Expand Down Expand Up @@ -176,9 +176,9 @@ class Solution {
* @return Integer
*/
function isPrefixOfWord($sentence, $searchWord) {
$arr = explode(' ', $sentence);
for ($i = 0; $i < count($arr); $i++) {
if (strpos($arr[$i], $searchWord) === 0) {
$words = explode(' ', $sentence);
for ($i = 0; $i < count($words); ++$i) {
if (strpos($words[$i], $searchWord) === 0) {
return $i + 1;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: String Splitting

We split $\textit{sentence}$ by spaces into $\textit{words}$, then iterate through $\textit{words}$ to check if $\textit{words}[i]$ is a prefix of $\textit{searchWord}$. If it is, we return $i+1$. If the iteration completes and no words satisfy the condition, we return $-1$.

The time complexity is $O(m \times n)$, and the space complexity is $O(m)$. Here, $m$ and $n$ are the lengths of $\textit{sentence}$ and $\textit{searchWord}$, respectively.

<!-- tabs:start -->

Expand Down Expand Up @@ -170,9 +174,9 @@ class Solution {
* @return Integer
*/
function isPrefixOfWord($sentence, $searchWord) {
$arr = explode(' ', $sentence);
for ($i = 0; $i < count($arr); $i++) {
if (strpos($arr[$i], $searchWord) === 0) {
$words = explode(' ', $sentence);
for ($i = 0; $i < count($words); ++$i) {
if (strpos($words[$i], $searchWord) === 0) {
return $i + 1;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ class Solution {
* @return Integer
*/
function isPrefixOfWord($sentence, $searchWord) {
$arr = explode(' ', $sentence);
for ($i = 0; $i < count($arr); $i++) {
if (strpos($arr[$i], $searchWord) === 0) {
$words = explode(' ', $sentence);
for ($i = 0; $i < count($words); ++$i) {
if (strpos($words[$i], $searchWord) === 0) {
return $i + 1;
}
}
Expand Down
Loading