Datasets:

inputs
stringlengths
50
14k
targets
stringlengths
4
655k
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
from functools import lru_cache class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: len_a = len(A) len_b = len(B) @lru_cache(None) def helper(i, j): nonlocal A, B, len_a, len_b, b_index_data if i >= len_a or j >= len_b: return 0 ans = float('-inf') for x in b_index_data.get(A[i], []): if x >= j: ans = 1 + helper(i + 1, x + 1) break return max(ans, helper(i + 1, j)) b_index_data = {} for i, val in enumerate(B): l = b_index_data.setdefault(val, []) l.append(i) return helper(0, 0)
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: if len(A) <len(B): A,B = B,A n = len(B) prev = [0] * (n+1) for i in range(1,n+1): if A[0] == B[i-1]: prev[i] = 1 else: prev[i] = prev[i-1] curr = [0] * (n+1) for i in range(1,len(A)): for j in range(1,n+1): if A[i] == B[j-1]: curr[j] = 1+prev[j-1] else: curr[j] = max(curr[j-1],prev[j]) prev,curr = curr,prev return prev[-1]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: prev, crnt = [0] * (1 + len(B)), [0] * (1 + len(B)) for a in range(len(A)): prev, crnt = crnt, prev for b in range(len(B)): if A[a] == B[b]: crnt[b + 1] = prev[b] + 1 else: crnt[b + 1] = max(crnt[b], prev[b + 1]) return crnt[len(B)]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: if len(A)<len(B): A, B=B, A DP=[0]* (len(A)+1) coll={j:[-1] for j in set(B)} for i in range(len(A)): if A[i] in coll: coll[A[i]].append(i) for iB in B[::-1]: for i in range(1, len(coll[iB])): if DP[coll[iB][i]]<DP[coll[iB][i]+1]+1: DP[coll[iB][i]] = DP[coll[iB][i]+1]+1 for j in range(coll[iB][i], coll[iB][i-1], -1): DP[j]=max(DP[j], DP[j+1]) return DP[0]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: ga = collections.defaultdict(list) # gb = collections.defaultdict(list) for i, v in enumerate(A): ga[v].append(i) # for j, v in enumerate(B): # gb[v].append(j) def pd(i, start_a): if i >= len(B) or start_a >= len(A): return 0 if (i, start_a) in memo: return memo[(i, start_a)] res = pd(i + 1, start_a) if B[i] in ga: new_start_a = -1 for cand in ga[B[i]]: if cand > start_a: new_start_a = cand break if new_start_a >= start_a: res = max(res, 1 + pd(i + 1, new_start_a)) memo[(i, start_a)] = res return res memo = dict() return pd(0, -1)
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: m = len(A) n = len(B) dp = [[0] * (n+1) for _ in range(m+1)] # dp[0][0] = 1 for i in range(1, m+1): if A[i-1] == B[0]: dp[i][1] = 1 for j in range(1, n+1): if B[j-1] == A[0]: dp[1][j] = 1 for i, a in enumerate(A, 1): for j, b in enumerate(B, 1): if a == b: dp[i][j] = dp[i-1][j-1] + 1 else: dp[i][j] = max(dp[i-1][j], dp[i][j-1]) return dp[-1][-1]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: M, N = len(A), len(B) dp = [0]*(N+1) res = 0 for i in range(1, M+1): dp2 = [0]*(N+1) for j in range(1, N+1): if A[i-1]==B[j-1]: dp2[j] = dp[j-1]+1 else: dp2[j] = max(dp[j], dp2[j-1]) dp = dp2 res = max(res, max(dp)) return res
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: dp = [[0] * (len(B)+1) for _ in range(2)] flag = 1 for i in range(len(A)-1, -1, -1): for j in range(len(B)-1, -1, -1): if A[i] == B[j]: dp[flag][j] = dp[flag^1][j+1] + 1 else: dp[flag][j] = max(dp[flag][j+1], dp[flag^1][j]) flag ^= 1 return dp[flag^1][0]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: A = [-1] + A B = [-1] + B lenA, lenB = len(A), len(B) dp = [[0] * lenA for _ in range(lenB)] for y in range(1, lenB): for x in range(1, lenA): if A[x] == B[y]: dp[y][x] = dp[y-1][x-1] + 1 else: dp[y][x] = max(dp[y-1][x], dp[y][x-1]) return dp[-1][-1] # A = [ -1 ] + A # B = [ -1 ] + B # h, w = len(A), len(B) # dp_table = [ [ 0 for _ in range(w) ] for _ in range(h) ] # for y in range(1, h): # for x in range(1, w): # if A[y] == B[x]: # # current number is matched, add one more uncrossed line # dp_table[y][x] = dp_table[y-1][x-1] + 1 # else: # # cuurent number is not matched, backtracking to find maximal uncrossed line # dp_table[y][x] = max( dp_table[y][x-1], dp_table[y-1][x] ) # return dp_table[-1][-1]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: l,w = len(A), len(B) dp = [[0]*(w+1) for _ in range(l+1)] for i in range(l): for x in range(w): if A[i] == B[x]: dp[i+1][x+1] = dp[i][x] + 1 else: dp[i+1][x+1] = max(dp[i+1][x], dp[i][x+1]) return dp[-1][-1]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: if(len(A)==0 or len(B)==0): return 0 A = [ -1 ] + A B = [ -1 ] + B res = [ [ 0 for _ in range(len(B)) ] for _ in range(len(A)) ] for i in range(1,len(A)): for j in range(1,len(B)): if(A[i]==B[j]): res[i][j]=res[i-1][j-1]+1 else: res[i][j]=max(res[i-1][j],res[i][j-1]) return res[-1][-1]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: # A = [-1] + A # B = [-1] + B # lenA, lenB = len(A), len(B) # dp = [[0] * lenB for _ in range(lenA)] # for y in range(lenA): # for x in range(lenB): # if A[y] == B[x]: # dp[y][x] = dp[y-1][x-1] + 1 # else: # dp[y][x] = max(dp[y-1][x], dp[y][x-1]) # return dp[-1][-1] A = [ -1 ] + A B = [ -1 ] + B h, w = len(A), len(B) dp_table = [ [ 0 for _ in range(w) ] for _ in range(h) ] for y in range(1, h): for x in range(1, w): if A[y] == B[x]: # current number is matched, add one more uncrossed line dp_table[y][x] = dp_table[y-1][x-1] + 1 else: # cuurent number is not matched, backtracking to find maximal uncrossed line dp_table[y][x] = max( dp_table[y][x-1], dp_table[y-1][x] ) return dp_table[-1][-1]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: # padding one dummy -1 to represent empty list A = [ -1 ] + A B = [ -1 ] + B h, w = len(A), len(B) dp_table = [ [ 0 for _ in range(w) ] for _ in range(h) ] for y in range(1, h): for x in range(1, w): if A[y] == B[x]: # current number is matched, add one more uncrossed line dp_table[y][x] = dp_table[y-1][x-1] + 1 else: # cuurent number is not matched, backtracking to find maximal uncrossed line dp_table[y][x] = max( dp_table[y][x-1], dp_table[y-1][x] ) return dp_table[-1][-1]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: dp = [[0 for b in range(len(B)+1)] for a in range(len(A)+1)] for a in range(1, len(A)+1): for b in range(1, len(B)+1): if A[a-1] == B[b-1]: dp[a][b] = dp[a-1][b-1] + 1 else: dp[a][b] = max(dp[a-1][b], dp[a][b-1]) return dp[-1][-1]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A, B): # Optimization #commons = set(A).intersection(set(B)) # or commons = set(A) & set(B) #A = [x for x in A if x in commons] #B = [x for x in B if x in commons] N1, N2 = len(A), len(B) dp = [[0 for _ in range(N2+1)] for _ in range(N1+1)] for i1, v1 in enumerate(A, start = 1): for i2, v2 in enumerate(B, start = 1): if v1 == v2: dp[i1][i2] = dp[i1-1][i2-1] + 1 else: dp[i1][i2] = max(dp[i1-1][i2], dp[i1][i2-1]) return dp[N1][N2] class Solution: def maxUncrossedLines(self, A, B): commons = set(A).intersection(set(B)) # or commons = set(A) & set(B) A = [x for x in A if x in commons] B = [x for x in B if x in commons] N1, N2 = len(A), len(B) dp = [0 for _ in range(N2+1)] for i1, v1 in enumerate(A, start = 1): tmp = [0 for _ in range(N2+1)] for i2, v2 in enumerate(B, start = 1): if v1 == v2: tmp[i2] = dp[i2-1] + 1 else: tmp[i2] = max(dp[i2], tmp[i2-1]) dp = tmp return dp[N2]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: lenA, lenB = len(A), len(B) dp = [[0 for _ in range(lenB+1)] for _ in range(lenA + 1) ] for i in range(lenA): for j in range(lenB): if A[i]==B[j]: dp[i+1][j+1] = 1 + dp[i][j] else: dp[i+1][j+1] = max(dp[i][j+1], dp[i+1][j]) #print(dp) return dp[lenA][lenB]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A, B): dp = [[0 for j in range(len(B)+1)] for i in range(len(A)+1)] for i in range(1,len(A)+1): for j in range(1,len(B)+1): if A[i-1] == B[j-1]: dp[i][j] = dp[i-1][j-1] + 1 else: dp[i][j] = max(dp[i-1][j],dp[i][j-1]) return dp[-1][-1]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: len_a = len(A)+1 len_b = len(B)+1 dp = [[0 for i in range(len_b)] for i in range(len_a)] for i in range(1, len_a): for j in range(1, len_b): if A[i - 1] == B[j - 1]: dp[i][j] = dp[i - 1][j - 1] + 1 else: dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) return dp[len_a-1][len_b-1]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: # padding one dummy -1 to represent empty list A = [ 1 ] + A B = [ 1 ] + B h, w = len(A), len(B) dp_table = [ [ 0 for _ in range(w) ] for _ in range(h) ] for y in range(1, h): for x in range(1, w): if A[y] == B[x]: # current number is matched, add one more uncrossed line dp_table[y][x] = dp_table[y-1][x-1] + 1 else: # cuurent number is not matched, backtracking to find maximal uncrossed line dp_table[y][x] = max( dp_table[y][x-1], dp_table[y-1][x] ) print (dp_table) return dp_table[-1][-1]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: # idea: 0. dp[i, j] is the max number of uncrossed lines in A[0,i] and B[0,j] # 1. init: fill dp with 0 # 2. transition function: loop(expand) B for each in A # dp[i][j] = max(dp[i-1][j],dp[i][j-1]) to have at least the same number supposing when A[i] was expanded, it won't be connected; or B[j] was expanded, it won't be connected; # then if B[j-1] == A[i-1], dp[i][j] = max(dp[i][j], dp[i-1][j-1] + 1) # time: O( len(A) x len(B) ) # space:O( len(A) x len(B) ) dp: List[List[int]] = [[0 for i in range(len(B)+1)] for j in range(len(A)+1)] for i in range(1, len(A)+1): for j in range(1, len(B)+1): dp[i][j] = max(dp[i-1][j],dp[i][j-1]) if B[j-1] == A[i-1]: dp[i][j] = max(dp[i][j], dp[i-1][j-1] + 1) return dp[len(A)][len(B)]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: La = len(A) Lb = len(B) dp = [[0]*Lb for i in range(La)] for i in range(La): for j in range(Lb): if i==0: if A[i]==B[j]: dp[i][j] = 1 elif j!=0: dp[i][j] = dp[i][j-1] else: if j==0: if A[i]==B[j]: dp[i][j] = 1 else: dp[i][j] = dp[i-1][j] else: if A[i]==B[j]: dp[i][j] = dp[i-1][j-1] + 1 else: dp[i][j] = dp[i-1][j] dp[i][j] = max(dp[i][j],dp[i][j-1]) #print(dp) return dp[-1][-1]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: dp = [[0] * (len(B) + 1) for _ in range(len(A) + 1)] for i in range(len(A)): for j in range(len(B)): dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j], dp[i][j] + (A[i] == B[j])) return dp[-1][-1]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: rows = len(A) cols = len(B) dp = [[0 for i in range(cols+1)] for j in range(rows+1)] for i in range(rows): for j in range(cols): if A[i]==B[j]: dp[i+1][j+1]= 1 + dp[i][j] else: dp[i+1][j+1]= max(dp[i][j+1],dp[i+1][j],dp[i][j]) return dp[rows][cols]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: m=len(A) n=len(B) dp=[[0 for i in range(n+1)] for j in range(m+1)] for i in range(1,m+1): for j in range(1,n+1): if A[i-1]==B[j-1]: dp[i][j]=1+dp[i-1][j-1] else: dp[i][j]=max(dp[i][j-1],dp[i-1][j]) return dp[m][n]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A, B): # Optimization #commons = set(A).intersection(set(B)) # or commons = set(A) & set(B) #A = [x for x in A if x in commons] #B = [x for x in B if x in commons] N1, N2 = len(A), len(B) dp = [[0 for _ in range(N2+1)] for _ in range(N1+1)] for i1, v1 in enumerate(A, start = 1): for i2, v2 in enumerate(B, start = 1): if v1 == v2: dp[i1][i2] = dp[i1-1][i2-1] + 1 else: dp[i1][i2] = max(dp[i1-1][i2], dp[i1][i2-1]) return dp[N1][N2] class Solution: def maxUncrossedLines(self, A, B): commons = set(A).intersection(set(B)) # or commons = set(A) & set(B) A = [x for x in A if x in commons] B = [x for x in B if x in commons] N1, N2 = len(A), len(B) dp = [0 for _ in range(N2+1)] for i1, v1 in enumerate(A, start = 1): tmp = [0 for _ in range(N2+1)] for i2, v2 in enumerate(B, start = 1): if v1 == v2: tmp[i2] = dp[i2-1] + 1 else: tmp[i2] = max(dp[i2], tmp[i2-1]) dp = tmp return dp[N2]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: dp = [[0 for i in range(len(B) + 1)] for j in range(len(A) + 1)] for i in range(len(A)): for j in range(len(B)): if A[i] == B[j]: dp[i + 1][j + 1] = dp[i][j] + 1 else: dp[i + 1][j + 1] = max(dp[i][j], dp[i][j + 1], dp[i + 1][j]) return dp[-1][-1]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: if((len_A := len(A)) == 0 or (len_B := len(B)) == 0): return 0 matrix = [[0 for i in range(len_B+1)] for k in range(len_A + 1)] for i in range(1, len_A+1): for k in range(1, len_B+1): if(A[i-1] == B[k-1]): matrix[i][k] = matrix[i-1][k-1] + 1 else: matrix[i][k] = max(matrix[i-1][k-1], matrix[i-1][k], matrix[i][k-1]) return matrix[-1][-1]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: self.result = 0 mem = collections.defaultdict() n = len(A) m = len(B) def dfs(pos_a, pos_b): if pos_a >= n or pos_b >= m: return 0 if (pos_a, pos_b) in mem: return mem[(pos_a, pos_b)] best = 0 for i in range(pos_a, n): for j in range(pos_b, m): if A[i] == B[j]: best = max(best, dfs(i+1, j+1) + 1) best = max(best, dfs(i+1, j), dfs(i, j+1)) mem[(pos_a, pos_b)] = best return best dp = [[0 for _ in range(m+1)] for _ in range(n+1)] for i in range(1, n+1): for j in range(1, m+1): if A[i-1] == B[j-1]: dp[i][j] = max(dp[i][j], dp[i-1][j-1] + 1) dp[i][j] = max(dp[i][j], dp[i-1][j], dp[i][j-1]) return dp[-1][-1] #return dfs(0, 0)
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: if not A or not B: return 0 dp = [[0 for _ in range(len(A)+1)] for _ in range(len(B)+1)] for i in range(len(B)+1): for j in range(len(A)+1): if i==0 or j==0: dp[i][j]=0 elif B[i-1]==A[j-1]: dp[i][j] = dp[i-1][j-1]+1 else: dp[i][j] = max(dp[i][j-1],dp[i-1][j]) return dp[-1][-1]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: memo = [[0]*len(A) for _ in range(len(B))] for i in range(len(memo)): for j in range(len(memo[0])): if B[i] == A[j]: memo[i][j] = 1 if i-1 >= 0 and j-1 >= 0: memo[i][j] += memo[i-1][j-1] sub_a = 0 if j-1 >= 0: sub_a = memo[i][j-1] sub_b = 0 if i-1 >= 0: sub_b = memo[i-1][j] memo[i][j] = max(memo[i][j], sub_a, sub_b) return memo[-1][-1]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: len_a = len(A) dp = [0] * len_a for n in B: prev = dp[0] if n == A[0]: dp[0] = 1 for idx in range(1,len_a): if n == A[idx]: prev, dp[idx] = dp[idx],prev+1 else: prev, dp[idx] = dp[idx],max(dp[idx-1], dp[idx]) return dp[-1]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: M, N = len(A), len(B) dp = [[0]*(N+1) for _ in range(M+1)] res = 0 for i in range(1, M+1): for j in range(1, N+1): if A[i-1]==B[j-1]: dp[i][j] = dp[i-1][j-1]+1 else: dp[i][j] = max(dp[i-1][j], dp[i][j-1]) res = max(res, dp[i][j]) return res
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: max_lines = 0 dp = [[0]*(len(B)+1) for _ in range(len(A)+1)] for i in range(1,len(A)+1): for j in range(1,len(B)+1): temp = int(A[i-1]==B[j-1]) dp[i][j] = max(dp[i-1][j],dp[i][j-1],dp[i-1][j-1]+temp) return dp[-1][-1]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: # Time Onm # Space Onm def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: l1, l2 = len(A), len(B) lcs = [[0] * (l1+1) for _ in range(l2+1)] # print(lcs) for i in range(1, l2+1): for j in range(1, l1+1): lcs[i][j] = max(lcs[i-1][j], lcs[i][j-1], lcs[i-1][j-1]+(B[i-1]==A[j-1])) return lcs[l2][l1]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: m, n = len(A), len(B) dp = [[0 for _ in range(n + 1)] for _ in range(m + 1)] for i in range(m + 1): dp[i][0] = 0 for i in range(n + 1): dp[0][i] = 0 ret = 0 for i in range(1, m + 1): for j in range(1, n + 1): dp[i][j] = dp[i-1][j-1] + 1 if A[i-1] == B[j-1] else max(dp[i-1][j], dp[i][j-1]) ret = max(ret, dp[i][j]) return ret
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A, B): # Optimization commons = set(A).intersection(set(B)) # or commons = set(A) & set(B) A = [x for x in A if x in commons] B = [x for x in B if x in commons] N, M = len(A), len(B) dp = [[0] * (M + 1) for _ in range(N + 1)] for i in range(N): for j in range(M): if A[i] == B[j]: dp[i + 1][j + 1] = dp[i][j] + 1 else: dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]) return dp[-1][-1]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: mat=[[0]*(len(B)+1) for j in range(len(A)+1)] for i in range(1,len(A)+1): for j in range(1,len(B)+1): if A[i-1]==B[j-1]: mat[i][j]=mat[i-1][j-1]+1 else: mat[i][j]=max(mat[i-1][j],mat[i][j-1]) return mat[-1][-1]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: # Make a grid of 0's with len(text2) + 1 columns # and len(text1) + 1 rows. dp_grid = [[0] * (len(text2) + 1) for _ in range(len(text1) + 1)] # Iterate up each column, starting from the last one. for col in reversed(list(range(len(text2)))): for row in reversed(list(range(len(text1)))): # If the corresponding characters for this cell are the same... if text2[col] == text1[row]: dp_grid[row][col] = 1 + dp_grid[row + 1][col + 1] # Otherwise they must be different... else: dp_grid[row][col] = max(dp_grid[row + 1][col], dp_grid[row][col + 1]) # The original problem's answer is in dp_grid[0][0]. Return it. return dp_grid[0][0] def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: dp_grid = [[0] * (len(B) + 1) for _ in range(len(A) + 1)] for col in reversed(list(range(len(B)))): for row in reversed(list(range(len(A)))): if B[col] == A[row]: dp_grid[row][col] = 1 + dp_grid[row+1][col+1] else: dp_grid[row][col] = max(dp_grid[row + 1][col], dp_grid[row][col + 1]) return dp_grid[0][0]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: na, nb = len(A), len(B) dp = [[0 for i in range(nb+1)] for j in range(na+1)] for i in range(na): for j in range(nb): if A[i] == B[j]: dp[i+1][j+1] = 1 + dp[i][j] dp[i+1][j+1] = max(dp[i+1][j+1], dp[i+1][j], dp[i][j+1]) return dp[-1][-1]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: m = len(A) n = len(B) dp = [[0 for j in range(n+1)] for i in range(m+1)] for i in range(m+1): for j in range(n+1): if(i==0 or j==0): dp[i][j] = 0 elif(A[i-1]==B[j-1]): dp[i][j] = 1 + dp[i-1][j-1] else: dp[i][j] = max(dp[i-1][j],dp[i][j-1]) print(dp) return dp[m][n]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: M, N = len(A), len(B) dp = [0] * (N+1) for i in range(M): for j in range(N)[::-1]: if B[j] == A[i]: dp[j+1] = dp[j] + 1 for j in range(N): dp[j+1] = max(dp[j+1], dp[j]) return dp[-1]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: memo = collections.defaultdict(int) def maxUncrossedLinesHelper(ai:int, bi:int)->int: result = 0 if ai >= len(A): return 0 if bi >= len(B): return 0 if (ai,bi) in memo: return memo[(ai,bi)] if A[ai] == B[bi]: result = maxUncrossedLinesHelper(ai+1, bi+1) + 1 memo[(ai,bi)] = result ## return result chooseA = 0 try: ainb = B.index(A[ai], bi) except: ainb = -1 if ainb > 0: chooseA = maxUncrossedLinesHelper(ai+1, ainb+1) + 1 chooseB = 0 try: bina = A.index(B[bi], ai) except: bina = -1 if bina > 0: chooseB = maxUncrossedLinesHelper(bina+1, bi+1) + 1 notchooseAB = maxUncrossedLinesHelper(ai+1, bi+1) result = max(chooseA, chooseB, notchooseAB,result) memo[(ai,bi)] = result return result result = maxUncrossedLinesHelper(0, 0) return result
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: a, b = len(A), len(B) dp = {} for i, num1 in enumerate(A): for j, num2 in enumerate(B): if num1 == num2: dp[(i,j)] = 1 + dp[i-1,j-1] if min(i,j) > 0 else 1 else: dp[(i,j)] = max(dp[i-1,j] if i > 0 else 0, dp[i,j-1] if j > 0 else 0 ) return dp[a-1,b-1]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: # DP : option #1 : Connect A to B # option #2 : Skip B and look for another candidate # Longest Common Subsequence # if A[i] == B[j], connect and move i-1, j-1 n, m = len(A), len(B) @lru_cache(None) def dp(i, j): if i < 0 or j < 0: return 0 if A[i] == B[j]: return dp(i-1, j-1) + 1 return max(dp(i-1, j), dp(i, j-1)) return dp(n-1, m-1)
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: M,N=len(A),len(B) @lru_cache(None) def dfs(i,j): if i==M or j==N: return 0 if A[i]==B[j]: return 1+dfs(i+1,j+1) else: return max( dfs(i+1,j), dfs(i,j+1) ) return dfs(0,0)
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: if len(A) > len(B): return self.maxUncrossedLines(B, A) if not A or not B: return 0 # try: # idx = B.index(A[0]) # return max(1+self.maxUncrossedLines(A[1:], B[idx+1:]), self.maxUncrossedLines(A[1:], B)) # except: # return self.maxUncrossedLines(A[1:], B) # i = 0..len(A)-1 2 5 1 2 5 # j = 0..len(B)-1 10 5 2 1 5 2 dp = [[0]*len(B) for _ in range(len(A))] indexB = collections.defaultdict(list) for i, num in enumerate(B): indexB[num].append(i) for i in reversed(list(range(len(A)))): for j in reversed(list(range(len(B)))): # find A[i] in B[j:] found = False for idx in indexB.get(A[i], []): if idx >= j: found = True break if found: dp[i][j] = max(1+(dp[i+1][idx+1] if i+1<len(A) and idx+1<len(B) else 0), dp[i+1][j] if i+1<len(A) else 0) else: dp[i][j] = dp[i+1][j] if i+1 < len(A) else 0 return dp[0][0]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A, B): commons = set(A) & set(B) A = [x for x in A if x in commons] B = [x for x in B if x in commons] N1, N2 = len(A), len(B) dp = [[0 for _ in range(N2+1)] for _ in range(N1+1)] for i1, v1 in enumerate(A, start = 1): for i2, v2 in enumerate(B, start = 1): if v1 == v2: dp[i1][i2] = dp[i1-1][i2-1] + 1 else: dp[i1][i2] = max(dp[i1-1][i2], dp[i1][i2-1]) return dp[N1][N2]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: A = [-1] + A B = [-1] + B #142 #121 #0000 #0111 #0111 #0122 w = len(A) h = len(B) dp_table = [[0 for _ in range(w)] for _ in range(h) ] for x in range(1,w): for y in range(1,h): if A[x] == B[y]: dp_table[y][x] = dp_table[y-1][x-1] +1 else: dp_table[y][x] = max(dp_table[y][x-1],dp_table[y-1][x]) print(dp_table) return dp_table[-1][-1]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: a = len(A) b = len(B) memo = {} for i in range(a): for j in range(b): if A[i] == B[j]: memo[(i,j)] = memo.get((i-1, j-1), 0) + 1 else: memo[(i,j)] = max(memo.get((i-1,j), 0), memo.get((i,j-1), 0)) return max(memo.values())
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: a = len(A) b = len(B) memo = {} for i in range(a): for j in range(b): if A[i] == B[j]: memo[(i,j)] = memo.get((i-1, j-1), 0) + 1 else: memo[(i,j)] = max(memo.get((i-1,j), 0), memo.get((i,j-1), 0)) return memo.get((a-1, b-1), 0)
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: n = len(A);m = len(B) @lru_cache(None) def fun(i,j): if i == n or j == m: return 0 if A[i] == B[j]: return 1 + fun(i+1,j+1) else: return max(fun(i+1,j),fun(i,j+1)) return fun(0,0)
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: dp = [[0] * (len(B) + 1) for _ in range(len(A) + 1)] last_a = {} last_b = {} for i in range(len(A)): last_a[A[i]] = i for j in range(len(B)): last_b[B[j]] = j if A[i] == B[j]: dp[i + 1][j + 1] = max(dp[i][j] + 1, dp[i + 1][j], dp[i][j + 1]) else: dp[i + 1][j + 1] = max(dp[i][j], dp[i + 1][last_b.get(A[i], -1) + 1], dp[last_a.get(B[j], -1) + 1][j + 1]) return dp[-1][-1]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: na, nb = len(A), len(B) dp = [[0 for i in range(nb+1)] for j in range(na+1)] for i in range(na): for j in range(nb): if A[i] == B[j]: dp[i+1][j+1] = 1 + dp[i][j] dp[i+1][j+1] = max(dp[i+1][j+1], dp[i][j+1], dp[i+1][j]) return dp[-1][-1]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: dp = collections.defaultdict(int) m = len(A) n = len(B) for i in range(m): for j in range(n): dp[i,j] = max(dp[i-1,j], dp[i,j-1], dp[i-1,j-1] + (A[i] == B[j])) return dp[m-1,n-1]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: dp, m, n = collections.defaultdict(int), len(A), len(B) for i in range(m): for j in range(n): dp[i, j] = max(dp[i - 1, j - 1] + (A[i] == B[j]), dp[i - 1, j], dp[i, j - 1]) return dp[m - 1, n - 1]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A, B): # optimization commons = set(A).intersection(set(B)) # or commons = set(A) & set(B) A = [x for x in A if x in commons] B = [x for x in B if x in commons] N1, N2 = len(A), len(B) dp = [[0 for _ in range(N2+1)] for _ in range(N1+1)] for i1, v1 in enumerate(A, start = 1): for i2, v2 in enumerate(B, start = 1): if v1 == v2: dp[i1][i2] = dp[i1-1][i2-1] + 1 else: dp[i1][i2] = max(dp[i1-1][i2], dp[i1][i2-1]) return dp[N1][N2]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: m, n = len(A), len(B) dp = collections.defaultdict(int) for i in range(m): for j in range(n): match = 1 if A[i] == B[j] else 0 dp[i, j] = max(dp[i - 1, j - 1] + match, dp[i - 1, j], dp[i, j - 1]) return dp[m - 1, n - 1]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
# https://www.youtube.com/watch?v=7qr4j_fB9S4 # https://leetcode.com/problems/uncrossed-lines/discuss/652102/Python-3-today's-one-liner class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: dp, n, m = defaultdict(int), len(A), len(B) for a,b in product(range(n), range(m)): dp[a,b] = max(dp[a-1,b-1] + (A[a]==B[b]), dp[a-1,b], dp[a,b-1]) return dp[n-1, m-1]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: @lru_cache(None) def max_connections(A_index, B_index): if A_index > len(A) - 1 or B_index > len(B) - 1: return 0 if A[A_index] == B[B_index]: return max(0, 1 + max_connections(A_index + 1, B_index + 1)) else: return max(0, max_connections(A_index + 1, B_index), max_connections(A_index, B_index + 1) ) return max_connections(0, 0)
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: dp = defaultdict(int) N, M = len(A), len(B) for i in range(N): for j in range(M): dp[i,j] = max(dp[i-1,j-1] + (A[i] == B[j]), dp[i-1,j], dp[i,j-1]) return dp[N-1,M-1]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: m = len(A) n = len(B) dp = [[0] * (n+1) for _ in range(m+1)] for i in range(1,m+1): for j in range(1,n+1): if A[i-1]==B[j-1]: dp[i][j] = dp[i-1][j-1]+1 dp[i][j] = max(dp[i-1][j], dp[i][j-1], dp[i][j]) return dp[-1][-1]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: dp, m, n = collections.defaultdict(int), len(A), len(B) for i in range(m): for j in range(n): dp[i, j] = max(dp[i - 1, j - 1] + (A[i] == B[j]), dp[i - 1, j], dp[i, j - 1]) return dp[m - 1, n - 1]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: @lru_cache(maxsize=None) def helper(a_idx, b_idx): if a_idx == len(A) or b_idx == len(B): return 0 c1 = 0 if A[a_idx] == B[b_idx]: c1 = 1 + helper(a_idx + 1, b_idx + 1) c2 = helper(a_idx + 1, b_idx) c3 = helper(a_idx, b_idx + 1) return max(c1, c2, c3) return helper(0, 0)
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: if len(A)<len(B): A, B=B, A DP=[0]* (len(A)+1) coll={j:[-1]+[i for i in range(len(A)) if A[i]==j] for j in set(B)} for iB in B[::-1]: for i in range(1, len(coll[iB])): DP[coll[iB][i]] = max(DP[coll[iB][i]], DP[coll[iB][i]+1]+1) for j in range(coll[iB][i], coll[iB][i-1], -1): DP[j]=max(DP[j], DP[j+1]) return DP[0]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: m, n = len(A), len(B) @lru_cache(None) def dfs(i, j): if i == -1 or j == -1: return 0 if A[i] == B[j]: return dfs(i - 1, j - 1) + 1 return max(dfs(i - 1, j), dfs(i, j - 1)) return dfs(m - 1, n - 1)
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: dp = [[0 for _ in B] for _ in A] for i in range(len(B)): if A[0] == B[i]: dp[0][i] = 1 elif i != 0: dp[0][i] = dp[0][i-1] for i in range(len(A)): if B[0] == A[i]: dp[i][0] = 1 elif i != 0: dp[i][0] = dp[i-1][0] for i in range(1,len(A)): for j in range(1,len(B)): if A[i] == B[j]: dp[i][j] = max(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]+1) else: dp[i][j] = max(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]) return dp[-1][-1]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: n1 = len(A) n2 = len(B) dp = [[-1 for _ in range(n2)] for _ in range(n1)] return self.recur(0,0,A,B,n1,n2, dp) def recur(self,i,j,A,B,n1,n2,dp): if i == n1 or j == n2: return 0 if dp[i][j] != -1: return dp[i][j] if A[i] == B[j]: dp[i][j] = 1 + self.recur(i+1,j+1,A,B,n1,n2,dp) else: c1 = self.recur(i+1,j,A,B,n1,n2,dp) c2 = self.recur(i,j+1,A,B,n1,n2,dp) dp[i][j] = max(c1,c2) return dp[i][j]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: max_val = 0 m, n = len(A), len(B) dp = [ [0 for i in range(n + 1)] for j in range(m + 1) ] for i in range(1, m + 1): for j in range(1, n + 1): if A[i - 1] == B[j - 1]: dp[i][j] = max( dp[i-1][j-1] + 1, dp[i-1][j], dp[i][j-1] ) else: dp[i][j] = max( dp[i-1][j-1], dp[i-1][j], dp[i][j-1] ) max_val = max(max_val, dp[i][j]) return max_val
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: memo = {} def helper(A, B, a, b): if (a,b) in memo: return memo[(a,b)] ret = 0 if a == len(A) or len(B) == b: return 0 if A[a] == B[b]: memo[(a,b)] = 1+ helper(A,B, a+1, b+1) return memo[(a,b)] ret = max(ret, helper(A,B,a, b+1), helper(A,B,a+1, b)) memo[(a,b)] = ret return ret return helper(A,B,0,0)
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: A = [ -1 ] + A B = [ -1 ] + B dp_table = {} def helper( idx_a, idx_b ): if (idx_a, idx_b) in dp_table: return dp_table[(idx_a, idx_b)] if idx_a == 0 or idx_b == 0: # Any list compared to empty list give no uncrossed line return 0 elif A[idx_a] == B[idx_b]: dp_table[(idx_a, idx_b)] = helper(idx_a-1, idx_b-1) + 1 return dp_table[(idx_a, idx_b)] else: dp_table[(idx_a, idx_b)] = max( helper(idx_a-1, idx_b), helper(idx_a, idx_b-1)) return dp_table[(idx_a, idx_b)] # -------------------------------------------- return helper( len(A)-1, len(B)-1 )
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: # Longest common subsequence dp = collections.defaultdict(int) for i in range(len(A)): for j in range(len(B)): dp[i, j] = max(dp[i - 1, j - 1] + (A[i] == B[j]), dp[i - 1, j], dp[i, j - 1]) return dp[len(A) - 1, len(B) - 1]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: l = max(len(A),len(B)) dp = [[-1 for i in range(l+1)]for j in range(l+1)] def func(indexA, indexB): if indexA >= len(A) or indexB >= len(B): return 0 print if dp[indexA][indexB] != -1: return dp[indexA][indexB] if A[indexA] == B[indexB]: dp[indexA][indexB] = func(indexA + 1, indexB + 1) + 1 else: dp[indexA][indexB] = max(func(indexA + 1, indexB),func(indexA, indexB + 1)) return dp[indexA][indexB] return func(0, 0)
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: store = {} a_len = len(A) b_len = len(B) def f(a, b): if (a, b) in store: return store[(a, b)] if a == a_len or b == b_len: store[(a, b)] = 0 return 0 if A[a] == B[b]: store[(a, b)] = 1 + f(a + 1, b + 1) else: store[(a, b)] = max(f(a + 1, b), f(a, b + 1)) return store[(a, b)] return f(0, 0)
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: ab_dict = {a:[] for a in A} for i, b in enumerate(B): if b in ab_dict: ab_dict[b].append(i) lenA, lenB = len(A), len(B) subSolutions = [[None]*lenB for _ in range(lenA)] def subSolve(i,j): if i >= lenA or j >= lenB: return 0 if subSolutions[i][j]: return subSolutions[i][j] if not ab_dict[A[i]]: subSolutions[i][j] = 0 return subSolve(i+1, j) if A[i] in ab_dict and j in ab_dict[A[i]]: subSolutions[i][j] = 1 + subSolve(i+1, j+1) return subSolutions[i][j] # if you include A[i]: next_j = None for b in ab_dict[A[i]]: if j < b: next_j = b break if next_j is None: subSolutions[i][j] = 0 return subSolve(i+1, j) subSolutions[i][j] = max(subSolve(i, next_j), subSolve(i+1, j)) return subSolutions[i][j] return subSolve(0,0)
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: m=len(A) n=len(B) dp=[[0 for _ in range(n+1)] for _ in range(m+1)] for i in range(1,m+1): for j in range(1,n+1): if(A[i-1]==B[j-1]): dp[i][j]=dp[i-1][j-1]+1 else: dp[i][j]=max(dp[i][j-1],dp[i-1][j]) return dp[m][n]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: A = [ -1 ] + A B = [ -1 ] + B dp_table = {} def helper( idx_a, idx_b ): if (idx_a, idx_b) in dp_table: return dp_table[(idx_a, idx_b)] if idx_a == 0 or idx_b == 0: # Any list compared to empty list give no uncrossed line return 0 elif A[idx_a] == B[idx_b]: dp_table[(idx_a, idx_b)] = helper(idx_a-1, idx_b-1) + 1 return dp_table[(idx_a, idx_b)] else: dp_table[(idx_a, idx_b)] = max( helper(idx_a-1, idx_b), helper(idx_a, idx_b-1)) return dp_table[(idx_a, idx_b)] # -------------------------------------------- return helper( len(A)-1, len(B)-1 )
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: n = len(A);m = len(B) @lru_cache(None) def fun(i,j): if i == n or j == m: return 0 loc = -1 for k in range(j,m): if B[k]== A[i]: loc = k break if loc == -1: return fun(i+1,j) else: return max(fun(i+1,j),1 + fun(i+1,loc+1)) return fun(0,0)
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: memo = [[None for i in range(len(B))] for i in range(len(A))] return self.uncrossRec(A,B,0,0,memo) def uncrossRec(self, A: List[int], B: List[int], i: int, j: int, memo): if j == len(B) or i == len(A): return 0 if memo[i][j] != None: return memo[i][j] if A[i] == B[j]: memo[i][j] = 1 + self.uncrossRec(A,B,i+1,j+1,memo) else: x =self.uncrossRec(A,B,i,j+1,memo) y = self.uncrossRec(A,B,i+1,j,memo) memo[i][j] = max(x,y) return memo[i][j]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: m,n = len(A),len(B) grid = [[0 for i in range(n)]for i in range(m)] for j in range(m): for i in range(n): av = A[j] bv = B[i] topleft = 0 top = 0 left = 0 if 0 <= j-1 < m and 0 <= i - 1 < n: topleft = grid[j-1][i-1] if 0 <= j-1 < m: top = grid[j-1][i] if 0 <= i-1 < n: left = grid[j][i-1] if av == bv: grid[j][i] = topleft + 1 else: grid[j][i] = max(top,left) return grid[-1][-1]
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: nA, nB = len(A), len(B) if nA == 0 or nB == 0: return 0 cache = {} def process(iA, iB): if iA < 0 or iB < 0: return 0 if not (iA, iB) in cache: ans = 0 if A[iA] == B[iB]: ans = 1 + process(iA-1, iB-1) else: ans = max(process(iA-1, iB), process(iA, iB-1)) cache[(iA, iB)] = ans return cache[(iA, iB)] return process(nA-1, nB-1)
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: mul_result = {} def get_mul(high, low): if high == -1 or low == -1: return 0 if (high, low) in mul_result: return mul_result[(high, low)] result = get_mul(high-1, low-1) if A[high] in B[:low+1]: for i in range(low, -1, -1): if B[i] == A[high]: break result = max(result, get_mul(high-1, i-1) + 1) if B[low] in A[:high+1]: for i in range(high, -1, -1): if A[i] == B[low]: break result = max(result, get_mul(i-1, low-1) + 1) mul_result[(high, low)] = result return result return get_mul(len(A) - 1, len(B) - 1)
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: N_a=len(A) N_b=len(B) pair2maxlines=defaultdict(int) def max_lines(a,b): if pair2maxlines.get((a,b)) is None: if a==N_a or b==N_b: pair2maxlines[(a,b)]=0 return 0 if A[a]==B[b]: output =1+max_lines(a+1,b+1) pair2maxlines[(a,b)]=output return output output = max(max_lines(a+1,b),max_lines(a,b+1)) pair2maxlines[(a,b)]=output return output else: return pair2maxlines[(a,b)] return max_lines(0,0)
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: n=len(A) m=len(B) d={} def helper(i,j): if i==n or j==m: return 0 if (i,j) in d: return d[(i,j)] if A[i]==B[j]: d[(i,j)]=1+helper(i+1,j+1) else: d[(i,j)]=max(helper(i+1,j),helper(i,j+1)) return d[(i,j)] return helper(0,0)
We write the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that: A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line. Return the maximum number of connecting lines we can draw in this way.   Example 1: Input: A = [1,4,2], B = [1,2,4] Output: 2 Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2. Example 2: Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2] Output: 3 Example 3: Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1] Output: 2   Note: 1 <= A.length <= 500 1 <= B.length <= 500 1 <= A[i], B[i] <= 2000
class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: def help(i, j, mem): if i>=len(A) or j>=len(B): return 0 if (i,j) in mem: return mem[(i, j)] if A[i]==B[j]: mem[(i,j)]= 1+help(i+1, j+1, mem) return mem[(i,j)] mem[(i,j)]= max(help(i,j+1, mem), help(i+1,j,mem)) return mem[(i,j)] mem = {} return help(0,0, mem)
Given the coordinates of four points in 2D space, return whether the four points could construct a square. The coordinate (x,y) of a point is represented by an integer array with two integers. Example: Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1] Output: True Note: All the input integers are in the range [-10000, 10000]. A valid square has four equal sides with positive length and four equal angles (90-degree angles). Input points have no order.
class Solution: def validSquare(self, p1, p2, p3, p4): """ :type p1: List[int] :type p2: List[int] :type p3: List[int] :type p4: List[int] :rtype: bool """ def length(x,y): return (x[0]-y[0])*(x[0]-y[0]) + (x[1]-y[1])*(x[1]-y[1]) res = [] a1 = length(p1,p2) a2 = length(p1,p3) a3 = length(p1,p4) a4 = length(p2,p3) a5 = length(p2,p4) a6 = length(p3,p4) res = [a1,a2,a3,a4,a5,a6] res = sorted(res); for i in range(3): if res[i] == res[i+1]: continue else: return False if res[4] != res[5]: return False if res[0] != 0: return True else: return False
Given the coordinates of four points in 2D space, return whether the four points could construct a square. The coordinate (x,y) of a point is represented by an integer array with two integers. Example: Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1] Output: True Note: All the input integers are in the range [-10000, 10000]. A valid square has four equal sides with positive length and four equal angles (90-degree angles). Input points have no order.
class Solution: def validSquare(self, p1, p2, p3, p4): """ :type p1: List[int] :type p2: List[int] :type p3: List[int] :type p4: List[int] :rtype: bool """ p1 = [p1[0]*4, p1[1]*4] p2 = [p2[0]*4, p2[1]*4] p3 = [p3[0]*4, p3[1]*4] p4 = [p4[0]*4, p4[1]*4] center_x = (p1[0] + p2[0] + p3[0] + p4[0]) / 4 center_y = (p1[1] + p2[1] + p3[1] + p4[1]) / 4 print((center_x, " ", center_y)) #if center_x != center_y: #return False #print(center_x, " ", center_y) if p1 == [center_x, center_y]: return False #print(center_x, " ", center_y) center_to_p1 = [center_x - p1[0], center_y - p1[1]] e_p2 = [(int)(center_x - (center_y - p1[1])), (int)(center_y + (center_x - p1[0]))] e_p3 = [(int)(center_x + center_x - p1[0]), (int)(center_y + center_y - p1[1])] e_p4 = [(int)(center_x + (center_y - p1[1])), (int)(center_y - (center_x - p1[0]))] #print ("e_p2 ", e_p2, " e_p3 ", e_p3, " e_p4 ", e_p4) #print ("p2 ", p2, " p3 ", p3, " p4 ", p4) if (e_p2 == p2 or e_p2 == p3 or e_p2 == p4) and (e_p3 == p2 or e_p3 == p3 or e_p3 == p4) and (e_p4 == p2 or e_p4 == p3 or e_p4 == p4): return True else: return False
Given the coordinates of four points in 2D space, return whether the four points could construct a square. The coordinate (x,y) of a point is represented by an integer array with two integers. Example: Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1] Output: True Note: All the input integers are in the range [-10000, 10000]. A valid square has four equal sides with positive length and four equal angles (90-degree angles). Input points have no order.
class Solution(object): def validSquare(self, p1, p2, p3, p4): points = [p1, p2, p3, p4] dists = collections.Counter() for i in range(len(points)): for j in range(i+1, len(points)): dists[self.getDistance(points[i], points[j])] += 1 return len(list(dists.values()))==2 and 4 in list(dists.values()) and 2 in list(dists.values()) def getDistance(self, p1, p2): return (p1[0] - p2[0])**2 + (p1[1] - p2[1])**2
Given the coordinates of four points in 2D space, return whether the four points could construct a square. The coordinate (x,y) of a point is represented by an integer array with two integers. Example: Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1] Output: True Note: All the input integers are in the range [-10000, 10000]. A valid square has four equal sides with positive length and four equal angles (90-degree angles). Input points have no order.
class Solution: def validSquare(self, p1, p2, p3, p4): """ :type p1: List[int] :type p2: List[int] :type p3: List[int] :type p4: List[int] :rtype: bool """ def distance(m1,m2): x1 = m1[0] y1 = m1[1] x2 = m2[0] y2 = m2[1] return (((y2-y1)**2) + ((x2-x1)**2) )**0.5 m = [d1,d2,d3,d4,d5,d6] = distance(p1,p2),distance(p2,p3),distance(p3,p4),distance(p4,p1),distance(p2,p4),distance(p3,p1) m = sorted(list(m)) print(m) c=0 x = m[0] if m[0] ==0.0 and m[1] == 0.0 and m[2] == 0.0 and m[3] == 0.0: return False for i in m: if c == 4: return True if i == m[0]: c +=1 else: m[0] = i c = 0 return False
Given the coordinates of four points in 2D space, return whether the four points could construct a square. The coordinate (x,y) of a point is represented by an integer array with two integers. Example: Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1] Output: True Note: All the input integers are in the range [-10000, 10000]. A valid square has four equal sides with positive length and four equal angles (90-degree angles). Input points have no order.
class Solution: def validSquare(self, p1, p2, p3, p4): """ :type p1: List[int] :type p2: List[int] :type p3: List[int] :type p4: List[int] :rtype: bool """ p1 = [p1[0]*4, p1[1]*4] p2 = [p2[0]*4, p2[1]*4] p3 = [p3[0]*4, p3[1]*4] p4 = [p4[0]*4, p4[1]*4] center_x = (p1[0] + p2[0] + p3[0] + p4[0]) / 4 center_y = (p1[1] + p2[1] + p3[1] + p4[1]) / 4 #print(center_x, " ", center_y) #if center_x != center_y: #return False #print(center_x, " ", center_y) if p1 == [center_x, center_y]: return False #print(center_x, " ", center_y) center_to_p1 = [center_x - p1[0], center_y - p1[1]] e_p2 = [(int)(center_x - (center_y - p1[1])), (int)(center_y + (center_x - p1[0]))] e_p3 = [(int)(center_x + center_x - p1[0]), (int)(center_y + center_y - p1[1])] e_p4 = [(int)(center_x + (center_y - p1[1])), (int)(center_y - (center_x - p1[0]))] #print ("e_p2 ", e_p2, " e_p3 ", e_p3, " e_p4 ", e_p4) #print ("p2 ", p2, " p3 ", p3, " p4 ", p4) if (e_p2 == p2 or e_p2 == p3 or e_p2 == p4) and (e_p3 == p2 or e_p3 == p3 or e_p3 == p4) and (e_p4 == p2 or e_p4 == p3 or e_p4 == p4): return True else: return False
Given the coordinates of four points in 2D space, return whether the four points could construct a square. The coordinate (x,y) of a point is represented by an integer array with two integers. Example: Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1] Output: True Note: All the input integers are in the range [-10000, 10000]. A valid square has four equal sides with positive length and four equal angles (90-degree angles). Input points have no order.
class Solution: def dist(self, p1, p2): return math.sqrt((p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2) def validSquare(self, p1, p2, p3, p4): """ :type p1: List[int] :type p2: List[int] :type p3: List[int] :type p4: List[int] :rtype: bool """ ps = [p1, p2, p3, p4] ds = [] for i in range(3): for j in range(i+1, 4): ds.append(self.dist(ps[i], ps[j])) ds.sort() return ds[0] > 0 and ds[0] == ds[1] == ds[2] == ds[3] and ds[4] == ds[5]
Given the coordinates of four points in 2D space, return whether the four points could construct a square. The coordinate (x,y) of a point is represented by an integer array with two integers. Example: Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1] Output: True Note: All the input integers are in the range [-10000, 10000]. A valid square has four equal sides with positive length and four equal angles (90-degree angles). Input points have no order.
class Solution: def validSquare(self, p1, p2, p3, p4): """ :type p1: List[int] :type p2: List[int] :type p3: List[int] :type p4: List[int] :rtype: bool """ p = [] for i in range(1,5): p.append( eval('p'+str(i)) ) e = {} for i in range(4): for j in range(4): for k in range(j+1,4): if( i== j or i == k or j == k): continue dij = (p[i][0] - p[j][0])**2 + (p[i][1] - p[j][1])**2 dik = (p[i][0] - p[k][0])**2 + (p[i][1] - p[k][1])**2 djk = (p[j][0] - p[k][0])**2 + (p[j][1] - p[k][1])**2 if( dij == dik and dij + dik == djk ): if dij in e: e[dij] += 1 else: e[dij] = 1 for k in e: print((e[k])) if( len(e) != 1 ): return False for k in e: if e[k] == 4: return True else: return False
Given the coordinates of four points in 2D space, return whether the four points could construct a square. The coordinate (x,y) of a point is represented by an integer array with two integers. Example: Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1] Output: True Note: All the input integers are in the range [-10000, 10000]. A valid square has four equal sides with positive length and four equal angles (90-degree angles). Input points have no order.
class Solution: def validSquare(self, p1, p2, p3, p4): """ :type p1: List[int] :type p2: List[int] :type p3: List[int] :type p4: List[int] :rtype: bool """ ps = sorted([p1,p2,p3,p4]) return ps[0][0]+ps[3][0]==ps[1][0]+ps[2][0] and ps[0][1]+ps[3][1]==ps[1][1]+ps[2][1] and ps[2][0]-ps[1][0]==abs(ps[3][1]-ps[0][1]) and not ps[0]==ps[1]
Given the coordinates of four points in 2D space, return whether the four points could construct a square. The coordinate (x,y) of a point is represented by an integer array with two integers. Example: Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1] Output: True Note: All the input integers are in the range [-10000, 10000]. A valid square has four equal sides with positive length and four equal angles (90-degree angles). Input points have no order.
class Solution: def validSquare(self, A, B, C, D): """ A B A D A C C D B C D B A C A B A D B D D C C B - figure out which orientation this is - B-A == D-C and C-A == D-B - D-A == C-B and B-A == C-D - C-A == B-D and D-A == B-C - if one of those is true, just need to check angles - check length of two perpendicular sides is same - enough to check -y, x? 4 A(2,4) 3 2 C(0,2) B(4,2) 1 0 D(2,0) 0 1 2 3 4 [1,0] [-1,0] [0,-1] [0,1] 1 D 0 B A -1 C -1 0 1 :type p1: List[int] :type p2: List[int] :type p3: List[int] :type p4: List[int] :rtype: bool """ def sub(p2, p1): return [p2[0]-p1[0], p2[1]-p1[1]] # def add(p1, p2): # return [p1[0]+p2[0], p1[1]+p2[1]] if sub(A, B) == [0, 0]: return False if sub(B, A) == sub(D, C) and sub(C, A) == sub(D, B): print((1)) diff1 = sub(B, A) diff2 = sub(A, C) print((diff1, diff2)) if diff1 == [-diff2[1], diff2[0]] or diff1 == [diff2[1], -diff2[0]]: return True elif sub(D, A) == sub(C, B) and sub(B, A) == sub(C, D): print((2)) diff1 = sub(D, A) diff2 = sub(A, B) print((diff1, diff2)) if diff1 == [-diff2[1], diff2[0]] or diff1 == [diff2[1], -diff2[0]]: return True elif sub(C, A) == sub(B, D) and sub(D, A) == sub(B, C): print((3)) diff1 = sub(C, A) diff2 = sub(A, D) print((diff1, diff2)) # [-1, -1] [1, -1] if diff1 == [-diff2[1], diff2[0]] or diff1 == [diff2[1], -diff2[0]]: return True else: print((0)) return False
Given the coordinates of four points in 2D space, return whether the four points could construct a square. The coordinate (x,y) of a point is represented by an integer array with two integers. Example: Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1] Output: True Note: All the input integers are in the range [-10000, 10000]. A valid square has four equal sides with positive length and four equal angles (90-degree angles). Input points have no order.
class Solution: def validSquare(self, p1, p2, p3, p4): """ :type p1: List[int] :type p2: List[int] :type p3: List[int] :type p4: List[int] :rtype: bool """ l = [p1, p2, p3, p4] s = set() for i in range(len(l)): for j in range(i+1, len(l)): d = self.dist(l[i], l[j]) if d == 0: return False s.add(d) return len(s) == 2 def dist(self, a, b): return (a[0] - b[0])**2 + (a[1] - b[1])**2
Given an integer array arr, you should partition the array into (contiguous) subarrays of length at most k. After partitioning, each subarray has their values changed to become the maximum value of that subarray. Return the largest sum of the given array after partitioning.   Example 1: Input: arr = [1,15,7,9,2,5,10], k = 3 Output: 84 Explanation: arr becomes [15,15,15,9,10,10,10] Example 2: Input: arr = [1,4,1,5,7,3,6,1,9,9,3], k = 4 Output: 83 Example 3: Input: arr = [1], k = 1 Output: 1   Constraints: 1 <= arr.length <= 500 0 <= arr[i] <= 109 1 <= k <= arr.length
class Solution: def maxSumAfterPartitioning(self, arr, k): res = [0] for idx, val in enumerate(arr): max_val, cur_val = 0, 0 for i in range(max(0, idx-k+1), idx+1)[::-1]: if arr[i] > max_val: max_val = arr[i] if res[i] + (idx-i+1)*max_val > cur_val: cur_val = res[i] + (idx-i+1)*max_val res.append(cur_val) return res[-1]
Given an integer array arr, you should partition the array into (contiguous) subarrays of length at most k. After partitioning, each subarray has their values changed to become the maximum value of that subarray. Return the largest sum of the given array after partitioning.   Example 1: Input: arr = [1,15,7,9,2,5,10], k = 3 Output: 84 Explanation: arr becomes [15,15,15,9,10,10,10] Example 2: Input: arr = [1,4,1,5,7,3,6,1,9,9,3], k = 4 Output: 83 Example 3: Input: arr = [1], k = 1 Output: 1   Constraints: 1 <= arr.length <= 500 0 <= arr[i] <= 109 1 <= k <= arr.length
class Solution: def maxSumAfterPartitioning(self, A: List[int], K: int) -> int: # i = 0 # temp = deque() # orig_k = K # while i < len(A): # temp.append(max(A[i:K])) # i = K # K *= 2 # print(temp) # answer = deque() # while temp: # for _ in range(len(temp)): # current_max = max(temp) # #print(current_max) # for _ in range(orig_k): # answer.append(current_max) # if len(answer) == len(A): # break # print(answer) # temp.remove(current_max) # return sum(answer) ans=[0] for r, n in enumerate(A): maxVal, curVal = 0, 0 for l in range(max(0, r-K+1), r+1)[::-1]: if A[l]>maxVal: maxVal=A[l] if ans[l]+(r-l+1)*maxVal>curVal: curVal=ans[l]+(r-l+1)*maxVal ans.append(curVal) return ans[-1]
Given an integer array arr, you should partition the array into (contiguous) subarrays of length at most k. After partitioning, each subarray has their values changed to become the maximum value of that subarray. Return the largest sum of the given array after partitioning.   Example 1: Input: arr = [1,15,7,9,2,5,10], k = 3 Output: 84 Explanation: arr becomes [15,15,15,9,10,10,10] Example 2: Input: arr = [1,4,1,5,7,3,6,1,9,9,3], k = 4 Output: 83 Example 3: Input: arr = [1], k = 1 Output: 1   Constraints: 1 <= arr.length <= 500 0 <= arr[i] <= 109 1 <= k <= arr.length
class Solution: def maxSumAfterPartitioning(self, A: List[int], K: int) -> int: dp = [A[0]] for i in range(1, len(A)): max_partition_sum = A[i] + dp[-1] max_element = A[i] end = i - K if (end < -1): end = -1 for j in range(i - 1, end, -1): if (A[j] > max_element): max_element = A[j] partition_sum = (i - j + 1) * max_element if (j > 0): partition_sum += dp[j - 1] if (partition_sum > max_partition_sum): max_partition_sum = partition_sum dp.append(max_partition_sum) return dp[-1]
Given an integer array arr, you should partition the array into (contiguous) subarrays of length at most k. After partitioning, each subarray has their values changed to become the maximum value of that subarray. Return the largest sum of the given array after partitioning.   Example 1: Input: arr = [1,15,7,9,2,5,10], k = 3 Output: 84 Explanation: arr becomes [15,15,15,9,10,10,10] Example 2: Input: arr = [1,4,1,5,7,3,6,1,9,9,3], k = 4 Output: 83 Example 3: Input: arr = [1], k = 1 Output: 1   Constraints: 1 <= arr.length <= 500 0 <= arr[i] <= 109 1 <= k <= arr.length
class Solution: def maxSumAfterPartitioning(self, arr: List[int], k: int) -> int: return self.dp(arr, 0, k, {}) def dp(self, arr, i, k, hashmap): if i>=len(arr): return 0 if i in hashmap: return hashmap[i] largest = 0 ans = 0 for idx in range(k): if idx + i >=len(arr): break largest = max(largest, arr[i+idx]) ans = max(ans, largest*(idx+1) + self.dp(arr, i+idx+1, k, hashmap)) hashmap[i] = ans return ans
Given an integer array arr, you should partition the array into (contiguous) subarrays of length at most k. After partitioning, each subarray has their values changed to become the maximum value of that subarray. Return the largest sum of the given array after partitioning.   Example 1: Input: arr = [1,15,7,9,2,5,10], k = 3 Output: 84 Explanation: arr becomes [15,15,15,9,10,10,10] Example 2: Input: arr = [1,4,1,5,7,3,6,1,9,9,3], k = 4 Output: 83 Example 3: Input: arr = [1], k = 1 Output: 1   Constraints: 1 <= arr.length <= 500 0 <= arr[i] <= 109 1 <= k <= arr.length
class Solution: def maxSumAfterPartitioning(self, arr: List[int], k: int) -> int: return self.recur(arr, len(arr)-1, k, {}) def recur(self, arr, index, k, hashMap): if index < 0: return 0 if index in hashMap: return hashMap[index] result = 0 maxV = 0 for i in range(k): if index - i < 0: continue maxV = max(maxV, arr[index-i]) result = max(result, maxV * (i +1 ) + self.recur(arr, index-i-1, k, hashMap)) hashMap[index] = result return result
Given an integer array arr, you should partition the array into (contiguous) subarrays of length at most k. After partitioning, each subarray has their values changed to become the maximum value of that subarray. Return the largest sum of the given array after partitioning.   Example 1: Input: arr = [1,15,7,9,2,5,10], k = 3 Output: 84 Explanation: arr becomes [15,15,15,9,10,10,10] Example 2: Input: arr = [1,4,1,5,7,3,6,1,9,9,3], k = 4 Output: 83 Example 3: Input: arr = [1], k = 1 Output: 1   Constraints: 1 <= arr.length <= 500 0 <= arr[i] <= 109 1 <= k <= arr.length
class Solution: def maxSumAfterPartitioning(self, arr: List[int], k: int) -> int: self.dp = {} def recur(index,arr,k): if index >= len(arr): return 0 if index in self.dp: return self.dp[index] maxi = 0 res = 0 for i in range(k): if index+i < len(arr): maxi = max(maxi, arr[index+i]) res = max(res, maxi * (i+1) + recur(index + i + 1, arr, k)) self.dp[index] = res return res return recur(0, arr, k)