2326. Spiral Matrix IV #509
-
Topics: You are given two integers You are also given the Generate an Return the generated matrix. Example 1:
Example 2:
Example 3:
Constraints:
Hint:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
We will simulate a spiral traversal of an Here's how the solution is structured:
Let's implement this solution in PHP: 2326. Spiral Matrix IV <?php
class ListNode {
public $val = 0;
public $next = null;
function __construct($val = 0, $next = null) {
$this->val = $val;
$this->next = $next;
}
}
/**
* @param Integer $m
* @param Integer $n
* @param ListNode $head
* @return Integer[][]
*/
function spiralMatrix($m, $n, $head) {
// Step 1: Initialize an m x n matrix filled with -1
$matrix = array_fill(0, $m, array_fill(0, $n, -1));
// Step 2: Define the direction vectors for right, down, left, and up movements
$dirs = [[0, 1], [1, 0], [0, -1], [-1, 0]]; // right, down, left, up
$dirIndex = 0; // Start with the direction moving right
$row = 0;
$col = 0;
// Step 3: Traverse the linked list
$curr = $head;
while ($curr) {
// Step 4: Fill the current position in the matrix with the current node's value
$matrix[$row][$col] = $curr->val;
// Move to the next node in the linked list
$curr = $curr->next;
// Calculate the next position
$nextRow = $row + $dirs[$dirIndex][0];
$nextCol = $col + $dirs[$dirIndex][1];
// Step 5: Check if the next position is out of bounds or already filled
if ($nextRow < 0 || $nextRow >= $m || $nextCol < 0 || $nextCol >= $n || $matrix[$nextRow][$nextCol] != -1) {
// Change direction (rotate clockwise)
$dirIndex = ($dirIndex + 1) % 4;
// Update the next position after changing direction
$nextRow = $row + $dirs[$dirIndex][0];
$nextCol = $col + $dirs[$dirIndex][1];
}
// Step 6: Update row and col to the next position
$row = $nextRow;
$col = $nextCol;
}
return $matrix;
}
// Helper function to print the matrix (for debugging)
function printMatrix($matrix) {
foreach ($matrix as $row) {
echo implode(" ", $row) . "\n";
}
}
// Example usage:
// Create the linked list: [3,0,2,6,8,1,7,9,4,2,5,5,0]
$head = new ListNode(3);
$head->next = new ListNode(0);
$head->next->next = new ListNode(2);
$head->next->next->next = new ListNode(6);
$head->next->next->next->next = new ListNode(8);
$head->next->next->next->next->next = new ListNode(1);
$head->next->next->next->next->next->next = new ListNode(7);
$head->next->next->next->next->next->next->next = new ListNode(9);
$head->next->next->next->next->next->next->next->next = new ListNode(4);
$head->next->next->next->next->next->next->next->next->next = new ListNode(2);
$head->next->next->next->next->next->next->next->next->next->next = new ListNode(5);
$head->next->next->next->next->next->next->next->next->next->next->next = new ListNode(5);
$head->next->next->next->next->next->next->next->next->next->next->next->next = new ListNode(0);
$m = 3;
$n = 5;
$matrix = spiralMatrix($m, $n, $head);
printMatrix($matrix);
?> Explanation:
Time Complexity:
|
Beta Was this translation helpful? Give feedback.
We will simulate a spiral traversal of an
m x n
matrix, filling it with values from a linked list. The remaining positions that don't have corresponding linked list values will be filled with-1
.Here's how the solution is structured:
m x n
matrix initialized with-1
.