Java Solution for Spiral Matrix III Problem
Intuition\n Describe your first thoughts on how to solve this problem. \nThe goal is to traverse a grid in a clockwise spiral pattern, starting from a given ini
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe goal is to traverse a grid in a clockwise spiral pattern, starting from a given initial position. This problem can be visualized by imagining a snail\u2019s shell, where the movement gradually spirals outward from the starting point. The challenge is to ensure we cover all cells exactly once, and we can achieve this by systematically expanding our movement outward in all four cardinal directions.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1.\tDirection Vectors: We define direction vectors for moving right, down, left, and up. These vectors help in changing the current position in the respective directions.\n2.\tSimulation of Movement:\n\t\u2022\tWe start at the initial position and move in the current direction for a specified number of steps.\n\t\u2022\tAfter completing the steps in the current direction, we turn to the next direction in a clockwise manner.\n\t\u2022\tEvery two turns, we increase the number of steps by one. This expansion ensures the spiral grows correctly.\n3.\tBoundary Check: While moving, we check if the new position is within the grid boundaries. If it is, we record the position.\n4.\tTermination: The process continues until we have recorded all positions in the grid.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nThe time complexity of the solution is O(rows x cols), where rows is the number of rows and cols is the number of columns in the grid. This is because we need to visit each cell exactly once, and the number of operations per cell (checking boundaries and updating positions) is constant.\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nThe space complexity is O(rows x cols) for storing the result array. Apart from the result array, we use a few variables for indices and direction management, which consume constant space O(1).\n\n# Code\n```\nclass Solution {\n    public int[][] spiralMatrixIII(int rows, int cols, int rStart, int cStart) {\n        // Direction vectors: right, down, left, up\n        int[] dr = {0, 1, 0, -1};\n        int[] dc = {1, 0, -1, 0};\n        \n        int totalCells = rows * cols;\n        int[][] result = new int[totalCells][2];\n        int r = rStart, c = cStart;\n        int direction = 0; // start by moving right\n        int steps = 1;\n        int index = 0;\n\n        result[index++] = new int[]{r, c};\n        \n        while (index < totalCells) {\n            for (int i = 0; i < 2; i++) { // there are 2 turns in each full spiral loop\n                for (int j = 0; j < steps; j++) {\n                    r += dr[direction];\n                    c += dc[direction];\n                    if (r >= 0 && r < rows && c >= 0 && c < cols) {\n                        result[index++] = new int[]{r, c};\n                        if (index == totalCells) {\n                            return result;\n                        }\n                    }\n                }\n                direction = (direction + 1) % 4; // change direction\n            }\n            steps++; // increase the number of steps after two turns\n        }\n\n        return result;\n    }\n}\n```
['Java']