Why this Swift code is not efficient enough?


  • 0
    W
    class Solution {
        func findRelativeRanks(_ nums: [Int]) -> [String] {
            let sortedArray = nums.sorted(by: >);
            var trueRank = [Int: String]();
            var result = [String]();
            for item in sortedArray {
                if(sortedArray.index(of: item) == 0){
                    trueRank[item] = "Gold Medal"
                }
                else if(sortedArray.index(of: item) == 1){
                    trueRank[item] = "Silver Medal"
                }
                else if(sortedArray.index(of: item) == 2){
                    trueRank[item] = "Bronze Medal"
                }
                else{
                    trueRank[item] = String((sortedArray.index(of: item))!+1)
                }
            }
            for num in nums {
                result.append(trueRank[num]!);
            }
            return result;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.