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;
    }
}
發佈了84 篇原創文章 · 獲贊 0 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章