Java/506. Relative Ranks 相对名次

本文介绍了一种用于比赛排名的算法实现,通过使用HashMap和数组排序,可以高效地为参赛者分配奖牌和名次。该算法首先将参赛者的分数与他们的位置关联起来,然后根据分数进行排序,最后为前三名分配金、银、铜牌,并为其余参赛者分配名次。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目


 

 

代码部分(22ms 55.88%)

class Solution {
    public String[] findRelativeRanks(int[] nums) {
        if(nums.length == 0) return new String[0];
        if(nums.length == 1) {
            String[] s = new String[1];
            s[0] = "Gold Medal";
            return s;
        }
        if(nums.length == 2) {
            String[] s = new String[2];
            if(nums[0] > nums[1]){
                s[0] = "Gold Medal";
                s[1] = "Silver Medal";
            }else{
                s[1] = "Gold Medal";
                s[0] = "Silver Medal";
            }
            return s;
        }
        
        int len = nums.length;
        String [] res  = new String[len];
        HashMap<Integer,Integer> map = new HashMap();
        
        for(int i = 0; i < len; i++){
            map.put(nums[i], i);
        }
        
        Arrays.sort(nums);
        
        int j = len - 3;
        int first = map.get(nums[j + 2]);
        int second = map.get(nums[j + 1]);
        int third = map.get(nums[j]); 
        res[first] = "Gold Medal";
        res[second] = "Silver Medal";
        res[third] = "Bronze Medal";
        
        int n = 4;
        for(int i = j - 1; i >= 0; i--){
            int index = map.get(nums[i]);
            res[index] = "" + (n++);
        }
        
        return res;
    }
}

 

def simple_ranking(addnum, linknum, multi): with open('input/MainInput.json', 'r') as f: input = json.loads(f.read()) f.close() weightmethod = input['weight']; rankmethod = input['rank'] jounal1_path = 'result/stat_all.csv'; jounal2_path = 'result/perform_log.csv' cmethod=input['rw_ws'] if cmethod==1: os.system('python3 ./lib/cal_rw_ws.py') else: while True: #print('expanding...\n') p = os.system('/usr/bin/time -f"%e %M" -o ./result/tmp/time.log python3 ./lib/expand.py {} {} {} {} {} {}'.format(addnum, linknum, multi, 'input/input.json', 'result/tmp/topo', 'result/graph_stat')) #print(p) if p!=0: break #print('begin caculating weights...\n') weight_input = input['weight_input'] p = os.system('/usr/bin/time -f"%e %M" -a -o ./result/tmp/time.log python3 ./lib/Weight/{}/{}.py {} {}'.format(weightmethod , weightmethod , 'input/'+weightmethod+'_input/'+weight_input, 'result/weight.json')) if p!=0: break #print('begin caculating metrics...\n') p = os.system('/usr/bin/time -f"%e %M" -a -o ./result/tmp/time.log python3 ./lib/cal_metrics.py {} {}'.format('result/tmp/topo.json', 'result/path_metrics.csv')) if p!=0: break #print('begin caculating ranks...\n') os.system('/usr/bin/time -f"%e %M" -a -o ./result/tmp/time.log python3 ./lib/Rank/{}/{}.py {} {} {} {} {}'.format(rankmethod , rankmethod , 'result/tmp/topo.json', 'result/weight.json', 'result/tmp/data.csv', 'result/rank.json', 'result/path_metrics.csv')) break def main(addnum, linknum, multi,method): with open('input/MainInput.json', 'r') as f: input = json.loads(f.read()) f.close() weightmethod = input['weight']; rankmethod = input['rank'] jounal1_path = 'result/stat_all.csv'; jounal2_path = 'result/perform_log.csv' while True: #print('expanding...\n') p = os.system('/usr/bin/time -f"%e %M" -o ./result/tmp/time.log python3 ./lib/expand.py {} {} {} {} {} {}'.format(addnum, linknum, multi, 'input/input.json', 'result/tmp/topo', 'result/graph_stat')) #print(p) if p!=0: break #print('begin caculating weights...\n') weight_input = input['weight_input'] p = os.system('/usr/bin/time -f"%e %M" -a -o ./result/tmp/time.log python3 ./lib/Weight/{}/{}.py {} {}'.format(weightmethod , weightmethod , 'input/'+weightmethod+'_input/'+weight_input, 'result/weight.json')) if p!=0: break #print('begin caculating metrics...\n') p = os.system('/usr/bin/time -f"%e %M" -a -o ./result/tmp/time.log python3 ./lib/cal_metrics.py {} {}'.format('result/tmp/topo.json', 'result/path_metrics.csv')) if p!=0: break #print('begin caculating ranks...\n') os.system('/usr/bin/time -f"%e %M" -a -o ./result/tmp/time.log python3 ./lib/Rank/{}/{}.py {} {} {} {} {}'.format(rankmethod , rankmethod , 'result/tmp/topo.json', 'result/weight.json', 'result/tmp/data.csv', 'result/rank.json', 'result/path_metrics.csv')) if p!=0: break if method=='s': os.system('/usr/bin/time -f"%e %M" -o ./result/tmp/risk_time.log python3 ./lib/RRisk/static.py {} {}'.format('result/tmp/topo.json', 'result/static_risk.json')) elif method=='d': os.system('/usr/bin/time -f"%e %M" -o ./result/tmp/risk_time.log python3 ./lib/RRisk/dynamic.py {} {}'.format('result/tmp/topo.json', 'result/dynamic_risk.json')) break if __name__ == '__main__': addnum = int(sys.argv[1]); linknum = int(sys.argv[2]); multi = int(sys.argv[3]) if len(sys.argv)==5: method=sys.argv[4] main(addnum, linknum, multi,method) print('finish!') save_risk(addnum, linknum, multi) sys.exit() else: simple_ranking(addnum, linknum, multi) save(addnum, linknum, multi) sys.exit() 接续上免得代码继续完成分析
08-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值