Skip to content

Commit 5a34c85

Browse files
Added solution for Spiral Matrix II.
1 parent b3c41cb commit 5a34c85

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ Happy to accept any contributions to this in any langugage.
5454
19. [Leet Code 22 Generate Paranthesis](https://leetcode.com/problems/generate-parentheses/) | [Solution Brute Force](level_medium/LeetCode_22_Generate_Paranthesis_1.ts) | [Solution BF Optimized](level_medium/LeetCode_22_Generate_Paranthesis_2.ts)
5555
20. [Leet Code 54 Spiral Matrix](https://leetcode.com/problems/spiral-matrix/) | [Solution](./med/../level_medium/SpiralMatrix.ts)
5656
21. [Leet Code 1239 - Maximum length of the unique character string](https://leetcode.com/problems/maximum-length-of-a-concatenated-string-with-unique-characters/) | [Solution](./level_medium/LeetCode_1239_MaxLength.ts)
57+
22. [Leet Code 445 - Add Numbers II](https://leetcode.com/problems/add-two-numbers-ii/) | [Solution 1](./level_medium/LeetCode%20445_Add%202%20Numbers_1.ts) | [Solution 2](./level_medium/LeetCode%20445_Add%202%20Numbers_2.ts)
58+
23. [Leet Code 59 - Spiral Matrix II](https://leetcode.com/problems/spiral-matrix-ii/) | [Solution](level_medium/SpiralMatrixII.ts)
59+
5760

5861

5962
## Hard
@@ -62,8 +65,7 @@ Happy to accept any contributions to this in any langugage.
6265
3. [Leet Code 297 - Serialize Deserialize Binary Tree](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/) | [Solution](./level_hard/SerializeDeserializeBinaryTree.ts)
6366
4. [Leet Code 428 - Serialize Deserialize N-Ary Tree](https://leetcode.com/problems/serialize-and-deserialize-n-ary-tree/) | [Solution](./level_hard/SerializeDeserializeNAryTree.ts)
6467
5. [Leet Code 25 - Reverse Linked List k Nodes at a time](https://leetcode.com/problems/reverse-nodes-in-k-group/) | [Solution](./level_hard/LeetCode%2025_Reverse%20Linked%20List%20K%20Nodes.ts)
65-
6. [Leet Code 445 - Add Numbers II](https://leetcode.com/problems/add-two-numbers-ii/) | [Solution 1](./level_medium/LeetCode%20445_Add%202%20Numbers_1.ts) | [Solution 2](./level_medium/LeetCode%20445_Add%202%20Numbers_2.ts)
66-
68+
6769
## Sorting
6870
1. Bubble Sort | [Solution](./sorting/Sort_Bubble.js)
6971
2. Insertion Sort | [Solution](./sorting/Sort_Insertion.js)

level_medium/SpiralMatrixII.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* Leet Code 59: Spiral Matrix II
3+
*
4+
* Two Solutions are available:
5+
* Just like the Spiral Matrix I, we fill the items layer by layer by setting the perimeter boundaries to extremes.
6+
* Then, we move in each direction, horizontal top, vertical right, horizontal bottom, vertical left and so on. After
7+
* completing each layer, we squeeze the perimter boundary one by one, until we get to an invalid.
8+
*
9+
* 2nd solution used here uses the directionality matrix which allows us to change the direction with each iteration.
10+
* This allows us to use one loop till all the items are done. At each step, we need to change the direction of
11+
* the pivot to either left, bottom, right, or top. This can be done as we hit the wall.
12+
*
13+
* Time: O(n*n) since each position needs to be visited once. Space: O(1) since no additional space is used apart
14+
* from the result.
15+
*
16+
* @param n
17+
*/
18+
function generateMatrix(n: number): number[][] {
19+
const result: number[][] = Array.from({length: n}, () => Array(n).fill(undefined));
20+
let [row, col] = [0, 0], counter = 1, dIdx = 0;
21+
while(true) {
22+
result[row][col] = counter++;
23+
let [newRow, newCol] = getNextIdx(row, col, dIdx);
24+
if (!isValidIdx(newRow, newCol, n) || isOccupied(result, newRow, newCol)) {
25+
// Update the direction
26+
dIdx = (dIdx + 1) % 4;
27+
[newRow, newCol] = getNextIdx(row, col, dIdx);
28+
29+
// Check if all items are filled.
30+
if (!isValidIdx(newRow, newCol, n) || isOccupied(result, newRow, newCol)) break;
31+
}
32+
33+
row = newRow; col = newCol;
34+
}
35+
36+
return result;
37+
};
38+
39+
function isValidIdx(row: number, col: number, max: number) {
40+
return row >= 0 && row < max && col >= 0 && col < max;
41+
}
42+
43+
function isOccupied(result: number[][], row: number, col: number) {
44+
return result[row][col] !== undefined;
45+
}
46+
47+
function getNextIdx(row: number, col: number, dIdx: number): number[] {
48+
const directions: number[][] = [
49+
[0, 1], [1, 0], [0, -1], [-1, 0]
50+
];
51+
const direction = directions[dIdx];
52+
return [row + direction[0], col + direction[1]]
53+
}

0 commit comments

Comments
 (0)