题目
506. 相对名次
解法
class Solution {
/**
* @param Integer[] $score
* @return String[]
*/
function findRelativeRanks($score) {
if (empty($score)) {
return [];
}
$scoreCopy = $score;
rsort($scoreCopy);
$scoreCopyMap = array_flip($scoreCopy);
$ret = [];
foreach ($score as $scoreItem) {
if ($scoreCopyMap[$scoreItem] == 0) {
$ret[] = 'Gold Medal';
} elseif ($scoreCopyMap[$scoreItem] == 1) {
$ret[] = 'Silver Medal';
} elseif ($scoreCopyMap[$scoreItem] == 2) {
$ret[] = 'Bronze Medal';
} else {
$ret[] = strval($scoreCopyMap[$scoreItem] + 1);
}
}
return $ret;
}
}