๐ข ๋ณธ ํฌ์คํ ์ ํ์ฉ๋๋ ๊ธฐ๋ณธ ๋ฌธ์ ๋ฐ ์๋ฃ ์ถ์ฒ๋
๋ฆฌํธ์ฝ๋ Problems, https://leetcode.com/problemset/all/์์ ๋ฐํ๋๋ค.
โ๋ฌธ์
https://leetcode.com/problems/movie-rating/
LeetCode - The World's Leading Online Programming Learning Platform
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
Table : Movies
Table : Users
Table : MovieRating
Write a solution to:
- Find the name of the user who has rated the greatest number of movies. In case of a tie, return the lexicographically smaller user name.
- Find the movie name with the highest average rating in February 2020. In case of a tie, return the lexicographically smaller movie name.
โ ํ์ด
๋ฌธ์ ์๊ตฌ์ฌํญ
๊ฐ ํ ์ด๋ธ ์ ๋ณด๋ฅผ ์ข ํฉํ์ฌ
๊ฐ์ฅ ๋ง์ ์ํ๋ฅผ ํ๊ฐํ ์ ์ ์ ์ด๋ฆ (=A) ๊ณผ
2020๋ 2์์ ๋ฌ๋ฆฐ ๋ฆฌ๋ทฐ์ค ํ๊ท ํ์ ์ด ๊ฐ์ฅ ๋์ ์ํ์ ์ด๋ฆ (=B) ์
results ๋ผ๋ ํ ์ปฌ๋ผ์ ์ถ๋ ฅํ๋ ๋ ๊ฐ ๋ชจ๋ ๋์ผํ ์์น์ ์ค๋ณต๊ฐ์ด ์์ ๊ฒฝ์ฐ ์ฌ์ ์(์ค๋ฆ์ฐจ์) ๊ธฐ์ค์ผ๋ก 1๊ฐ์ฉ๋ง ์ถ์ถํด์ผ ํ๋ค.
โ ๋จผ์ Movie Rating ํ ์ด๋ธ ๊ธฐ์ค์ผ๋ก Movies ์ Users ํ ์ด๋ธ ์ ๋ณด๋ฅผ left join ํด์ ๋ชจ๋ ์ํ ํ์ ๋ฐ์ดํฐ์ ๋ํด ์ํ ์ ๋ณด์ ๋ฆฌ๋ทฐ๋ฅผ ๋จ ์ ์ ์ ๋ณด๊น์ง ํฌํจ๋๋๋ก ํ ํ
โก A๊ฐ์ ๊ตฌํ๊ธฐ ์ํด
์ฌ์ฉ์ ์ด๋ฆ๊ณผ ์์ด๋๋ก ๊ทธ๋ฃนํํด์ ๊ฐ์ฅ ๋ง์ด ๋ฆฌ๋ทฐ๋ฅผ ๋ฌ๊ณ ์ด๋ฆ ์ค๋ฆ์ฐจ์์ผ๋ก ์ฒซ๋ฒ์งธ์ธ ์ฌ์ฉ์ ์ด๋ฆ ์ถ์ถ.
โข B๊ฐ์ ๊ตฌํ๊ธฐ ์ํด
์ํ ์์ด๋์ ์ ๋ชฉ์ผ๋ก ๊ทธ๋ฃนํํด์ ํ๊ท ํ์ ์ด ๊ฐ์ฅ ๋์ผ๋ฉด์ ์ด๋ฆ์ด ์ค๋ฆ์ฐจ์์ผ๋ก ์ฒซ๋ฒ์งธ์ธ ์ํ ์ด๋ฆ์ ์ถ์ถ.
(๋จ, B๊ฐ ์ ์ ์กฐ๊ฑด์ธ 2020๋ 2์ ๋ฆฌ๋ทฐ์ฌ์ผ ํ๋ฏ๋ก ๋ ์ง ํจ์๋ฅผ ์ฌ์ฉํด์ ์กฐ๊ฑด์ ์ ์ฉํด์ฃผ๊ธฐ)
โฃ ์ดํ โก + โข์ UNION ALL ์ฒ๋ฆฌํด์คฌ๋ค.
โป ์ฒ์์๋ UNION์ ์ฌ์ฉํ๋๋ ์ํ์ด๋ฆ=์ฌ์ฉ์ ์ด๋ฆ์ด ๊ฐ์ ๊ฒฝ์ฐ ๋ฐ์ดํฐ๊ฐ ์ค๋ณต ์ ๊ฑฐ๋ก 1๊ฐ๋ง ์ถ๋ ฅ๋๋ ๊ฒฝ์ฐ๊ฐ ๋ฐ์ํ๋ค.
(SELECT US.name AS results
FROM MovieRating MR
LEFT JOIN Movies MI
ON MR.movie_id = MI.movie_id
LEFT JOIN Users US
ON MR.user_id = US.user_id
GROUP BY MR.user_id, US.name
ORDER BY COUNT(*) DESC, US.name ASC
LIMIT 1)
UNION ALL
(SELECT MI.title AS results
FROM MovieRating MR
LEFT JOIN Movies MI
ON MR.movie_id = MI.movie_id
LEFT JOIN Users US
ON MR.user_id = US.user_id
WHERE DATE_FORMAT(MR.created_at, '%Y-%m') = '2020-02'
GROUP BY MR.movie_id, MI.title
ORDER BY AVG(MR.rating) DESC, MI.title ASC
LIMIT 1);
'Growth ๐ณ > Practice ๐ป' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Leet code] 1204. Last Person to Fit in the Bus (0) | 2023.09.26 |
---|---|
1907. Count Salary Categories (0) | 2023.09.24 |
[Leet code] 1193. Monthly Transactions I (0) | 2023.09.21 |
[Leet code] 1174. Immediate Food Delivery II (0) | 2023.09.15 |
[Leet code] 1141. User Activity for the Past 30 Days IEasy474634 (0) | 2023.09.13 |