You are viewing a single comment's thread. Return to all comments →
Here is the code to solve it in Swift First removing the dublicates then using binary search to determine the rank
func climbingLeaderboard(ranked: [Int], player: [Int]) -> [Int] { var result: [Int] = [] var clearRanked: [Int] = [ranked [0]] var total = 1 for rank in ranked { if rank == clearRanked.last! { continue } else { clearRanked.append(rank) total += 1 } } for playerTrial in player { if playerTrial >= clearRanked [0] { result.append(1) } else if playerTrial == clearRanked.last! { result.append(total) } else if playerTrial < clearRanked.last! { result.append(total + 1) } else { func findTheRankIndex(arr: [Int], rank: Int, range: Range<Int>) -> Int { if range.lowerBound > range.upperBound { return range.lowerBound } else { let midIndex = range.lowerBound + (range.upperBound - range.lowerBound) / 2 if rank == arr [midIndex] { return midIndex + 1 } else if rank < arr [midIndex] { if midIndex + 1 < total - 1 && rank > arr [midIndex + 1] { return midIndex + 2 } else { return findTheRankIndex(arr: arr, rank: rank, range: midIndex + 1 ..< range.upperBound) } } else //if rank > arr [midIndex] { if midIndex - 1 > 0 && rank < arr [midIndex - 1] { return midIndex + 1 } else { return findTheRankIndex(arr: arr, rank: rank, range: range.lowerBound ..< midIndex) } } } } result.append(findTheRankIndex(arr: clearRanked, rank: playerTrial, range: 0 ..< total)) } } return result }
Seems like cookies are disabled on this browser, please enable them to open this website
Climbing the Leaderboard
You are viewing a single comment's thread. Return to all comments →
Here is the code to solve it in Swift First removing the dublicates then using binary search to determine the rank