Saturday, December 28, 2019

Leetcode solution 59: Sprial Matrix II

Problem Statement 

Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
Input: 3
 [ 1, 2, 3 ],
 [ 8, 9, 4 ],
 [ 7, 6, 5 ]
 Problem link

Video Tutorial

You can find the detailed video tutorial here

Thought Process

Straight forward question, could use Spiral Matrix exact same recursion algorithm to solve this, you can also watch that video tutorial here

Still needs pay attribute to this guy, had another great solution:


Simulation using Recursion

Time Complexity: O(M*N) where M, N is row and col of matrix
Space Complexity: O(M*N) since we used list to store the result, where M, N is row and col of matrix


No comments:

Post a Comment

Thank your for your comment! Check out us at if you need mock interviews!