aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2024-09-08 19:39:02 -0600
committerJack Sangdahl <[email protected]>2024-09-08 19:39:02 -0600
commit1539df1b1354faff30151ccaa907793b362b27d5 (patch)
tree1992404966d3cf9ad8a81df5609425589dd3a774
parente4a7cbda66d8490df756a6170b1bf47afc428419 (diff)
downloadleetcode-1539df1b1354faff30151ccaa907793b362b27d5.tar.gz
leetcode-1539df1b1354faff30151ccaa907793b362b27d5.zip
2326. spiral matrix iv
-rw-r--r--spiral_matrix4.c66
-rw-r--r--spiral_matrix4.py49
2 files changed, 115 insertions, 0 deletions
diff --git a/spiral_matrix4.c b/spiral_matrix4.c
new file mode 100644
index 0000000..d4e3be7
--- /dev/null
+++ b/spiral_matrix4.c
@@ -0,0 +1,66 @@
+// 2326. Spiral Matrix IV
+#include "leetcode.h"
+
+/*
+ * you are given two integer '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 * 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 spaces, fill them with -1. return the generated matrix.
+ */
+
+int **spiralMatrix(int m, int n, struct ListNode *head, int *returnSize,
+ int **returnColumnSizes) {
+ *returnSize = m;
+ *returnColumnSizes = (int *)calloc(m, sizeof(int));
+ int **ans = (int **)calloc(m, sizeof(int *));
+ for (int i = 0; i < m; i++) {
+ (*returnColumnSizes)[i] = n;
+ ans[i] = (int *)calloc(n, sizeof(int));
+ memset(ans[i], -1, n * sizeof(int));
+ }
+ int start_row = 0, start_col = 0, end_row = 0, end_col = 0;
+ while (head) {
+ for (int col = start_col; col < end_col && head; col++) {
+ ans[start_row][col] = head->val;
+ head = head->next;
+ }
+ start_row++;
+ for (int row = start_row; row < end_row && head; row++) {
+ ans[row][end_col - 1] = head->val;
+ head = head->next;
+ }
+ end_col--;
+ if (start_row < end_row) {
+ for (int col = end_col - 1; col >= start_col && head; col--) {
+ ans[end_row - 1][col] = head->val;
+ head = head->next;
+ }
+ end_row--;
+ }
+ if (start_col < end_col) {
+ for (int row = end_row - 1; row >= start_row && head; row--) {
+ ans[row][start_col] = head->val;
+ head = head->next;
+ }
+ start_col++;
+ }
+ }
+ return ans;
+}
+
+int main() {
+ struct ListNode *head = listnode_create(0);
+ head->next = listnode_create(1);
+ head->next->next = listnode_create(2);
+ int returnSize, **returnColumnSizes;
+ int **sm = spiralMatrix(1, 4, head, &returnSize, returnColumnSizes);
+ for (int i = 0; i < returnSize; i++) {
+ for (int j = 0; j < returnColumnSizes[i]; j++)
+ printf("%d ", sm[i][j]);
+ printf("\n");
+ }
+ for (int i = 0; i < returnSize; i++)
+ free(sm[i]);
+ free(sm);
+}
diff --git a/spiral_matrix4.py b/spiral_matrix4.py
new file mode 100644
index 0000000..46088b1
--- /dev/null
+++ b/spiral_matrix4.py
@@ -0,0 +1,49 @@
+# 2326. Spiral Matrix IV
+
+"""
+you are given two integer 'm' and 'n', which repansent the dimensions of a
+matrix. you are also given the 'head' of a linked list of integers. generate
+an 'm n' matrix that contains the integers in the linked list pansented in
+spiral order (clockwise) starting from the top left of the matrix. if there
+are remaining spaces, fill them with -1. return the generated matrix.
+"""
+
+
+# Definition for singly-linked list.
+class ListNode(object):
+ def __init__(self, val=0, next=None):
+ self.val = val
+ self.next = next
+
+
+class Solution(object):
+ def spiralMatrix(self, m, n, head):
+ """
+ :type m: int
+ :type n: int
+ :type head: Optional[ListNode]
+ :rtype: List[List[int]]
+ """
+ ans = [[-1 for j in range(n)] for i in range(m)]
+ x, y = 0, 0
+ dx, dy = 1, 0
+ while head:
+ ans[y][x] = head.val
+ if (
+ x + dx < 0
+ or x + dx >= n
+ or y + dy < 0
+ or y + dy >= m
+ or ans[y + dy][x + dx] != -1
+ ):
+ dx, dy = -dy, dx
+ x = x + dx
+ y = y + dy
+ head = head.next
+ return ans
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.spiralMatrix(m=3, n=5, head=[3, 0, 2, 6, 8, 1, 7, 9, 4, 2, 5, 5, 0]))
+ print(obj.spiralMatrix(m=1, n=4, head=[0, 1, 2]))