Skip to content

Commit e38ad98

Browse files
authored
Merge pull request vJechsmayr#177 from divyhshah/master
Solution to 0054 Spiral Matrix
2 parents baf07ac + e53f704 commit e38ad98

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution:
2+
def trap(self, height: List[int]) -> int:
3+
n = len(height)
4+
if n>0:
5+
left = [0]*n
6+
right = [0]*n
7+
result = 0
8+
9+
# Fill left array
10+
left[0] = height[0]
11+
for i in range( 1, n):
12+
left[i] = max(left[i-1], height[i])
13+
14+
# Fill right array
15+
right[n-1] = height[n-1]
16+
for i in range(n-2, -1, -1):
17+
right[i] = max(right[i + 1], height[i]);
18+
19+
for i in range(0, n):
20+
result += min(left[i], right[i]) - height[i]
21+
22+
return result
23+
else:
24+
return 0

LeetCode/0054_Spiral_Matrix.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution:
2+
def spiralOrder(self, matrix):
3+
if len(matrix)>0:
4+
k, l = 0, 0
5+
m = len(matrix)
6+
n = len(matrix[0])
7+
8+
l1 = []
9+
while (k < m and l < n):
10+
11+
for i in range(l, n):
12+
l1.append(matrix[k][i])
13+
14+
k += 1
15+
16+
for i in range(k, m):
17+
l1.append(matrix[i][n - 1])
18+
n -= 1
19+
20+
if (k < m):
21+
22+
for i in range(n - 1, (l - 1), -1):
23+
l1.append(matrix[m - 1][i])
24+
25+
m -= 1
26+
27+
if (l < n):
28+
for i in range(m - 1, k - 1, -1):
29+
l1.append(matrix[i][l])
30+
31+
l += 1
32+
return l1
33+
34+
35+

0 commit comments

Comments
 (0)