Skip to content

Commit a6f8f56

Browse files
committed
Leetcode submissions
1 parent 9dad54f commit a6f8f56

18 files changed

+283
-5
lines changed

angle-between-hands-of-a-clock.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution:
2+
def angleClock(self, hour: int, minutes: int) -> float:
3+
mAngle = minutes * 6
4+
hAngle = (hour * 30 + minutes / 2) % 360
5+
diff = abs(mAngle - hAngle)
6+
return min(diff, 360 - diff)

compare-version-numbers.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def compareVersion(self, version1: str, version2: str) -> int:
3+
a = [int(r) for r in version1.split(".")]
4+
b = [int(r) for r in version2.split(".")]
5+
if len(a) < len(b):
6+
a += [0] * (len(b) - len(a))
7+
else:
8+
b += [0] * (len(a) - len(b))
9+
if tuple(a) > tuple(b):
10+
return 1
11+
elif tuple(a) < tuple(b):
12+
return -1
13+
return 0

copy-list-with-random-pointer.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,29 @@ class Solution:
1111
def copy(self, head):
1212
newhead = None
1313
if head:
14-
newhead = self.visited.get(head, Node(head.val))
15-
self.visited[head] = newhead
14+
newhead = self.visited.get(head.val, Node(head.val))
15+
self.visited[newhead.val] = newhead
1616
if head.random != None:
17-
newhead.random = self.visited.get(head.random, Node(head.random.val))
18-
self.visited[head.random] = newhead.random
17+
newhead.random = self.visited.get(head.random.val, Node(head.random.val))
18+
self.visited[newhead.random.val] = newhead.random
1919
newhead.next = self.copy(head.next)
2020
return newhead
2121

2222
def copyRandomList(self, head: 'Optional[Node]') -> 'Optional[Node]':
2323
self.visited = {}
24-
return self.copy(head)
24+
i = 0
25+
vals = []
26+
curr = head
27+
while curr:
28+
vals.append(curr.val)
29+
curr.val = i
30+
curr = curr.next
31+
i += 1
32+
newhead = self.copy(head)
33+
i = 0
34+
curr = newhead
35+
while curr:
36+
curr.val = vals[i]
37+
curr = curr.next
38+
i += 1
39+
return newhead

course-schedule-ii.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution:
2+
def topologicalSort(self, graph, node, visited):
3+
visited.add(node)
4+
for j in graph.get(node, []):
5+
if j not in visited:
6+
self.topologicalSort(graph, j, visited)
7+
self.sorted.append(node)
8+
9+
def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:
10+
graph = {}
11+
for a, b in prerequisites:
12+
graph[b] = graph.get(b, []) + [a]
13+
self.sorted = []
14+
visited = set()
15+
for node in range(numCourses):
16+
if node not in visited:
17+
self.topologicalSort(graph, node, visited)
18+
self.sorted.reverse()
19+
pos = {}
20+
for i, node in enumerate(self.sorted):
21+
pos[node] = i
22+
for i in graph:
23+
for j in graph[i]:
24+
if pos[i] > pos[j]:
25+
return []
26+
return self.sorted

course-schedule.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution:
2+
def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
3+
graph = {}
4+
indegree = [0] * numCourses
5+
for a, b in prerequisites:
6+
graph[b] = graph.get(b, []) + [a]
7+
indegree[a] += 1
8+
queue = []
9+
for i in range(numCourses):
10+
if indegree[i] == 0:
11+
queue.append(i)
12+
ctr = 0
13+
while len(queue) > 0:
14+
curr = queue.pop(0)
15+
for j in graph.get(curr, []):
16+
indegree[j] -= 1
17+
if indegree[j] == 0:
18+
queue.append(j)
19+
ctr += 1
20+
if ctr < numCourses:
21+
return False
22+
return True

delete-leaves-with-a-given-value.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def removeLeafNodes(self, root: Optional[TreeNode], target: int) -> Optional[TreeNode]:
9+
if root:
10+
if not root.left and not root.right and root.val == target:
11+
return None
12+
root.left = self.removeLeafNodes(root.left, target)
13+
root.right = self.removeLeafNodes(root.right, target)
14+
if not root.left and not root.right and root.val == target:
15+
return None
16+
return root
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from collections import Counter
2+
3+
class Solution:
4+
def countCharacters(self, words: List[str], chars: str) -> int:
5+
chars = Counter(chars)
6+
l = 0
7+
for w in words:
8+
curr = Counter(w)
9+
possible = True
10+
for c in curr:
11+
if chars[c] < curr[c]:
12+
possible = False
13+
break
14+
if possible:
15+
l += len(w)
16+
return l

flower-planting-with-no-adjacent.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution:
2+
def gardenNoAdj(self, n: int, paths: List[List[int]]) -> List[int]:
3+
types = [1] * n
4+
graph = {}
5+
for a, b in paths:
6+
graph[a - 1] = graph.get(a - 1, []) + [b - 1]
7+
graph[b - 1] = graph.get(b - 1, []) + [a - 1]
8+
visited = set()
9+
stack = list(range(n))
10+
while len(stack) > 0:
11+
curr = stack.pop()
12+
visited.add(curr)
13+
colors = set()
14+
for j in graph.get(curr, []):
15+
colors.add(types[j])
16+
if j not in visited:
17+
stack.append(j)
18+
if types[curr] in colors:
19+
for i in range(1, n + 1):
20+
if i not in colors:
21+
types[curr] = i
22+
break
23+
return types

largest-number.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from functools import cmp_to_key
2+
3+
class Solution:
4+
def compare(self, a, b):
5+
if a + b > b + a:
6+
return -1
7+
return 1
8+
9+
def largestNumber(self, nums: List[int]) -> str:
10+
return str(int("".join(sorted([str(num) for num in nums], key = cmp_to_key(self.compare)))))

maximal-network-rank.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def maximalNetworkRank(self, n: int, roads: List[List[int]]) -> int:
3+
l = len(roads)
4+
edges = [set() for _ in range(n)]
5+
for k in range(l):
6+
a, b = roads[k]
7+
edges[a].add(k)
8+
edges[b].add(k)
9+
mnetwork = 0
10+
for i in range(n):
11+
for j in range(i + 1, n):
12+
mnetwork = max(mnetwork, len(set.union(edges[i], edges[j])))
13+
return mnetwork

0 commit comments

Comments
 (0)