Skip to content

Commit f80aad1

Browse files
committed
Clone Graph/ Gas Station
1 parent 8a996c4 commit f80aad1

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

133 Clone Graph.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
'''
2+
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.
3+
4+
5+
OJ's undirected graph serialization:
6+
Nodes are labeled uniquely.
7+
8+
We use # as a separator for each node, and , as a separator for node label and each neighbor of the node.
9+
As an example, consider the serialized graph {0,1,2#1,2#2,2}.
10+
11+
The graph has a total of three nodes, and therefore contains three parts as separated by #.
12+
13+
First node is labeled as 0. Connect node 0 to both nodes 1 and 2.
14+
Second node is labeled as 1. Connect node 1 to node 2.
15+
Third node is labeled as 2. Connect node 2 to node 2 (itself), thus forming a self-cycle.
16+
Visually, the graph looks like the following:
17+
18+
1
19+
/ \
20+
/ \
21+
0 --- 2
22+
/ \
23+
\_/
24+
'''
25+
26+
# Definition for a undirected graph node
27+
class UndirectedGraphNode(object):
28+
def __init__(self, x):
29+
self.label = x
30+
self.neighbors = []
31+
32+
33+
class Solution(object):
34+
def cloneGraph(self, node):
35+
"""
36+
:type node: UndirectedGraphNode
37+
:rtype: UndirectedGraphNode
38+
"""
39+
if not node:
40+
return node
41+
visited = {}
42+
first = UndirectedGraphNode(node.label)
43+
visited[node.label] = first
44+
stack = [node]
45+
while stack:
46+
top = stack.pop()
47+
for n in top.neighbors:
48+
if n.label not in visited:
49+
visited[n.label] = UndirectedGraphNode(n.label)
50+
stack.append(n)
51+
visited[top.label].neighbors.append(visited[n.label])
52+
return first
53+
54+
55+
if __name__ == "__main__":
56+
None

134 Gas Station.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'''
2+
There are N gas stations along a circular route, where the amount of gas at station i is gas[i].
3+
4+
You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations.
5+
6+
Return the starting gas station's index if you can travel around the circuit once, otherwise return -1.
7+
8+
Note:
9+
The solution is guaranteed to be unique.
10+
'''
11+
12+
class Solution(object):
13+
def canCompleteCircuit(self, gas, cost):
14+
"""
15+
:type gas: List[int]
16+
:type cost: List[int]
17+
:rtype: int
18+
"""
19+
if sum(gas) < sum(cost):
20+
return -1
21+
min_sum, min_index, total = 0, 0, 0
22+
for i in range(len(gas)):
23+
total += gas[i] - cost[i]
24+
if min_sum > total:
25+
min_sum, min_index = total, i + 1
26+
return -1 if total < 0 else min_index
27+
28+
29+
if __name__ == "__main__":
30+
assert Solution().canCompleteCircuit([5], [4]) == 0
31+
assert Solution().canCompleteCircuit([5, 1, 2, 3, 4], [4, 4, 1, 5, 1]) == 4

0 commit comments

Comments
 (0)