JavaScript Solutions, Competitive programming in JavaScript, MCQ in JS

Tuesday, 4 July 2023

LeetCode Solution: 54: Spiral Matrix in JavaScript

LeetCode Solution: 54 Spiral Matrix in JavaScript

 Hello, fellow coding enthusiasts! In this blog post, we'll tackle an interesting problem from LeetCode - "Spiral Matrix." We'll provide a detailed JavaScript solution and a table of contents to guide you through the discussion. Additionally, we'll analyze the time and space complexity of the solution.

    Problem Overview

    The problem, as stated on LeetCode, is to traverse a given m x n matrix in a spiral order and return all the elements in that order.

    Example 1:


    Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]

    Output: [1,2,3,6,9,8,7,4,5]

    Example 2:


    Input: matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]

    Output: [1,2,3,4,8,12,11,10,9,5,6,7]

    Approach and Algorithm

    To solve this problem, we will simulate the process of spiraling through the matrix. We will use four pointers to represent the boundaries of the current spiral. As we move through each spiral layer, we will collect the elements in the order specified.


    The algorithm can be summarized as follows:

    1. Initialize four pointers top, bottom, left, and right to represent the boundaries of the current spiral.
    2. Traverse the matrix in a spiral order while updating these pointers and collecting the elements.
    3. Repeat until all elements in the matrix have been processed.

    JavaScript Solution

    Now, let's implement the solution in JavaScript:


    /**
    * @param {number[][]} matrix
    * @return {number[]}
    */
    const spiralOrder = function(matrix) {
    const result = [];
    if (!matrix || matrix.length === 0) return result;

    let top = 0;
    let bottom = matrix.length - 1;
    let left = 0;
    let right = matrix[0].length - 1;

    while (top <= bottom && left <= right) {
    // Traverse top row
    for (let i = left; i <= right; i++) {
    result.push(matrix[top][i]);
    }
    top++;

    // Traverse rightmost column
    for (let i = top; i <= bottom; i++) {
    result.push(matrix[i][right]);
    }
    right--;

    if (top <= bottom) {
    // Traverse bottom row
    for (let i = right; i >= left; i--) {
    result.push(matrix[bottom][i]);
    }
    bottom--;
    }

    if (left <= right) {
    // Traverse leftmost column
    for (let i = bottom; i >= top; i--) {
    result.push(matrix[i][left]);
    }
    left++;
    }
    }

    return result;
    };


    Time and Space Complexity Analysis

    Let's analyze the time and space complexity of our solution.


    Time Complexity: 

    The solution iterates through the entire matrix once, visiting each cell exactly once. Therefore, the time complexity is O(m * n), where m is the number of rows, and n is the number of columns in the matrix.


    Space Complexity: 

    The space complexity is O(m * n) as well. The result array stores all the elements in the matrix.


    Conclusion

    In this blog post, we explored the LeetCode problem "Spiral Matrix" and provided a JavaScript solution to traverse the matrix in a spiral order. We discussed the algorithm, implemented the solution, and analyzed its time and space complexity.


    Solving problems like these can enhance your problem-solving skills and improve your coding abilities. We hope this article was helpful to you, and happy coding! Keep practicing, and you'll become a master of algorithmic challenges in no time!


    If you have any questions or suggestions, feel free to leave a comment below.

    No comments:

    Post a Comment