Skip to content

Commit 75e6c76

Browse files
author
Partho Biswas
committed
59. Spiral Matrix II
1 parent 7047ca3 commit 75e6c76

File tree

4 files changed

+77
-179
lines changed

4 files changed

+77
-179
lines changed

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ I have solved quite a number of problems from several topics. See the below tabl
159159
|47| **[48. Rotate Image](https://tinyurl.com/yy3z9hab)** | [Python](https://tinyurl.com/wu6rdaw/48_Rotate_Image.py)| [Must](https://tinyurl.com/tr62top), [Vid 1](https://tinyurl.com/uhnruzt) | Medium | Very important |
160160
|48| **[362. Design Hit Counter](https://tinyurl.com/sswrcc3)** | [Python](https://tinyurl.com/wu6rdaw/362_Design_Hit_Counter.py), [Swift](https://tinyurl.com/wuja3c4/362_Design_Hit_Counter.swift)| --- | Medium | --- |
161161
|49| **[289. Game of Life](https://tinyurl.com/y5ujyvu5)** | [Python](https://tinyurl.com/wu6rdaw/289_Game_of_Life.py), [Swift](https://tinyurl.com/wuja3c4/289_Game_of_Life.swift)| --- | Medium | --- |
162-
|50| **[54. Spiral Matrix](https://tinyurl.com/yy5rnvce)** | [Python](https://tinyurl.com/wu6rdaw/54_Spiral_Matrix.py), [Swift](https://tinyurl.com/wuja3c4/54_Spiral_Matrix.swift)| [Official](https://tinyurl.com/s6np53k) | Medium | --- |
162+
|50| **[54. Spiral Matrix](https://tinyurl.com/yy5rnvce)** | [Python](https://tinyurl.com/wu6rdaw/54_Spiral_Matrix.py), [Swift](https://tinyurl.com/wuja3c4/54_Spiral_Matrix.swift)| [Official](https://tinyurl.com/s6np53k), [Algoexpert.io](https://www.algoexpert.io/questions/Spiral%20Traverse) | Medium | --- |
163163
|51| **[380. Insert Delete GetRandom O(1)](https://tinyurl.com/y3urnkfj)** | [Python](https://tinyurl.com/wu6rdaw/380_Insert_Delete_GetRandom_O(1).py), [Swift](https://tinyurl.com/wuja3c4/380_Insert_Delete_GetRandom_O(1).swift)| [Official](https://tinyurl.com/t4t38od) | Medium | --- |
164164
|52| **[835. Image Overlap](https://tinyurl.com/r9x9nkm)** | [Python](https://tinyurl.com/wu6rdaw/835_Image_Overlap.py), [Swift](https://tinyurl.com/wuja3c4/835_Image_Overlap.swift)| [Art 1](https://tinyurl.com/tx3gd98), [Art 2](https://tinyurl.com/tnmj6du) | Medium | --- |
165165
|53| **[1051. Height Checker](https://tinyurl.com/scupmnz)** | [Python](https://tinyurl.com/wu6rdaw/1051_Height_Checker.py), [Swift](https://tinyurl.com/wuja3c4/1051_Height_Checker.swift) | --- | Easy | |
@@ -170,6 +170,7 @@ I have solved quite a number of problems from several topics. See the below tabl
170170
|59| **[1089. Duplicate Zeros](https://tinyurl.com/vdlwjwa)** | [Python](https://tinyurl.com/wu6rdaw/1089_Duplicate_Zeros.py), [Swift](https://tinyurl.com/wuja3c4/1089_Duplicate_Zeros.swift) | [Art 1](https://tinyurl.com/y3b2ynd4) | Easy | Not so easy and intuitive. Check again |
171171
|60| [941. Valid Mountain Array](https://tinyurl.com/tgdqrrk) | [Python](https://tinyurl.com/wu6rdaw/941_Valid_Mountain_Array.py), [Swift](https://tinyurl.com/wuja3c4/941_Valid_Mountain_Array.swift) | | Easy | |
172172
|61| [731. My Calendar II](https://tinyurl.com/sde6smv) | [Python](https://tinyurl.com/wu6rdaw/731_My_Calendar_II.py), [Swift](https://tinyurl.com/wuja3c4/731_My_Calendar_II.swift) | | Medium | Merge interval |
173+
|62| [59. Spiral Matrix II](https://tinyurl.com/pqfjvm7) | [Python](https://tinyurl.com/wu6rdaw/59_Spiral_Matrix_II.py), [Swift](https://tinyurl.com/wuja3c4/59_Spiral_Matrix_II.swift) | | Medium | loved it |
173174

174175

175176
</p>
@@ -726,7 +727,7 @@ BFS, DFS, Dijkstra, Floyd–Warshall, Bellman-Ford, Kruskal, Prim's, Minimum Spa
726727

727728
### 1. Arrays
728729

729-
<details><summary>Problem sstatement from AlgoExpert.io with solutions and tutorials/videos</summary>
730+
<details><summary>Problem statement from AlgoExpert.io with solutions and tutorials/videos</summary>
730731
<p>
731732

732733
| # | Title | Solution | Tutorial | Level | Remarks |
@@ -741,6 +742,7 @@ BFS, DFS, Dijkstra, Floyd–Warshall, Bellman-Ford, Kruskal, Prim's, Minimum Spa
741742
|08| [Zigzag_Traverse](algoexpert.io/questions/Zigzag_Traverse.md) | [Python](algoexpert.io/python/Zigzag_Traverse.py) |
742743
|08| [Apartment_Hunting](algoexpert.io/questions/Apartment_Hunting.md) | [Python](algoexpert.io/python/Apartment_Hunting.py) |
743744
|09| [Calendar_Matching](algoexpert.io/questions/Calendar_Matching.md) | [Python](algoexpert.io/python/Calendar_Matching.py) |
745+
|10| [Spiral_Traverse](algoexpert.io/questions/Spiral_Traverse.md) | [Python](algoexpert.io/python/Spiral_Traverse.py) |
744746

745747
</p>
746748
</details>
+13-177
Original file line numberDiff line numberDiff line change
@@ -1,257 +1,93 @@
1-
# https://www.algoexpert.io/questions/Linked%20List%20Construction
2-
# Write a class for a Doubly Linked List.
3-
4-
5-
# Definition for doubly-linked list node.
6-
# class ListNode(object):
7-
# def __init__(self, x):
8-
# self.val = x
9-
# self.next = None
10-
# self.prev = prev
11-
12-
class DoublyLinkedList:
13-
14-
def __init__(self):
15-
self.head = None
16-
self.tail = None
17-
18-
# O(1) time | O(1) space
19-
def set_head(self, node):
20-
if self.head is None:
21-
self.head = node
22-
self.tail = node
23-
return
24-
self.insert_before(self.head, node)
25-
26-
# O(1) time | O(1) space
27-
def set_tail(self, node):
28-
if self.tail is None:
29-
self.set_head(node)
30-
return
31-
self.insert_after(self.tail, node)
32-
33-
# O(1) time | O(1) space
34-
def insert_before(self, node, node_to_insert):
35-
if node_to_insert == self.head and node_to_insert == self.tail:
36-
return
37-
self.remove(node_to_insert)
38-
node_to_insert.prev = node.prev
39-
node_to_insert.next = node
40-
if node.prev is None:
41-
self.head = node_to_insert
42-
else:
43-
node.prev.next = node_to_insert
44-
node.prev = node_to_insert
45-
46-
# O(1) time | O(1) space
47-
def insert_after(self, node, node_to_insert):
48-
if node_to_insert == self.head and node_to_insert == self.tail:
49-
return
50-
self.remove(node_to_insert)
51-
node_to_insert.prev = node
52-
node_to_insert.next = node.next
53-
if node.next is None:
54-
self.tail = node_to_insert
55-
else:
56-
node.next.prev = node_to_insert
57-
node.next = node_to_insert
58-
59-
# O(p) time | O(1) space
60-
def insert_at_position(self, position, node_to_insert):
61-
if position == 1:
62-
self.set_head(node_to_insert)
63-
return
64-
node = self.head
65-
current_position = 1
66-
while node is not None and current_position != position:
67-
node = node.next
68-
current_position += 1
69-
if node is not None:
70-
self.insert_before(node, node_to_insert)
71-
else:
72-
self.set_tail(node)
73-
74-
# O(n) time | O(1) space
75-
def remove_nodes_with_value(self, value):
76-
node = self.head
77-
while node is not None:
78-
node_to_remove = node
79-
node = node.next
80-
if node_to_remove.value == value:
81-
self.remove(node_to_remove)
82-
83-
# O(1) time | O(1) space
84-
def remove(self, node):
85-
if node == self.head:
86-
self.head = self.head.next
87-
if node == self.tail:
88-
self.tail = self.tail.prev
89-
self.remove_node_bindings(node)
90-
91-
def contains_node_with_value(self, value):
92-
node = self.head
93-
while node is not None and node.value != value:
94-
node = node.next
95-
return node is not None
96-
97-
def remove_node_bindings(self, node):
98-
if node.prev is not None:
99-
node.prev.next = node.next
100-
if node.next is not None:
101-
node.next.prev = node.prev
102-
node.next = None
103-
node.prev = None
104-
105-
106-
107-
# -------------------------------------
108-
109-
1+
# This is an input class. Do not edit.
1102
class Node:
1113
def __init__(self, value):
1124
self.value = value
1135
self.prev = None
1146
self.next = None
1157

1168

9+
# Code is OK but test cases are failing. DOn't know why
11710
class DoublyLinkedList:
11811
def __init__(self):
11912
self.head = None
12013
self.tail = None
12114

122-
# O(1) time | O(1) space
12315
def setHead(self, node):
12416
if self.head is None:
12517
self.head = node
12618
self.tail = node
12719
return
12820
self.insertBefore(self.head, node)
12921

130-
# O(1) time | O(1) space
13122
def setTail(self, node):
13223
if self.tail is None:
13324
self.setHead(node)
13425
return
13526
self.insertAfter(self.tail, node)
13627

137-
# O(1) time | O(1) space
13828
def insertBefore(self, node, nodeToInsert):
13929
if nodeToInsert == self.head and nodeToInsert == self.tail:
14030
return
14131
self.remove(nodeToInsert)
14232
nodeToInsert.prev = node.prev
14333
nodeToInsert.next = node
144-
if node.prev is None:
34+
if not node.prev:
14535
self.head = nodeToInsert
14636
else:
14737
node.prev.next = nodeToInsert
14838
node.prev = nodeToInsert
14939

150-
# O(1) time | O(1) space
15140
def insertAfter(self, node, nodeToInsert):
15241
if nodeToInsert == self.head and nodeToInsert == self.tail:
15342
return
15443
self.remove(nodeToInsert)
15544
nodeToInsert.prev = node
15645
nodeToInsert.next = node.next
157-
if node.next is None:
46+
if not node.next:
15847
self.tail = nodeToInsert
15948
else:
16049
node.next.prev = nodeToInsert
16150
node.next = nodeToInsert
16251

163-
# O(p) time | O(1) space
16452
def insertAtPosition(self, position, nodeToInsert):
16553
if position == 1:
16654
self.setHead(nodeToInsert)
16755
return
16856
node = self.head
169-
currentPosition = 1
170-
while node is not None and currentPosition != position:
57+
currPos = 1
58+
while node and currPos != position:
17159
node = node.next
172-
currentPosition += 1
173-
if node is not None:
174-
self.insertBefore(node, nodeToInsert)
60+
currPos += 1
61+
if node:
62+
self.insertBefore(nodeToInsert)
17563
else:
17664
self.setTail(nodeToInsert)
17765

178-
# O(n) time | O(1) space
179-
18066
def removeNodesWithValue(self, value):
18167
node = self.head
182-
while node is not None:
68+
while node:
18369
nodeToRemove = node
18470
node = node.next
18571
if nodeToRemove.value == value:
18672
self.remove(nodeToRemove)
18773

188-
# O(1) time | O(1) space
18974
def remove(self, node):
19075
if node == self.head:
19176
self.head = self.head.next
19277
if node == self.tail:
19378
self.tail = self.tail.prev
19479
self.removeNodeBindings(node)
19580

196-
# O(n) time | O(1) space
19781
def containsNodeWithValue(self, value):
19882
node = self.head
199-
while node is not None and node.value != value:
83+
while node and node.value != value:
20084
node = node.next
20185
return node is not None
20286

20387
def removeNodeBindings(self, node):
204-
if node.prev is not None:
88+
if node.prev:
20589
node.prev.next = node.next
206-
if node.next is not None:
90+
if node.next:
20791
node.next.prev = node.prev
20892
node.prev = None
20993
node.next = None
210-
211-
212-
# ------------------
213-
# This is an input class. Do not edit.
214-
class Node:
215-
def __init__(self, value):
216-
self.value = value
217-
self.prev = None
218-
self.next = None
219-
220-
221-
# Feel free to add new properties and methods to the class.
222-
class DoublyLinkedList:
223-
def __init__(self):
224-
self.head = None
225-
self.tail = None
226-
227-
def setHead(self, node):
228-
# Write your code here.
229-
pass
230-
231-
def setTail(self, node):
232-
# Write your code here.
233-
pass
234-
235-
def insertBefore(self, node, nodeToInsert):
236-
# Write your code here.
237-
pass
238-
239-
def insertAfter(self, node, nodeToInsert):
240-
# Write your code here.
241-
pass
242-
243-
def insertAtPosition(self, position, nodeToInsert):
244-
# Write your code here.
245-
pass
246-
247-
def removeNodesWithValue(self, value):
248-
# Write your code here.
249-
pass
250-
251-
def remove(self, node):
252-
# Write your code here.
253-
pass
254-
255-
def containsNodeWithValue(self, value):
256-
# Write your code here.
257-
pass
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
def spiralTraverse(matrix):
2+
result = []
3+
sR, eR, sC, eC = 0, len(matrix) - 1, 0, len(matrix[0]) - 1
4+
while sR <= eR and sC <= eC:
5+
for i in range(sC, eC + 1):
6+
result.append(matrix[sR][i])
7+
8+
for i in range(sR + 1, eR + 1):
9+
result.append(matrix[i][eC])
10+
11+
for i in range(eC - 1, sC - 1, -1):
12+
if sR == eR:
13+
break
14+
result.append(matrix[eR][i])
15+
16+
for i in range(eR - 1, sR, -1):
17+
if sC == eC:
18+
break
19+
result.append(matrix[i][sC])
20+
21+
sR += 1
22+
eR -= 1
23+
sC += 1
24+
eC -= 1
25+
26+
return result
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution(object):
2+
def generateMatrix(self, n):
3+
values = [i for i in range(1, n * n + 1)]
4+
matrix = [[0 for _ in range(n)] for _ in range(n)]
5+
sR, eR, sC, eC = 0, n - 1, 0, n - 1
6+
7+
valueIdx = 0
8+
while sR <= eR and sC <= eC:
9+
for i in range(sC, eC + 1):
10+
matrix[sR][i] = values[valueIdx]
11+
valueIdx += 1
12+
13+
for i in range(sR + 1, eR + 1):
14+
matrix[i][eC] = values[valueIdx]
15+
valueIdx += 1
16+
17+
for i in range(eC - 1, sC - 1, -1):
18+
if sR == eR:
19+
break
20+
matrix[eR][i] = values[valueIdx]
21+
valueIdx += 1
22+
23+
for i in range(eR - 1, sR, -1):
24+
if sC == eC:
25+
break
26+
matrix[i][sC] = values[valueIdx]
27+
valueIdx += 1
28+
29+
sR += 1
30+
eR -= 1
31+
sC += 1
32+
eC -= 1
33+
34+
return matrix

0 commit comments

Comments
 (0)