On the journey of
[Leetcode] 1934.Confirmation Rate, 1193.Monthly Transactions 1, 1729.Find Followers Count 본문
[Leetcode] 1934.Confirmation Rate, 1193.Monthly Transactions 1, 1729.Find Followers Count
dlrpskdi 2023. 9. 30. 07:21지난 주 일요일은 여러 일정이 겹치면서 한 주 쉬어가는 세션이 되었었는데.... 놀랍게도 벌써 8주차란다. 진짜 언비리버블..
각설하고 시작
1) 1934. https://leetcode.com/problems/confirmation-rate/
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
Q.
The confirmation rate of a user is the number of 'confirmed' messages divided by the total number of requested confirmation messages. The confirmation rate of a user that did not request any confirmation messages is 0. Round the confirmation rate to two decimal places.
Write a solution to find the confirmation rate of each user.
Return the result table in any order.
The result format is in the following example.
그러니까 요청을 하지 않거나(no request), 요청이 다 만료되어 제껴지거나(not confirmed anyway) 하면 0, 그 외 confirmed면 1이라는 거고 두 개가 섞여 있다면 비율을 confirmed / request로 계산하면 된다는 것. 데이터프레임 join만 시켜주면 그리 어렵지는 않았다 :-)
# Write your MySQL query statement below
select
user_id,
round(avg(if(action = 'confirmed', 1, 0)), 2) as confirmation_rate
from Signups left join Confirmations using (user_id)
group by 1
order by 1
2) 1193. https://leetcode.com/problems/monthly-transactions-i/
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
Write an SQL query to find for each month and country, the number of transactions and their total amount, the number of approved transactions and their total amount.
Return the result table in any order.
The query result format is in the following example.
우선 example를 가져오기는 했는데, 답을 any order로 쓰라고는 하지만 each month and country가 반드시 있어야 한다는 건 알 수 있다.
SELECT Date_format(trans_date, '%Y-%m') AS month,
country,
Count(id) AS trans_count,
Count(IF(state = 'approved', 1, NULL)) AS approved_count,
SUM(amount) AS trans_total_amount,
SUM(IF(state = 'approved', amount, 0)) AS approved_total_amount
FROM transactions
GROUP BY Date_format(trans_date, '%Y-%m'),
country
ORDER BY NULL
시간복잡도 기준 O(n)이 되는 알고리즘으로, Date format 이외 포인트가 있다면 Order by NULL이 아닐까 싶다. null을 기준으로 정렬하는 게 아니고, 기본적인 null의 위치를 정해주는 알고리즘으로 오름차순이면 NULL이 맨 마지막, 내림차순이면 NULL이 맨 위에 오게 되는 정렬방식이다. 또한 여기서 사용하진 않았지만, 오름차순이어도 null값이 맨 위에 왔으면 좋겠다던가, 반대로 내림차순임에도 null이 맨 아래였으면 좋겠다 할 수도 있다! 이때는 NULLS First / Last 를 사용하면 됨.
그냥 RUN만 했는데 Result에 실제 transactions, 그리고 expected result도 나오는 건 쫌 신기했다
3) 1729. https://leetcode.com/problems/find-followers-count/
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
Q.
Write a solution that will, for each user, return the number of followers.
Return the result table ordered by user_id in ascending order.
The result format is in the following example.
user/follower id라는 고유 아이디 2개로 followers를 구분하겠다는 문제. follower_id가 중복 없다는 전제 하에, user_id에 수반되는 팔로워 아이디 수를 세면 된다. 즉 Count(follower_id)만 잘 써줬다면 (followers_count로다가..) 좀 쉬웠을 문제.
# Time: O(nlogn)
# Space: O(n)
###################################################
SELECT user_id,
Count(follower_id) AS followers_count
FROM followers
GROUP BY user_id
ORDER BY user_id;
깔쌈하게 끝 :-0
(누가 올지는 모르겠지만 (?)) 즐거운 추석 보내세요 모두 ! ლ(╹◡╹ლ)