Growth ๐ŸŒณ/Practice ๐Ÿ’ป

[Hacker Rank] Contest Leaderboard

์ธ” 2022. 9. 20. 20:29

๐Ÿ“ข ๋ณธ ํฌ์ŠคํŒ…์— ํ™œ์šฉ๋˜๋Š” ๊ธฐ๋ณธ ๋ฌธ์ œ ๋ฐ ์ž๋ฃŒ ์ถœ์ฒ˜๋Š” HackerRank ์ž„์„ ๋ฐํž™๋‹ˆ๋‹ค.

https://www.hackerrank.com


โ–  ๋ฌธ์ œ

You did such a great job helping Julia with her last coding contest challenge that she wants you to work on this one, too!

The total score of a hacker is the sum of their maximum scores for all of the challenges. Write a query to print the hacker_id, name, and total score of the hackers ordered by the descending score. If more than one hacker achieved the same total score, then sort the result by ascending hacker_id. Exclude all hackers with a total score of  from 0 your result.

Input Format

The following tables contain contest data:

  • Hackers: The hacker_id is the id of the hacker, and name is the name of the hacker.
     
  • Submissions: The submission_id is the id of the submission, hacker_id is the id of the hacker who made the submission, challenge_id is the id of the challenge for which the submission belongs to, and score is the score of the submission.
     

Sample Input

Hackers Table: 

Submissions Table: 

Sample Output

4071 Rose 191
74842 Lisa 174
84072 Bonnie 100
4806 Angela 89
26071 Frank 85
80305 Kimberly 67
49438 Patrick 43

Explanation


โ–  ํ’€์ด

๋ฌธ์ œ ์š”๊ตฌ์‚ฌํ•ญ

> ์ด์ (total score) = ํ•ด์ปค๋ณ„ ๊ทธ๋“ค์˜ challenges์— ๋Œ€ํ•œ ์ตœ๋Œ€ ์ ์ˆ˜์˜ ํ•ฉ

 

> ์ ์ˆ˜ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ hacker_id, name, and total score of the hackers ์ถ”์ถœ

   (๋‘๋ช… ์ด์ƒ์˜ ํ•ด์ปค๊ฐ€ ์ด์ ์ด ๋˜‘๊ฐ™๋‹ค๋ฉด, hacker_id ์ˆœ์œผ๋กœ ์ •๋ ฌ)

 

> ์ด์ ์ด 0์ธ ํ•ด์ปค๋“ค์€ ๊ฒฐ๊ณผ์—์„œ ์ œ์™ธํ•  ๊ฒƒ.


โ‘  hackers ํ…Œ์ด๋ธ”๊ณผ Submissions ํ…Œ์ด๋ธ”์„ ์กฐ์ธ

     > ๋‹จ, explanation์— ๋”ฐ๋ผ ํ•ด์ปค๋ณ„๋กœ ๋™์ผํ•œ challege์— ๋Œ€ํ•œ ์ด์ ์€ '์ตœ๊ณ ์ ' ๋งŒ ๊ณ ๋ คํ•œ๋‹ค.

โ‘ก Submissions ํ…Œ์ด๋ธ”์—์„œ hacker_id, challenge_id๋ณ„๋กœ Max(score) max_score ์„ ์ถ”์ถœ

     โ–ฒ ์ด ํ…Œ์ด๋ธ”๊ณผ Hackers ํ…Œ์ด๋ธ”์„ inner join

           ํ•ด์ปค๋ณ„ id, name, total_score( sum(s.max_sore))๋ฅผ ์ถ”์ถœํ•˜๋Š”๋ฐ

โ‘ข sum(s.max_score)๊ฐ€ 0์ธ ํ•ด์ปค๋Š” ์ œ์™ธํ•ด์•ผ ํ•˜๋ฏ€๋กœ ์ง‘๊ณ„ํ•จ์ˆ˜ ์กฐ๊ฑด์ธ HAVING์ ˆ๋กœ ์กฐ๊ฑด ๋ถ€์—ฌํ•˜๊ณ 

โ‘ฃ ์ •๋ ฌ (order by ~)

 - (1์ˆœ์œ„) total_score๊ฐ€ ๋†’์€ ์ˆœ

 - (2์ˆœ์œ„) total_score๊ฐ€ ๋™์ผํ•  ๊ฒฝ์šฐ id ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ

SELECT h.hacker_id, h.name, sum(s.max_score)
 FROM Hackers h
 JOIN (SELECT hacker_id, challenge_id, Max(score) max_score
        FROM Submissions 
        GROUP BY hacker_id, challenge_id
       ) s
 ON h.hacker_id = s.hacker_id
 GROUP BY 1,2
 HAVING SUM(s.max_score) > 0
 ORDER BY SUM(s.max_score) DESC, h.hacker_id;

 

 

'Growth ๐ŸŒณ > Practice ๐Ÿ’ป' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[LeetCode] 511. Game Play Analysis I  (0) 2022.09.28
[LeetCode] 586. Customer Placing the Largest Number of Orders  (0) 2022.09.28
[Hacker Rank] SQL Project Planning  (0) 2022.09.20
[Hacker Rank] Occupations  (0) 2022.09.20
[Hacker Rank] Draw The Triangle 2  (1) 2022.09.20