Skip to content

Commit 91b049e

Browse files
committed
Leetcode accepted
1 parent 093a533 commit 91b049e

2 files changed

Lines changed: 66 additions & 2 deletions

File tree

Blind 75/Programs/Spiral Matrix.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Spiral Matrix
2+
# https://leetcode.com/problems/spiral-matrix/
3+
4+
class Boundries(object):
5+
def __init__(self):
6+
self.rowStart = None
7+
self.rowEnd = None
8+
self.colStart = None
9+
self.colEnd = None
10+
11+
class Solution(object):
12+
13+
# shrink boundries of the matrix for spiral print.
14+
def setBoundries(self, matrix, decreaseBy):
15+
boundries = Boundries()
16+
# set row boundry
17+
boundries.rowStart = 0 + decreaseBy
18+
boundries.rowEnd = len(matrix) - decreaseBy
19+
# set column boundry
20+
boundries.colStart = 0 + decreaseBy
21+
boundries.colEnd = len(matrix[0]) - decreaseBy
22+
return boundries
23+
24+
def markCellVisitedAddToSpiralPrint(self, matrix, row, col, spiralPrint):
25+
# if the cell has not been visited
26+
if (matrix[row][col] != "visited"):
27+
spiralPrint.append(matrix[row][col])
28+
# mark the cell visited
29+
matrix[row][col] = "visited"
30+
31+
def printSpiral(self, matrix, boundries, spiralPrint):
32+
# print first row
33+
for col in range(boundries.colStart, boundries.colEnd):
34+
self.markCellVisitedAddToSpiralPrint(matrix, boundries.rowStart, col, spiralPrint)
35+
# print last col
36+
for row in range(boundries.rowStart+1, boundries.rowEnd-1):
37+
self.markCellVisitedAddToSpiralPrint(matrix, row, boundries.colEnd-1, spiralPrint)
38+
# print last row in reverse
39+
for col in range(boundries.colEnd-1, boundries.colStart-1, -1):
40+
self.markCellVisitedAddToSpiralPrint(matrix, boundries.rowEnd-1, col, spiralPrint)
41+
# print first col
42+
for row in range(boundries.rowEnd-2, boundries.rowStart, -1):
43+
self.markCellVisitedAddToSpiralPrint(matrix, row, boundries.colStart, spiralPrint)
44+
45+
def spiralOrder(self, matrix):
46+
spiralPrint = []
47+
boundryIndex = 0
48+
boundries = self.setBoundries(matrix, boundryIndex)
49+
# shirnk boundries until bounds are equal.
50+
while(boundries.rowStart < boundries.rowEnd and boundries.colStart < boundries.colEnd):
51+
boundryIndex = boundryIndex + 1
52+
self.printSpiral(matrix, boundries, spiralPrint)
53+
boundries = self.setBoundries(matrix, boundryIndex)
54+
return spiralPrint
55+
"""
56+
:type matrix: List[List[int]]
57+
:rtype: List[int]
58+
"""
59+

Blind 75/README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<h1>Blind 75</h1>
22
<a href="https://leetcode.com/discuss/general-discussion/460599/blind-75-leetcode-questions">Leetcode link to the post.</a>
3-
<h4>Total Completed: 25</h4>
3+
<h4>Total Completed: 27</h4>
44
<h2>Array</h2>
55
<ul>
66
<li><a href="Programs/Two Sum.py">Two Sum</a>
@@ -201,7 +201,12 @@
201201
This ensures we use no extra space and do it in O(n) time.
202202
</p>
203203
</li>
204-
<li>Spiral Matrix</li>
204+
<li><a href="Programs/Spiral Matrix.py">Spiral Matrix</a>
205+
<p><b>Approach</b>: Had to see this idea from leetcode solutions. <br/>
206+
First write a function that first prints the spiral of rows and cols, then just reduce the bounds and do the same. <br/>
207+
Don't forget the mark the cells visited to avoid duplicates.
208+
</p>
209+
</li>
205210
<li>Rotate Image</li>
206211
<li>Word Search</li>
207212
</ul>

0 commit comments

Comments
 (0)