| | 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 | | namespace LeetCode.Algorithms.SpiralMatrix3; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class SpiralMatrix3Simulation : ISpiralMatrix3 |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(rows x cols) |
| | 19 | | /// Space complexity - O(rows x cols) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="rows"></param> |
| | 22 | | /// <param name="cols"></param> |
| | 23 | | /// <param name="rStart"></param> |
| | 24 | | /// <param name="cStart"></param> |
| | 25 | | /// <returns></returns> |
| | 26 | | public int[][] SpiralMatrixIII(int rows, int cols, int rStart, int cStart) |
| 2 | 27 | | { |
| 2 | 28 | | var result = new int[rows * cols][]; |
| | 29 | |
|
| 2 | 30 | | var direction = 0; |
| 2 | 31 | | var directionCounter = 1; |
| 2 | 32 | | var directionCount = -1; |
| 2 | 33 | | var i = 0; |
| | 34 | |
|
| 100 | 35 | | while (i < result.Length) |
| 98 | 36 | | { |
| 98 | 37 | | if (rStart >= 0 && cStart >= 0 && rStart < rows && cStart < cols) |
| 34 | 38 | | { |
| 34 | 39 | | result[i] = [rStart, cStart]; |
| | 40 | |
|
| 34 | 41 | | i++; |
| 34 | 42 | | } |
| | 43 | |
|
| 98 | 44 | | directionCount++; |
| | 45 | |
|
| 98 | 46 | | if (directionCount >= directionCounter) |
| 24 | 47 | | { |
| 24 | 48 | | directionCount = 0; |
| | 49 | |
|
| 24 | 50 | | direction++; |
| | 51 | |
|
| 24 | 52 | | if (direction > 0 && direction % 2 == 0) |
| 11 | 53 | | { |
| 11 | 54 | | directionCounter++; |
| 11 | 55 | | } |
| | 56 | |
|
| 24 | 57 | | if (direction > 3) |
| 5 | 58 | | { |
| 5 | 59 | | direction = 0; |
| 5 | 60 | | } |
| 24 | 61 | | } |
| | 62 | |
|
| 98 | 63 | | switch (direction) |
| | 64 | | { |
| | 65 | | case 0: |
| 25 | 66 | | cStart++; |
| 25 | 67 | | break; |
| | 68 | | case 1: |
| 23 | 69 | | rStart++; |
| 23 | 70 | | break; |
| | 71 | | case 2: |
| 26 | 72 | | cStart--; |
| 26 | 73 | | break; |
| | 74 | | default: |
| 24 | 75 | | rStart--; |
| 24 | 76 | | break; |
| | 77 | | } |
| 98 | 78 | | } |
| | 79 | |
|
| 2 | 80 | | return result; |
| 2 | 81 | | } |
| | 82 | | } |