Last Round for Each Player
Learn this problemProblem statement
There are N players, numbered from 0 to N - 1, participating in a tournament. Player k has skill level skills[k]. No two players have the same skill level.
The tournament is played in rounds for as long as there are at least two players remaining. In the first round, player 0 faces player 1, player 2 faces player 3, and so on. In the second round, the winner of the match between players 0 and 1 faces the winner of the match between players 2 and 3, and so on. The player with the higher skill level wins the match.
For example, for skills = [4, 2, 7, 3, 1, 8, 6, 5], the tournament is as follows (numbers in circles are skill levels):
round 3: ⑦───final───⑧
╱ ╲ ╱ ╲
round 2: ④ ⑦ ⑧ ⑥
╱ ╲ ╱ ╲ ╱ ╲ ╱ ╲
round 1: ④ ② ⑦ ③ ① ⑧ ⑥ ⑤
──────────────────────
player: 0 1 2 3 4 5 6 7For each player, find the last round in the tournament in which they participate.
Function
solution(skills: int[]) → int[]Examples
Example 1
skills = [4, 2, 7, 3, 1, 8, 6, 5]return = [2, 1, 3, 1, 1, 3, 2, 1]Players 1, 3, 4, and 7 lose in round 1. Players 0 and 6 lose in round 2. Players 2 and 5 reach the final, so their last participation round is 3.
Example 2
skills = [9, 1]return = [1, 1]The only match is the final. Both players participate in round 1, even though player 0 wins the tournament.
Example 3
skills = [1, 4, 3, 2]return = [1, 2, 2, 1]Player 0 loses to player 1, and player 3 loses to player 2 in round 1. Players 1 and 2 meet in round 2, so both have last participation round 2.
Constraints
skills.length >= 2.- For this exercise,
skills.lengthis a power of two. - Every value in
skillsis an integer. - All values in
skillsare distinct.