Skip to content

Commit 793b97d

Browse files
committed
update
1 parent 077a0e3 commit 793b97d

File tree

2 files changed

+181
-0
lines changed

2 files changed

+181
-0
lines changed

1143 Longest Common Subsequence.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/python3
2+
"""
3+
Given two strings text1 and text2, return the length of their longest common subsequence. If there is no common subsequence, return 0.
4+
5+
A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.
6+
7+
For example, "ace" is a subsequence of "abcde".
8+
A common subsequence of two strings is a subsequence that is common to both strings.
9+
10+
11+
12+
Example 1:
13+
14+
Input: text1 = "abcde", text2 = "ace"
15+
Output: 3
16+
Explanation: The longest common subsequence is "ace" and its length is 3.
17+
Example 2:
18+
19+
Input: text1 = "abc", text2 = "abc"
20+
Output: 3
21+
Explanation: The longest common subsequence is "abc" and its length is 3.
22+
Example 3:
23+
24+
Input: text1 = "abc", text2 = "def"
25+
Output: 0
26+
Explanation: There is no such common subsequence, so the result is 0.
27+
28+
29+
Constraints:
30+
31+
1 <= text1.length, text2.length <= 1000
32+
text1 and text2 consist of only lowercase English characters.
33+
"""
34+
class Solution:
35+
def longestCommonSubsequence(self, a: str, b: str) -> int:
36+
"""
37+
Let F_{i, j} be the longest common subsequence of a[:i] and b[:j]
38+
"""
39+
m, n = len(a), len(b)
40+
F = [
41+
[0 for _ in range(n+1)]
42+
for _ in range(m+1)
43+
]
44+
for i in range(1, m+1):
45+
for j in range(1, n+1):
46+
if a[i-1] == b[j-1]:
47+
F[i][j] = F[i-1][j-1] + 1
48+
else:
49+
F[i][j] = max(F[i-1][j], F[i][j-1])
50+
51+
return F[m][n]
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
#!/usr/bin/python3
2+
"""
3+
You are given an integer n and a 2D integer array queries.
4+
5+
There are n cities numbered from 0 to n - 1. Initially, there is a unidirectional road from city i to city i + 1 for all 0 <= i < n - 1.
6+
7+
queries[i] = [ui, vi] represents the addition of a new unidirectional road from city ui to city vi. After each query, you need to find the length of the shortest path from city 0 to city n - 1.
8+
9+
Return an array answer where for each i in the range [0, queries.length - 1], answer[i] is the length of the shortest path from city 0 to city n - 1 after processing the first i + 1 queries.
10+
11+
12+
13+
Example 1:
14+
15+
Input: n = 5, queries = [[2,4],[0,2],[0,4]]
16+
17+
Output: [3,2,1]
18+
19+
Explanation:
20+
21+
22+
23+
After the addition of the road from 2 to 4, the length of the shortest path from 0 to 4 is 3.
24+
25+
26+
27+
After the addition of the road from 0 to 2, the length of the shortest path from 0 to 4 is 2.
28+
29+
30+
31+
After the addition of the road from 0 to 4, the length of the shortest path from 0 to 4 is 1.
32+
33+
Example 2:
34+
35+
Input: n = 4, queries = [[0,3],[0,2]]
36+
37+
Output: [1,1]
38+
39+
Explanation:
40+
41+
42+
43+
After the addition of the road from 0 to 3, the length of the shortest path from 0 to 3 is 1.
44+
45+
46+
47+
After the addition of the road from 0 to 2, the length of the shortest path remains 1.
48+
49+
50+
51+
Constraints:
52+
53+
3 <= n <= 500
54+
1 <= queries.length <= 500
55+
queries[i].length == 2
56+
0 <= queries[i][0] < queries[i][1] < n
57+
1 < queries[i][1] - queries[i][0]
58+
There are no repeated roads among the queries.
59+
"""
60+
from collections import defaultdict, deque
61+
62+
63+
class Solution:
64+
def shortestDistanceAfterQueries(self, n: int, queries: List[List[int]]) -> List[int]:
65+
G = defaultdict(list)
66+
dist = []
67+
for i in range(n-1):
68+
G[i].append(i+1)
69+
70+
for i in range(n):
71+
dist.append(i)
72+
73+
ret = []
74+
for u, v in queries:
75+
G[u].append(v)
76+
self.bfs(G, dist, u)
77+
ret.append(dist[~0])
78+
79+
return ret
80+
81+
def bfs(self, G, dist, s):
82+
"""
83+
* Known origin and end destination
84+
* dist is the distance from origin, not to destination
85+
* BFS update the distance from the source, where the source is the
86+
start of the new edge, not the origin of the graph
87+
"""
88+
q = deque()
89+
q.append(s)
90+
while q:
91+
v = q.popleft()
92+
for nbr in G[v]:
93+
if dist[nbr] > dist[v] + 1:
94+
# It and its descendants require distance update
95+
dist[nbr] = dist[v] + 1
96+
q.append(nbr)
97+
98+
99+
class SolutionTLE:
100+
def shortestDistanceAfterQueries(self, n: int, queries: List[List[int]]) -> List[int]:
101+
"""
102+
Naive solution:
103+
1. maintain a graph
104+
2. BFS
105+
"""
106+
G = defaultdict(list)
107+
for i in range(n-1):
108+
G[i].append(i+1)
109+
110+
ret = []
111+
for q in queries:
112+
s, t = q
113+
G[s].append(t)
114+
ret.append(self.bfs(G, 0, n - 1))
115+
116+
return ret
117+
118+
def bfs(self, G, s, t):
119+
q = [s]
120+
ret = 0
121+
while q:
122+
nxt_q = []
123+
ret += 1
124+
for v in q:
125+
for nbr in G[v]:
126+
if nbr == t:
127+
return ret
128+
nxt_q.append(nbr)
129+
130+
q = nxt_q

0 commit comments

Comments
 (0)