You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: solution/1400-1499/1454.Active Users/README_EN.md
+34-16
Original file line number
Diff line number
Diff line change
@@ -58,7 +58,7 @@ This table contains the account id of the user who logged in and the login date.
58
58
<p><strongclass="example">Example 1:</strong></p>
59
59
60
60
<pre>
61
-
<strong>Input:</strong>
61
+
<strong>Input:</strong>
62
62
Accounts table:
63
63
+----+----------+
64
64
| id | name |
@@ -80,13 +80,13 @@ Logins table:
80
80
| 1 | 2020-06-07 |
81
81
| 7 | 2020-06-10 |
82
82
+----+------------+
83
-
<strong>Output:</strong>
83
+
<strong>Output:</strong>
84
84
+----+----------+
85
85
| id | name |
86
86
+----+----------+
87
87
| 7 | Jonathan |
88
88
+----+----------+
89
-
<strong>Explanation:</strong>
89
+
<strong>Explanation:</strong>
90
90
User Winston with id = 1 logged in 2 times only in 2 different days, so, Winston is not an active user.
91
91
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.
92
92
</pre>
@@ -100,26 +100,44 @@ User Jonathan with id = 7 logged in 7 times in 6 different days, five of them we
100
100
101
101
<!-- solution:start -->
102
102
103
-
### Solution 1
103
+
### Solution 1: Using Window Functions
104
+
105
+
First, we join the `Logins` table and the `Accounts` table, and remove duplicates to get the temporary table `T`.
106
+
107
+
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.
108
+
109
+
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.
104
110
105
111
<!-- tabs:start -->
106
112
107
113
#### MySQL
108
114
109
115
```sql
110
116
# Write your MySQL query statement below
111
-
WITH t AS
112
-
(SELECT*,
113
-
SUM(id) over(partition by id
114
-
ORDER BY login_date range interval 4 day preceding)/id cnt
Copy file name to clipboardExpand all lines: solution/1400-1499/1455.Check If a Word Occurs As a Prefix of Any Word in a Sentence/README_EN.md
+8-4
Original file line number
Diff line number
Diff line change
@@ -67,7 +67,11 @@ tags:
67
67
68
68
<!-- solution:start -->
69
69
70
-
### Solution 1
70
+
### Solution 1: String Splitting
71
+
72
+
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$.
73
+
74
+
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.
0 commit comments