Skip to content

Commit fa8ab46

Browse files
Chris WuChris Wu
Chris Wu
authored and
Chris Wu
committed
no message
1 parent 618703a commit fa8ab46

File tree

2 files changed

+103
-8
lines changed

2 files changed

+103
-8
lines changed

longest-common-prefix.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
#https://leetcode.com/problems/longest-common-prefix/
2+
"""
3+
We ramdomly pick a string from 'strs' as 'bench_mark' [0]
4+
Lets say the 'bench_mark' is 'flower'
5+
We compair 'f', 'fl', 'flo'... to every other string [1]
6+
If any string does not match, we return the longest common prefix [2]
7+
In ths case the whole 'bench_mark' is actually the longest common prefix [3]
8+
We need to return the 'bench_mark'
9+
10+
11+
"""
212
class Solution(object):
313
def longestCommonPrefix(self, strs):
414
if len(strs)==0 or strs==None:
515
return ''
616

7-
bench_mark = strs[0]
17+
bench_mark = strs[0] #[0]
818

9-
for i in range(1, len(bench_mark)+1):
19+
for i in range(1, len(bench_mark)+1): #[1]
1020
common_substring = bench_mark[:i]
1121
for s in strs:
12-
if s[:i]!=common_substring:
13-
if i==1:
14-
return ''
15-
else:
16-
return bench_mark[:i-1]
17-
return bench_mark
22+
if s[:i]!=common_substring: #[2]
23+
return bench_mark[:i-1]
24+
return bench_mark #[3]

minimize-malware-spread.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
"""
2+
Lets start from a node in the initial. DFS through the graph. [0]
3+
When a node is infected, we paint it by color1.
4+
After the DFS is done, we start from another node in the initial. DFS through the graph.
5+
When a node is infected, we paint it by color2.
6+
...
7+
8+
We don't paint the node that we already colored. [1]
9+
The more node that are colored by an initial node the more affective it will minimize the malware. [2]
10+
But if a color have two or more initial node, they won't make any difference if taken away. [3]
11+
Because those node are still going to be infected by one another initial node.
12+
13+
So our goal here is to find the initial node that paint the most.
14+
But did not paint other intial node.
15+
16+
I use 'color_data' to store the result [4]
17+
{
18+
color1: [
19+
[intial nodes in this color],
20+
the number of node in this color
21+
],
22+
color2: [
23+
...
24+
],
25+
color3: [
26+
...
27+
],
28+
...
29+
}
30+
31+
By 'color_data' I can easily see the things that I care about and calculate the answer
32+
1. The intial nodes in this color
33+
2. The number of node in this color
34+
35+
The time complexity is O(I*N),
36+
because we loop through the initial nodes.
37+
And each node, it could potential travel all the nodes.
38+
I is the initial nodes count, N is the nodes count.
39+
40+
Space complexity is O(N), because we use colored to keep track of all the nodes.
41+
"""
42+
43+
44+
class Solution(object):
45+
def minMalwareSpread(self, graph, initial):
46+
colored = set()
47+
initial_set = set(initial)
48+
color_data = {} #[4]
49+
color = 0
50+
51+
def dfs(node, c):
52+
stack = [node]
53+
while stack:
54+
n = stack.pop()
55+
if n in colored: continue #[1]
56+
colored.add(n)
57+
58+
if n in initial_set:
59+
color_data[c][0].append(n)
60+
color_data[c][1]+=1
61+
62+
for nb in xrange(len(graph)):
63+
if graph[n][nb]==1:
64+
stack.append(nb)
65+
66+
# [0]
67+
for node in initial:
68+
if color not in color_data:
69+
color_data[color] = [[], 0]
70+
71+
dfs(node, color)
72+
color+=1
73+
74+
ans = min(initial)
75+
max_infected = float('-inf')
76+
for c in color_data.keys():
77+
if len(color_data[c][0])!=1: continue #[3]
78+
n = color_data[c][0][0]
79+
infected = color_data[c][1]
80+
81+
if color_data[c][1]>max_infected: #[2]
82+
max_infected = infected
83+
ans = n
84+
elif infected==max_infected and n<ans:
85+
ans = n
86+
87+
return ans
88+

0 commit comments

Comments
 (0)