返回介绍

solution / 1900-1999 / 1919.Leetcodify Similar Friends / README_EN

发布于 2024-06-17 01:03:12 字数 3711 浏览 0 评论 0 收藏 0

1919. Leetcodify Similar Friends

中文文档

Description

Table: Listens

+-------------+---------+
| Column Name | Type  |
+-------------+---------+
| user_id   | int   |
| song_id   | int   |
| day     | date  |
+-------------+---------+
This table may contain duplicate rows.
Each row of this table indicates that the user user_id listened to the song song_id on the day day.

 

Table: Friendship

+---------------+---------+
| Column Name   | Type  |
+---------------+---------+
| user1_id    | int   |
| user2_id    | int   |
+---------------+---------+
(user1_id, user2_id) is the primary key (combination of columns with unique values) for this table.
Each row of this table indicates that the users user1_id and user2_id are friends.
Note that user1_id < user2_id.

 

Write a solution to report the similar friends of Leetcodify users. A user x and user y are similar friends if:

  • Users x and y are friends, and
  • Users x and y listened to the same three or more different songs on the same day.

Return the result table in any order. Note that you must return the similar pairs of friends the same way they were represented in the input (i.e., always user1_id < user2_id).

The result format is in the following example.

 

Example 1:

Input: 
Listens table:
+---------+---------+------------+
| user_id | song_id | day    |
+---------+---------+------------+
| 1     | 10    | 2021-03-15 |
| 1     | 11    | 2021-03-15 |
| 1     | 12    | 2021-03-15 |
| 2     | 10    | 2021-03-15 |
| 2     | 11    | 2021-03-15 |
| 2     | 12    | 2021-03-15 |
| 3     | 10    | 2021-03-15 |
| 3     | 11    | 2021-03-15 |
| 3     | 12    | 2021-03-15 |
| 4     | 10    | 2021-03-15 |
| 4     | 11    | 2021-03-15 |
| 4     | 13    | 2021-03-15 |
| 5     | 10    | 2021-03-16 |
| 5     | 11    | 2021-03-16 |
| 5     | 12    | 2021-03-16 |
+---------+---------+------------+
Friendship table:
+----------+----------+
| user1_id | user2_id |
+----------+----------+
| 1    | 2    |
| 2    | 4    |
| 2    | 5    |
+----------+----------+
Output: 
+----------+----------+
| user1_id | user2_id |
+----------+----------+
| 1    | 2    |
+----------+----------+
Explanation: 
Users 1 and 2 are friends, and they listened to songs 10, 11, and 12 on the same day. They are similar friends.
Users 1 and 3 listened to songs 10, 11, and 12 on the same day, but they are not friends.
Users 2 and 4 are friends, but they did not listen to the same three different songs.
Users 2 and 5 are friends and listened to songs 10, 11, and 12, but they did not listen to them on the same day.

Solutions

Solution 1

# Write your MySQL query statement below
SELECT DISTINCT user1_id, user2_id
FROM
  Friendship AS f
  LEFT JOIN Listens AS l1 ON user1_id = l1.user_id
  LEFT JOIN Listens AS l2 ON user2_id = l2.user_id
WHERE l1.song_id = l2.song_id AND l1.day = l2.day
GROUP BY 1, 2, l1.day
HAVING COUNT(DISTINCT l1.song_id) >= 3;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文