Skip to content

Commit 22823be

Browse files
committed
feat: add solutions to lc problem: No.1142
No.1142. User Activity for the Past 30 Days II
1 parent ca31a03 commit 22823be

File tree

7 files changed

+45
-21
lines changed

7 files changed

+45
-21
lines changed

solution/1000-1099/1093.Statistics from a Large Sample/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@
8383
- 更新 $cnt = cnt + count[k]$;
8484
- 如果 $count[k] \gt count[mode]$,那么更新 $mode = k$。
8585

86-
遍历结束后,我们再根据 $cnt$ 的奇偶性来更新中位数 $median$,如果 $cnt$ 是奇数,那么中位数就是第 $\lfloor \frac{cnt}{2} \rfloor + 1$ 个数字,如果 $cnt$ 是偶数,那么中位数就是第 $\lfloor \frac{cnt}{2} \rfloor$ 和第 $\lfloor \frac{cnt}{2} \rfloor + 1$ 个数字的平均值。
86+
遍历结束后,我们再根据 $cnt$ 的奇偶性来计算中位数 $median$,如果 $cnt$ 是奇数,那么中位数就是第 $\lfloor \frac{cnt}{2} \rfloor + 1$ 个数字,如果 $cnt$ 是偶数,那么中位数就是第 $\lfloor \frac{cnt}{2} \rfloor$ 和第 $\lfloor \frac{cnt}{2} \rfloor + 1$ 个数字的平均值。
87+
88+
> 这里我们通过一个简单的辅助函数 $find(i)$ 来找到第 $i$ 个数字,具体实现可以参考下面的代码。
8789
8890
最后,我们将 $mi, mx, \frac{s}{cnt}, median, mode$ 放入答案数组中返回即可。
8991

solution/1100-1199/1141.User Activity for the Past 30 Days I/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,15 @@ Activity table:
7272

7373
```sql
7474
SELECT
75-
activity_date AS DAY,
76-
COUNT( DISTINCT user_id ) AS active_users
75+
activity_date AS day,
76+
COUNT(DISTINCT user_id) AS active_users
7777
FROM
78-
Activity
78+
Activity
7979
WHERE
80-
DATEDIFF( '2019-07-27', activity_date ) > 0
81-
AND DATEDIFF( '2019-07-27', activity_date ) < 30
80+
DATEDIFF('2019-07-27', activity_date) >= 0 and
81+
DATEDIFF('2019-07-27', activity_date) < 30
8282
GROUP BY
83-
activity_date;
83+
activity_date;
8484
```
8585

8686
<!-- tabs:end -->

solution/1100-1199/1141.User Activity for the Past 30 Days I/README_EN.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@ Activity table:
6868

6969
```sql
7070
SELECT
71-
activity_date AS DAY,
72-
COUNT( DISTINCT user_id ) AS active_users
71+
activity_date AS day,
72+
COUNT(DISTINCT user_id) AS active_users
7373
FROM
74-
Activity
74+
Activity
7575
WHERE
76-
DATEDIFF( '2019-07-27', activity_date ) > 0
77-
AND DATEDIFF( '2019-07-27', activity_date ) < 30
76+
DATEDIFF('2019-07-27', activity_date) >= 0 and
77+
DATEDIFF('2019-07-27', activity_date) < 30
7878
GROUP BY
79-
activity_date;
79+
activity_date;
8080
```
8181

8282
<!-- tabs:end -->
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
SELECT
2-
activity_date AS DAY,
3-
COUNT( DISTINCT user_id ) AS active_users
2+
activity_date AS day,
3+
COUNT(DISTINCT user_id) AS active_users
44
FROM
5-
Activity
5+
Activity
66
WHERE
7-
DATEDIFF( '2019-07-27', activity_date ) > 0
8-
AND DATEDIFF( '2019-07-27', activity_date ) < 30
7+
DATEDIFF('2019-07-27', activity_date) >= 0 and
8+
DATEDIFF('2019-07-27', activity_date) < 30
99
GROUP BY
10-
activity_date;
10+
activity_date;

solution/1100-1199/1142.User Activity for the Past 30 Days II/README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,14 @@ Activity 表:
7171
### **SQL**
7272

7373
```sql
74-
74+
# Write your MySQL query statement below
75+
SELECT
76+
IFNULL( ROUND( COUNT( DISTINCT session_id ) / COUNT( DISTINCT user_id ), 2 ), 0 ) AS average_sessions_per_user
77+
FROM
78+
Activity
79+
WHERE
80+
DATEDIFF( '2019-07-27', activity_date ) >= 0
81+
AND DATEDIFF( '2019-07-27', activity_date ) < 30
7582
```
7683

7784
<!-- tabs:end -->

solution/1100-1199/1142.User Activity for the Past 30 Days II/README_EN.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,14 @@ Activity table:
6767
### **SQL**
6868

6969
```sql
70-
70+
# Write your MySQL query statement below
71+
SELECT
72+
IFNULL( ROUND( COUNT( DISTINCT session_id ) / COUNT( DISTINCT user_id ), 2 ), 0 ) AS average_sessions_per_user
73+
FROM
74+
Activity
75+
WHERE
76+
DATEDIFF( '2019-07-27', activity_date ) >= 0
77+
AND DATEDIFF( '2019-07-27', activity_date ) < 30
7178
```
7279

7380
<!-- tabs:end -->
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Write your MySQL query statement below
2+
SELECT
3+
IFNULL( ROUND( COUNT( DISTINCT session_id ) / COUNT( DISTINCT user_id ), 2 ), 0 ) AS average_sessions_per_user
4+
FROM
5+
Activity
6+
WHERE
7+
DATEDIFF( '2019-07-27', activity_date ) >= 0
8+
AND DATEDIFF( '2019-07-27', activity_date ) < 30

0 commit comments

Comments
 (0)