the power of 8 = // 16,777,216 possible board configurations $board = $this->generateBoard($hash); if (!$this->hasCollisions($board)) { return true; } $this->run($hash-1); }
function format() : string; public function hasCollision(int $row, int $col) : bool; public function run(int $col) : bool; } class NQueensSolver implements SolverInterface {}
if we've iterated over the whole board if ($col >= $this->n) { return true; } // iterate over the rows for ($row = 0; $row < $this->n; ++$row) { if (!$this->hasCollision($row, $col)) { // place a queen $this->board[$row][$col] = 1; // check the next column to see if this worked if ($this->run($col+1)) { return true; } // this didn't work so let's remove the queen, backtrack, and try the next row $this->board[$row][$col] = 0; } } return false; }
if we've iterated over the whole board if ($col >= $this->n) { return true; } // iterate over the rows for ($row = 0; $row < $this->n; ++$row) { if (!$this->hasCollision($row, $col)) { // place a queen $this->board[$row][$col] = 1; // check the next column to see if this worked if ($this->run($col+1)) { return true; } // this didn't work so let's remove the queen, backtrack, and try the next row $this->board[$row][$col] = 0; } } return false; }
if we've iterated over the whole board if ($col >= $this->n) { return true; } // iterate over the rows for ($row = 0; $row < $this->n; ++$row) { if (!$this->hasCollision($row, $col)) { // place a queen $this->board[$row][$col] = 1; // check the next column to see if this worked if ($this->run($col+1)) { return true; } // this didn't work so let's remove the queen, backtrack, and try the next row $this->board[$row][$col] = 0; } } return false; }
if we've iterated over the whole board if ($col >= $this->n) { return true; } // iterate over the rows for ($row = 0; $row < $this->n; ++$row) { if (!$this->hasCollision($row, $col)) { // place a queen $this->board[$row][$col] = 1; // check the next column to see if this worked if ($this->run($col+1)) { return true; } // this didn't work so let's remove the queen, backtrack, and try the next row $this->board[$row][$col] = 0; } } return false; }
if we've iterated over the whole board if ($col >= $this->n) { return true; } // iterate over the rows for ($row = 0; $row < $this->n; ++$row) { if (!$this->hasCollision($row, $col)) { // place a queen $this->board[$row][$col] = 1; // check the next column to see if this worked if ($this->run($col+1)) { return true; } // this didn't work so let's remove the queen, backtrack, and try the next row $this->board[$row][$col] = 0; } } return false; }
if we've iterated over the whole board if ($col >= $this->n) { return true; } // iterate over the rows for ($row = 0; $row < $this->n; ++$row) { if (!$this->hasCollision($row, $col)) { // place a queen $this->board[$row][$col] = 1; // check the next column to see if this worked if ($this->run($col+1)) { return true; } // this didn't work so let's remove the queen, backtrack, and try the next row $this->board[$row][$col] = 0; } } return false; }
if we've iterated over the whole board if ($col >= $this->n) { return true; } // iterate over the rows for ($row = 0; $row < $this->n; ++$row) { if (!$this->hasCollision($row, $col)) { // place a queen $this->board[$row][$col] = 1; // check the next column to see if this worked if ($this->run($col+1)) { return true; } // this didn't work so let's remove the queen, backtrack, and try the next row $this->board[$row][$col] = 0; } } return false; }
if we've iterated over the whole board if ($col >= $this->n) { return true; } // iterate over the rows for ($row = 0; $row < $this->n; ++$row) { if (!$this->hasCollision($row, $col)) { // place a queen $this->board[$row][$col] = 1; // check the next column to see if this worked if ($this->run($col+1)) { return true; } // this didn't work so let's remove the queen, backtrack, and try the next row $this->board[$row][$col] = 0; } } return false; }
if we've iterated over the whole board if ($col >= $this->n) { return true; } // iterate over the rows for ($row = 0; $row < $this->n; ++$row) { if (!$this->hasCollision($row, $col)) { // place a queen $this->board[$row][$col] = 1; // check the next column to see if this worked if ($this->run($col+1)) { return true; } // this didn't work so let's remove the queen, backtrack, and try the next row $this->board[$row][$col] = 0; } } return false; }
// check the current row for ($thisCol = 0; $thisCol < $col; ++$thisCol) { if ($this->board[$row][$thisCol]) { return true; } } // check diagonally to the top left // no need to check the top right since we're processing one column at a time for ($thisRow = $row, $thisCol = $col; $thisRow >= 0 && $thisCol >= 0; $thisRow--, $thisCol--) { if ($this->board[$thisRow][$thisCol]) { return true; } } // check diagonally to the bottom left // no need to check the bottom right since we're processing one column at a time for ($thisRow = $row, $thisCol = $col; $thisCol >= 0 && $thisRow < $this->n; $thisRow++, $thisCol--) { if ($this->board[$thisRow][$thisCol]) { return true; } } return false; }
// check the current row for ($thisCol = 0; $thisCol < $col; ++$thisCol) { if ($this->board[$row][$thisCol]) { return true; } } // check diagonally to the top left // no need to check the top right since we're processing one column at a time for ($thisRow = $row, $thisCol = $col; $thisRow >= 0 && $thisCol >= 0; $thisRow--, $thisCol--) { if ($this->board[$thisRow][$thisCol]) { return true; } } // check diagonally to the bottom left // no need to check the bottom right since we're processing one column at a time for ($thisRow = $row, $thisCol = $col; $thisCol >= 0 && $thisRow < $this->n; $thisRow++, $thisCol--) { if ($this->board[$thisRow][$thisCol]) { return true; } } return false; }
// check the current row for ($thisCol = 0; $thisCol < $col; ++$thisCol) { if ($this->board[$row][$thisCol]) { return true; } } // check diagonally to the top left // no need to check the top right since we're processing one column at a time for ($thisRow = $row, $thisCol = $col; $thisRow >= 0 && $thisCol >= 0; $thisRow--, $thisCol--) { if ($this->board[$thisRow][$thisCol]) { return true; } } // check diagonally to the bottom left // no need to check the bottom right since we're processing one column at a time for ($thisRow = $row, $thisCol = $col; $thisCol >= 0 && $thisRow < $this->n; $thisRow++, $thisCol--) { if ($this->board[$thisRow][$thisCol]) { return true; } } return false; }
names in 2021, sort them alphabetically” “List the top 10 products sold alphabetically on the sidebar of a site. Given an array of the top 1,000 products where the key is their popularity and the value is the product name. Return an array.” Quicksort
sidebar of a site. Given an array of the top 1,000 products where the key is their popularity and the value is the product name. Return an array.” Quicksort
“List the top 10 products sold alphabetically on the sidebar of a site. Given an array of the top 1,000 products where the key is their popularity and the value is the product name. Return an array.” Quicksort
“List the top 10 products sold alphabetically on the sidebar of a site. Given an array of the top 1,000 products where the key is their popularity and the value is the product name. Return an array.” Quicksort