1900. The Earliest and Latest Rounds Where Players Compete #1918
-
Topics: There is a tournament where The tournament consists of multiple rounds (starting from round number
After each round is over, the winners are lined back up in the row based on the original ordering assigned to them initially (ascending order). The players numbered Given the integers Example 1:
Example 2:
Constraints:
Hint:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
We need to determine the earliest and latest rounds in which two given players (firstPlayer and secondPlayer) will compete against each other in a tournament. The tournament involves multiple rounds where players are paired in a specific manner, and the outcomes of matches not involving the two best players can be chosen arbitrarily to either expedite or delay their meeting. Approach
Let's implement this solution in PHP: 1900. The Earliest and Latest Rounds Where Players Compete <?php
/**
* @param Integer $n
* @param Integer $firstPlayer
* @param Integer $secondPlayer
* @return Integer[]
*/
function earliestAndLatest($n, $firstPlayer, $secondPlayer) {
$max_round = 6;
$total_masks = 1 << $n;
$int_len = (int)ceil($total_masks / 32);
$visited = [];
for ($r = 1; $r <= $max_round; $r++) {
$visited[$r] = array_fill(0, $int_len, 0);
}
$ans_earliest = PHP_INT_MAX;
$ans_latest = -1;
$current_states = [(1 << $n) - 1];
setVisited(1, (1 << $n) - 1, $visited, $int_len);
for ($round = 1; $round <= $max_round; $round++) {
$next_states = [];
foreach ($current_states as $mask) {
$players = [];
for ($i = 0; $i < $n; $i++) {
if ($mask & (1 << $i)) {
$players[] = $i + 1;
}
}
sort($players);
$m = count($players);
$idx1 = array_search($firstPlayer, $players);
$idx2 = array_search($secondPlayer, $players);
if ($idx1 === false || $idx2 === false) {
continue;
}
if ($idx1 > $idx2) {
list($idx1, $idx2) = [$idx2, $idx1];
}
if ($idx2 == $m - 1 - $idx1) {
if ($round < $ans_earliest) {
$ans_earliest = $round;
}
if ($round > $ans_latest) {
$ans_latest = $round;
}
continue;
}
$next_mask_base = 0;
if ($m % 2 == 1) {
$mid_index = ($m - 1) >> 1;
$mid_player = $players[$mid_index];
$next_mask_base = 1 << ($mid_player - 1);
}
$matches = [];
for ($i = 0; $i < intval($m / 2); $i++) {
$a = $players[$i];
$b = $players[$m - 1 - $i];
$matches[] = [$a, $b];
}
$next_masks = [$next_mask_base];
foreach ($matches as $match) {
list($a, $b) = $match;
if ($a == $firstPlayer || $a == $secondPlayer) {
$winner = $a;
$bit_winner = 1 << ($winner - 1);
foreach ($next_masks as $j => $nm) {
$next_masks[$j] |= $bit_winner;
}
} elseif ($b == $firstPlayer || $b == $secondPlayer) {
$winner = $b;
$bit_winner = 1 << ($winner - 1);
foreach ($next_masks as $j => $nm) {
$next_masks[$j] |= $bit_winner;
}
} else {
$new_next_masks = [];
$bit_a = 1 << ($a - 1);
$bit_b = 1 << ($b - 1);
foreach ($next_masks as $nm) {
$new_next_masks[] = $nm | $bit_a;
$new_next_masks[] = $nm | $bit_b;
}
$next_masks = $new_next_masks;
}
}
if ($round < $max_round) {
foreach ($next_masks as $nm) {
if (!isVisited($round + 1, $nm, $visited, $int_len)) {
setVisited($round + 1, $nm, $visited, $int_len);
$next_states[] = $nm;
}
}
}
}
$current_states = $next_states;
}
return [$ans_earliest, $ans_latest];
}
/**
* @param $round
* @param $mask
* @param $visited
* @param $int_len
* @return false|int
*/
function isVisited($round, $mask, &$visited, $int_len) {
$idx = $mask >> 5;
$bit = $mask & 31;
if ($idx < $int_len) {
return ($visited[$round][$idx] >> $bit) & 1;
}
return false;
}
/**
* @param $round
* @param $mask
* @param $visited
* @param $int_len
* @return void
*/
function setVisited($round, $mask, &$visited, $int_len) {
$idx = $mask >> 5;
$bit = $mask & 31;
if ($idx < $int_len) {
$visited[$round][$idx] |= (1 << $bit);
}
}
// Test cases
print_r(earliestAndLatest(11, 2, 4)); // Output: [3, 4]
print_r(earliestAndLatest(5, 1, 5)); // Output: [1, 1]
?> Explanation:
This approach efficiently explores all possible tournament paths while leveraging bit manipulation for state management, ensuring optimal performance even for the upper constraint limits. |
Beta Was this translation helpful? Give feedback.
We need to determine the earliest and latest rounds in which two given players (firstPlayer and secondPlayer) will compete against each other in a tournament. The tournament involves multiple rounds where players are paired in a specific manner, and the outcomes of matches not involving the two best players can be chosen arbitrarily to either expedite or delay their meeting.
Approach