<p>Given a positive integer <code>n</code>, generate an <code>n x n</code> <code>matrix</code> filled with elements from <code>1</code> to <code>n<sup>2</sup></code> in spiral order.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/13/spiraln.jpg" style="width: 242px; height: 242px;" />
<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> [[1,2,3],[8,9,4],[7,6,5]]
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> [[1]]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 20</code></li>
</ul>

class Solution:
    def generateMatrix(self, n: int) -> List[List[int]]:
        nums = []
        for i in range(1,n**2+1):
            nums.append(i)

        res = [[0 for _ in range(n)] for _ in range(n)]

        i = 0
        j = 0
        up, left = 0, 0
        down, right = n-1, n-1

        for t in range(n, 1, -2):
            for i in range(left, right):
                res[up][i] = nums[j]
                j+=1

            for i in range(up, down):
                res[i][right] = nums[j]
                j+=1

            for i in range(right, left, -1):
                res[down][i] = nums[j]
                j+=1

            for i in range(down, up, -1):
                res[i][left] = nums[j]
                j+=1

            up+=1
            left+=1
            down-=1
            right-=1
            
        if n%2:
            res[up][right] = nums[j]

        return res


16500