티스토리 뷰

1. 문제

https://www.hackerrank.com/challenges/full-score/problem?isFullScreen=true&h_r=next-challenge&h_v=zen&h_r=next-challenge&h_v=zen 

 

Top Competitors | HackerRank

Query a list of top-scoring hackers.

www.hackerrank.com

 

2. 풀이

  • 1번 넘게 만점 받은 학생들의 수를 구하고, 그 결과에 맞는 아이디와 이름을 출력하는 방식으로 풀이했다.
    • challenges, submissions, difficulty을 inner join 한 후
    • 만점인 학생들을 구하고,
    • hacker_id별로 그룹화 하고 hacker_id별로 1번 넘는 경우를 구한다.
  • 이렇게 풀었긴 하지만...
  • hackers, challenges, submissions, difficulty 테이블을 모두 inner join 한 후 조건을 적용하는 방식이 더 간단하다.

 

3. 코드

-- 내 풀이
select h.hacker_id, h.name
from (
    select s.hacker_id, count(*) as allCnt
    from submissions s 
    inner join challenges c on s.challenge_id = c.challenge_id 
    inner join difficulty d on c.difficulty_level=d.difficulty_level
    and s.score = d.score
    group by s.hacker_id
    having count(*) > 1) t
inner join hackers h on t.hacker_id=h.hacker_id
order by allCnt desc, t.hacker_id asc;

-- 더 간단한 풀이
select h.hacker_id,h.name
from hackers h, challenges c, difficulty d, submissions s 
where h.hacker_id = s.hacker_id
and c.challenge_id = s.challenge_id
and c.difficulty_level = d.difficulty_level
and s.score=d.score
group by h.hacker_id, h.name 
having count(h.hacker_id)>1
order by count(c.challenge_id) desc, h.hacker_id
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/10   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
글 보관함