복's

[ LeetCode - 178 ] Rank Scores 본문

알고리즘/LeetCode

[ LeetCode - 178 ] Rank Scores

나복이 2023. 11. 20. 22:33
728x90

https://leetcode.com/problems/rank-scores/description/

 

Rank Scores - LeetCode

Can you solve this real interview question? Rank Scores - Table: Scores +-------------+---------+ | Column Name | Type | +-------------+---------+ | id | int | | score | decimal | +-------------+---------+ id is the primary key (column with unique values)

leetcode.com

음... 진짜 rank 함수만 쓰면 바로 넘어갈 수 있는 문제였다.

습관적으로 맨 처음에는 서브 쿼리를 썼지만 사실 랭킹이랑 점수만 출력하면 되는거지 조건이 따로 필요 없어서 그냥 select 해주기만 하면 된다.


[ 📌 풀이 ]

풀이랄것도 없지만 딱 하나 공동 랭킹을 허용 하면서 공동 랭킹시 다음 랭크는 공통 랭킹 다음 랭킹으로 변하는것만 신경쓰면 된다.

ex)
4.0 -> 1등    4.0 -> 1등
4.0 -> 1등    4.0 -> 1등
3.5 -> 2등    3.5 -> 3등

랭크 함수 구분만 해주면 될 듯


[ 📌 코드 - Oracle ]

-- Author    : Lee In Bok 
-- Date      : 2023.11.20(Mon)
-- Spend Time: 04m 23s
-- Runtime   : 1075 ms (Beats 12.39%)
-- Algoritm  : rank

select score
     , dense_rank() over(order by score desc) as rank
  from Scores
;

[ 📌 결과 - Oracle ]

[ Result - Oracle ]

728x90