Relative Ranks

来源:互联网 发布:移动2g网络可以上网吗 编辑:程序博客网 时间:2024/06/11 18:05

Given scores of N athletes, find their relative ranks and the people with the top three highest scores, who will be awarded medals: “Gold Medal”, “Silver Medal” and “Bronze Medal”.

var findRelativeRanks = function(nums) {    var res = [];    var tmp = [];    var obj = {};    tmp = nums.slice();    tmp.sort(function(a,b) {return b - a})    obj[tmp[0]]  = 'Gold Medal';    obj[tmp[1]] = 'Silver Medal';    obj[tmp[2]] = 'Bronze Medal';    for(var m = 3; m < tmp.length; m++) {        obj[tmp[m]] = m + 1 + ''    }    for(var j = 0; j < nums.length; j++) {        res.push(obj[nums[j]])    }    return res};
0 0
原创粉丝点击