Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Spiral Matrix IV

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "2326" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

You are given two integers m and n, which represent the dimensions of a matrix. You are also given the head of a linked list of integers. Generate an m x n matrix that contains the integers in the linked list presented in spiral order (clockwise), starting from the top-left of the matrix. If there are remaining empty spaces, fill them with -1. Return the generated matrix. Example 1: Input: m = 3, n = 5, head = [3,0,2,6,8,1,7,9,4,2,5,5,0] Output: [[3,0,2,6,8],[5,0,-1,-1,1],[5,2,4,9,7]] Explanation: The diagram above shows how the values are printed in the matrix. Note that the remaining spaces in the matrix are filled with -1. Example 2: Input: m = 1, n = 4, head = [0,1,2] Output: [[0,1,2,-1]] Explanation: The diagram above shows how the values are printed from left to right in the matrix. The last space in the matrix is set to -1. Constraints: 1 <= m, n <= 105 1 <= m n <= 105 The number of nodes in the list is in the range [1, m n]. 0 <= Node.val <= 1000

Explanation

  • Spiral Traversal: The core idea is to simulate the spiral traversal of the matrix. We maintain boundaries (top, bottom, left, right) and move along the edges, filling the matrix elements with values from the linked list.
    • Linked List Iteration: We iterate through the linked list, inserting the node values into the matrix as we traverse it in a spiral manner.
    • Handling Empty Spaces: After exhausting the linked list, we fill any remaining empty spaces in the matrix with -1.
  • Time Complexity: O(m*n), where m and n are the dimensions of the matrix.
  • Space Complexity: O(m*n) due to the matrix storage.

Code

    from typing import List, Optional

# Definition for singly-linked list.
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

class Solution:
    def spiralMatrix(self, m: int, n: int, head: Optional[ListNode]) -> List[List[int]]:
        matrix = [[0] * n for _ in range(m)]
        for i in range(m):
            for j in range(n):
                matrix[i][j] = -1

        top, bottom = 0, m - 1
        left, right = 0, n - 1
        direction = 0  # 0: right, 1: down, 2: left, 3: up
        curr = head

        while curr:
            if direction == 0:  # right
                for i in range(left, right + 1):
                    if curr:
                        matrix[top][i] = curr.val
                        curr = curr.next
                    else:
                        break
                top += 1
            elif direction == 1:  # down
                for i in range(top, bottom + 1):
                    if curr:
                        matrix[i][right] = curr.val
                        curr = curr.next
                    else:
                        break
                right -= 1
            elif direction == 2:  # left
                for i in range(right, left - 1, -1):
                    if curr:
                        matrix[bottom][i] = curr.val
                        curr = curr.next
                    else:
                        break
                bottom -= 1
            elif direction == 3:  # up
                for i in range(bottom, top - 1, -1):
                    if curr:
                        matrix[i][left] = curr.val
                        curr = curr.next
                    else:
                        break
                left += 1

            direction = (direction + 1) % 4

        return matrix

More from this blog

C

Chatmagic blog

2894 posts