LeetCode 885: Spiral Matrix III Solution

Master LeetCode problem 885 (Spiral Matrix III), 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.

885. Spiral Matrix III

Problem Explanation

Explanation

To solve this problem, we can simulate the movement in a spiral order by keeping track of the direction we are moving in. We start at the given cell (rStart, cStart) and keep moving in a spiral pattern until we have visited all cells in the grid.

We can define four directions: right, down, left, and up. We start by moving right, then down, then left, then up, and repeat this pattern while adjusting the boundaries of the grid as we move.

We can keep track of the current position and direction, and iterate until we have visited all cells in the grid.

Time Complexity: O(rows * cols) - we need to visit all cells in the grid once.

Space Complexity: O(rows * cols) - the space required to store the result array.

Solution Code

class Solution {
    public int[][] spiralMatrixIII(int rows, int cols, int rStart, int cStart) {
        int[][] result = new int[rows * cols][2];
        int[][] directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; // right, down, left, up
        int direction = 0; // starting with right
        int steps = 1; // number of steps to take in current direction
        int stepCount = 0; // counter for steps taken
        int r = rStart, c = cStart;
        int index = 0;
        
        while (index < rows * cols) {
            if (r >= 0 && r < rows && c >= 0 && c < cols) {
                result[index][0] = r;
                result[index][1] = c;
                index++;
            }
            
            r += directions[direction][0];
            c += directions[direction][1];
            stepCount++;
            
            if (stepCount == steps) {
                direction = (direction + 1) % 4;
                if (direction == 0 || direction == 2) {
                    steps++; // increase steps every two directions
                }
                stepCount = 0;
            }
        }
        
        return result;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 885 (Spiral Matrix III)?

This page provides optimized solutions for LeetCode problem 885 (Spiral Matrix III) 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 885 (Spiral Matrix III)?

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

Can I run code for LeetCode 885 on DevExCode?

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

Back to LeetCode Solutions