Tournament
Lv2
wordsCount: 345
readingTime: 2 mins
viewers:
Problem
In the Jay’s game tournament, N players compete in a knockout format. Each player is sequentially assigned a number from 1 to N. The matches are arranged as 1 vs 2, 3 vs 4, …, N-1 vs N. The winner of each game advances to the next round, where they receive new sequential numbers starting from 1 for the advancing players. For instance, if player 2 beats player 1, in the next round, they receive number 1, and so on until only one player remains.
Given the number of participants N
, and two specific participants A
and B
, you need to determine in which round players A
and B
will meet, assuming both players always win until they meet each other.
Constraints
N
is a natural number between 21 and 220, and it’s always a power of two, so no byes occur in the tournament.A
andB
are natural numbers less than or equal toN
(A ≠ B).
Example
N | A | B | answer |
---|---|---|---|
8 | 4 | 7 | 3 |
Explanation of Example
-
First Round:
- Player 4 competes against player 3, and player 7 competes against player 8. Assuming both win, player 4 becomes number 2 in the next round, and player 7 becomes number 4.
-
Second Round:
- Player 2 (formerly player 4) now competes against player 1, and player 4 (formerly player 7) competes against player 3. Both win again.
-
Third Round:
- Players 1 and 2 meet, where player 1 is the former player 2, and player 2 is the former player 4. Since these are the new identities of players 4 and 7, they meet in this round.
Therefore, players A (4) and B (7) meet in the third round, hence the answer is 3.
How to approach?
Code
|
|
Table of Contents
Related Posts
tuple
Problem A “tuple” is defined as an ordered collection of elements which can include duplicate values and each element is assigned an order. A tuple with n elements (a1, a2, a3, ..., an) can be expressed using the set notation in various ways, with any ordering of the subsets as:
2024-5-1
braket rotating
Problem Define a string as a “valid bracket string” if it follows the given rules:
“()”, “[]”, and “{}” are all valid bracket strings. If A is a valid bracket string, then “(A)”, “[A]”, and “{A}” are also valid bracket strings. For example, since “[]” is a valid bracket string, “([])” is also valid.
2024-4-27
Last and First
Problem Several people are playing the game of English word chain where each player takes turns saying a word that must begin with the last letter of the previous word. Here are the rules:
Players take turns in a sequence from 1 to n. After the last player, it starts again with the first player.
2024-4-23
High Score Kit: Greedy#1
Problem Given a number, your task is to remove k digits from the number in such a way that the remaining digits form the largest possible number. The number is provided as a string number and the integer k represents the number of digits to remove. Your function solution should return the resulting largest number as a string.
2024-4-11
High Score Kit: DFS&BFS#2
Problem In the ROR game, you play on a team that competes to destroy the opposing team’s base first, making it advantageous to reach the enemy base as quickly as possible. Suppose you’re a team member and are located at position (row: 1, column: 1) on a 5x5 map, while the enemy base is at (row: 5, column: 5).
2024-4-11
High Score Kit: DFS&BFS#1
Problem You are given an array of non-negative integers and a target number. Your task is to find how many ways you can add and subtract the given numbers to achieve the target number. The order of numbers in the array must be preserved. For example, with the array [1, 1, 1, 1, 1], there are five ways to reach the target number 3:
2024-4-11
Sponsor
Wechat
Alipay