# LeetCode: Relative Ranks Solution

Sometimes, LeetCode difficulty is not reliable

## Approach: Sorting and Hash Table

(commented in implementation)

## Implementation

```.css-ds3kc{display:table-row;}.css-1t8atru{display:table-cell;opacity:0.5;padding-right:var(--chakra-space-6);-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;text-align:right;}1.css-2qghsv{display:table-cell;}var findRelativeRanks = function (score) {2  const [aBeforeB, bBeforeA] = [-1, 1] // for better readability when do sorting below3
4  const positionRankMap = {5    0: "Gold Medal",6    1: "Silver Medal",7    2: "Bronze Medal",8  }9
10  const indexRankMap = Object.fromEntries(11    score12      .map((s, i) => [s, i]) // create array of [score, index]13      .sort(([sA], [sB]) => (sA > sB ? aBeforeB : bBeforeA)) // sort the array by score, descending14      .map(([_, originalScoreIndex], index) => [15        positionRankMap[index] || String(index + 1),16        originalScoreIndex,17      ]) // create array of [rank name, index in original score]18      .map(([rank, originalScoreIndex]) => [originalScoreIndex, rank]) // to convert to object in the next step19  )20
21  return score.map((_, i) => indexRankMap[i])22}```

leetcode

sorting

hash table

## Next Post

LeetCode: Reverse Prefix of Word

Sep 12, 2021

Substring manipulating

## Previous Post

LeetCode: Maximum Average Subarray I

Sep 10, 2021

Find maximum sum of contiguous subarray with length of K

Search Posts