LeetCode 2326: Spiral Matrix IV Solution

Master LeetCode problem 2326 (Spiral Matrix IV), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2326. Spiral Matrix IV

Problem Explanation

Explanation

To solve this problem, we will iterate through the linked list to populate the matrix in a spiral order. We can achieve this by simulating the process of filling the matrix in a clockwise spiral order. We will keep track of the boundaries of the matrix and iterate through each side of the matrix, updating the elements accordingly.

Algorithmic Steps:

  1. Create a 2D matrix of size m x n filled with -1.
  2. Traverse the linked list and store the values in an array.
  3. Initialize variables for boundaries (top, bottom, left, right) and direction (0 for right, 1 for down, 2 for left, 3 for up).
  4. Iterate through the matrix in a spiral order, filling in the values from the linked list.
  5. Return the generated matrix.

Time Complexity: O(m * n) - We iterate through the entire matrix to fill in the values. Space Complexity: O(m * n) - We use a matrix of size m x n to store the elements.

Solution Code

class Solution {
    public int[][] spiralMatrixIII(int m, int n, int[][] head) {
        int[][] matrix = new int[m][n];
        int[][] directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
        int[] bounds = new int[]{0, n - 1, m - 1, 0};
        
        int total = m * n;
        int idx = 0;
        
        for (int[] val : head) {
            matrix[bounds[0]][bounds[3]] = val;
            if (++idx == total)
                break;
            
            int row = bounds[0];
            int col = bounds[3];
            
            int nextRow = row + directions[bounds[4]][0];
            int nextCol = col + directions[bounds[4]][1];
            
            if (nextRow < bounds[0] || nextRow > bounds[2] || nextCol < bounds[3] || nextCol > bounds[1]) {
                bounds[bounds[4]] += (bounds[4] % 2 == 0) ? (bounds[4] == 0 ? 1 : -1) : (bounds[4] == 1 ? -1 : 1);
                
                if (bounds[4] % 2 == 0)
                    bounds[(bounds[4] + 1) % 4] += (bounds[4] == 0) ? 1 : -1;
                else
                    bounds[(bounds[4] + 1) % 4] += (bounds[4] == 1) ? -1 : 1;
                
                bounds[4] = (bounds[4] + 1) % 4;
            }
            
            bounds[0] = Math.min(bounds[0], Math.max(nextRow, 0));
            bounds[1] = Math.max(bounds[1], Math.min(nextCol, n - 1));
            bounds[2] = Math.max(bounds[2], Math.min(nextRow, m - 1));
            bounds[3] = Math.min(bounds[3], Math.max(nextCol, 0));
        }
        
        return matrix;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2326 (Spiral Matrix IV)?

This page provides optimized solutions for LeetCode problem 2326 (Spiral Matrix IV) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2326 (Spiral Matrix IV)?

The time complexity for LeetCode 2326 (Spiral Matrix IV) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2326 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2326 in Java, C++, or Python.

Back to LeetCode Solutions