LeetCode | 506. Relative Ranks

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”.

Example 1:
Input: [5, 4, 3, 2, 1]
Output: [“Gold Medal”, “Silver Medal”, “Bronze Medal”, “4”, “5”]
Explanation: The first three athletes got the top three highest scores, so they got “Gold Medal”, “Silver Medal” and “Bronze Medal”.
For the left two athletes, you just need to output their relative ranks according to their scores.
Note:
N is a positive integer and won’t exceed 10,000.
All the scores of athletes are guaranteed to be unique.
题目链接

最愚蠢的方法,用一个保存排序后的数组,再和原数组两两比较找出对应位置,容易想到时间复杂度为 O(n2)

public class Solution {
    public String[] findRelativeRanks(int[] nums) {
        String[] ranks = new String[nums.length];
        int[] sorted = new int[nums.length];
        for(int i = 0; i < nums.length; i++) {
            sorted[i] = nums[i];
        }

        Arrays.sort(sorted);
        for(int i = 0; i < nums.length; i++) {
            for(int j = 0; j < nums.length; j++) {
                if(sorted[i] == nums[j]) {
                    int temp = nums.length - i;
                    if(temp == 1) ranks[j] = "Gold Medal";
                    else if(temp == 2) ranks[j] = "Silver Medal";
                    else if(temp == 3) ranks[j] = "Bronze Medal";
                    else ranks[j] = "" + (temp);
                }
            }
        }

        return ranks;
    }
}

可以用pair的思路解决,即创建一个同等大小的数组保存数据原有的位置,排序时使数据与位置一起移动,这样既能实现保存原有数据顺序,又能直接读位置。另外,上面的解法中直接使用了API里的Arrays.sort方法,尽管内部是用快排实现,但是由于API中的快排是从小到大排序,所以还要经过逆序的步骤,故我们需要自己实现一个快排算法,使其对数据数组进行排序移动的同时,也使位置数组移动,并实现从大到小的排序,时间复杂度为 O(n)

public class Solution {
    public String[] findRelativeRanks(int[] nums) {
        int[] locs = new int[nums.length];
        String[] rank = new String[nums.length];
        for(int i = 0; i < nums.length; i++) {
            locs[i] = i;
        }
        quickSort(nums, locs, 0, nums.length - 1);
        for(int i = 0; i < nums.length; i++) {
            rank[locs[i]] = 1 + i + "";
        }

        if(nums.length == 1) {
            rank[locs[0]] = "Gold Medal";
        } else if(nums.length == 2) {
            rank[locs[0]] = "Gold Medal";
            rank[locs[1]] = "Silver Medal";
        } else {
            rank[locs[0]] = "Gold Medal";
            rank[locs[1]] = "Silver Medal";
            rank[locs[2]] = "Bronze Medal";
        }
        return rank;
    }

    private void quickSort(int[] nums, int[] locs, int start, int end) {
        if(start < end) {
            int middle = partition(nums, locs, start, end);
            quickSort(nums, locs, start, middle);
            quickSort(nums, locs, middle + 1, end);
        }
    }

    private int partition(int[] nums, int[] locs, int start, int end) {
        int tempNum = nums[start];
        int tempLoc = locs[start];
        while(start < end) {
            while(start < end && nums[end] < tempNum) {
                end--;
            }
            nums[start] = nums[end];
            locs[start] = locs[end];
            while(start < end && nums[start] > tempNum) {
                start++;
            }
            nums[end] = nums[start];
            locs[end] = locs[start];
        }
        nums[start] = tempNum;
        locs[start] = tempLoc;
        return end;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值