-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
1035 Uncrossed Lines.swift
43 lines (37 loc) 路 1.1 KB
/
1035 Uncrossed Lines.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//
// 1035 Uncrossed Lines.swift
// LeetCode-Solutions
//
// Created by Aleksandar Dinic on 25/05/2020.
// Copyright 漏 2020 Aleksandar Dinic. All rights reserved.
//
import Foundation
/// Source: https://leetcode.com/problems/uncrossed-lines/
class Solution {
/// Calculates the maximum number of uncrossed Lines.
///
/// - Parameters:
/// - A: An array A.
/// - B: An array B.
/// - Returns: The maximum number of connecting lines.
///
/// - Complexity:
/// - time: O(n * m), where n is the length of A and m is the length of B.
/// - space: O(m), where m is the length of *B*.
func maxUncrossedLines(_ A: [Int], _ B: [Int]) -> Int {
var dp = [Int](repeating: 0, count: B.count+1)
for i in 1...A.count {
var prev = 0
for j in 1...B.count {
let cur = dp[j]
if A[i-1] == B[j-1] {
dp[j] = prev + 1
} else {
dp[j] = max(dp[j-1], dp[j])
}
prev = cur
}
}
return dp[B.count]
}
}