| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | 3 | | // All Rights Reserved. |
| | 4 | | // -------------------------------------------------------------------------------- |
| | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | 9 | | // known as Yevhenii Yeriemeieiv). |
| | 10 | | // -------------------------------------------------------------------------------- |
| | 11 | |
|
| | 12 | | using LeetCode.Core.Models; |
| | 13 | |
|
| | 14 | | namespace LeetCode.Algorithms.SpiralMatrix4; |
| | 15 | |
|
| | 16 | | /// <inheritdoc /> |
| | 17 | | public class SpiralMatrix4Simulation : ISpiralMatrix4 |
| | 18 | | { |
| | 19 | | /// <summary> |
| | 20 | | /// Time complexity - O(m * n) |
| | 21 | | /// Space complexity - O(1) |
| | 22 | | /// </summary> |
| | 23 | | /// <param name="m"></param> |
| | 24 | | /// <param name="n"></param> |
| | 25 | | /// <param name="head"></param> |
| | 26 | | /// <returns></returns> |
| | 27 | | public int[][] SpiralMatrix(int m, int n, ListNode head) |
| 3 | 28 | | { |
| 3 | 29 | | var matrix = new int[m][]; |
| | 30 | |
|
| 22 | 31 | | for (var i = 0; i < m; i++) |
| 8 | 32 | | { |
| 8 | 33 | | matrix[i] = new int[n]; |
| 8 | 34 | | } |
| | 35 | |
|
| 3 | 36 | | var x = 0; |
| 3 | 37 | | var y = 0; |
| 3 | 38 | | var round = 0; |
| 3 | 39 | | var direction = 0; |
| 3 | 40 | | var count = 0; |
| | 41 | |
|
| 3 | 42 | | var listNode = head; |
| | 43 | |
|
| 38 | 44 | | while (count < m * n) |
| 35 | 45 | | { |
| 35 | 46 | | if (listNode == null) |
| 3 | 47 | | { |
| 3 | 48 | | matrix[x][y] = -1; |
| 3 | 49 | | } |
| | 50 | | else |
| 32 | 51 | | { |
| 32 | 52 | | matrix[x][y] = listNode.val; |
| 32 | 53 | | } |
| | 54 | |
|
| 35 | 55 | | switch (direction) |
| | 56 | | { |
| | 57 | | case 0: |
| | 58 | |
|
| 18 | 59 | | if (y + 1 < n - round) |
| 13 | 60 | | { |
| 13 | 61 | | y++; |
| 13 | 62 | | } |
| | 63 | | else |
| 5 | 64 | | { |
| 5 | 65 | | direction = 1; |
| | 66 | |
|
| 5 | 67 | | x++; |
| 5 | 68 | | } |
| | 69 | |
|
| 18 | 70 | | break; |
| | 71 | | case 1: |
| 6 | 72 | | if (x + 1 < m - round) |
| 3 | 73 | | { |
| 3 | 74 | | x++; |
| 3 | 75 | | } |
| | 76 | | else |
| 3 | 77 | | { |
| 3 | 78 | | direction = 2; |
| | 79 | |
|
| 3 | 80 | | y--; |
| 3 | 81 | | } |
| | 82 | |
|
| 6 | 83 | | break; |
| | 84 | | case 2: |
| 8 | 85 | | if (y - 1 < round) |
| 3 | 86 | | { |
| 3 | 87 | | direction = 3; |
| | 88 | |
|
| 3 | 89 | | round++; |
| | 90 | |
|
| 3 | 91 | | x--; |
| 3 | 92 | | } |
| | 93 | | else |
| 5 | 94 | | { |
| 5 | 95 | | y--; |
| 5 | 96 | | } |
| | 97 | |
|
| 8 | 98 | | break; |
| | 99 | | case 3: |
| 3 | 100 | | if (x - 1 < round) |
| 2 | 101 | | { |
| 2 | 102 | | direction = 0; |
| | 103 | |
|
| 2 | 104 | | y++; |
| 2 | 105 | | } |
| | 106 | | else |
| 1 | 107 | | { |
| 1 | 108 | | x--; |
| 1 | 109 | | } |
| | 110 | |
|
| 3 | 111 | | break; |
| | 112 | | } |
| | 113 | |
|
| 35 | 114 | | listNode = listNode?.next; |
| 35 | 115 | | count++; |
| 35 | 116 | | } |
| | 117 | |
|
| 3 | 118 | | return matrix; |
| 3 | 119 | | } |
| | 120 | | } |