-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1940-LongestCommonSubsequenceBetweenSortedArrays.go
More file actions
122 lines (109 loc) · 3.4 KB
/
1940-LongestCommonSubsequenceBetweenSortedArrays.go
File metadata and controls
122 lines (109 loc) · 3.4 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
// 1940. Longest Common Subsequence Between Sorted Arrays
// Given an array of integer arrays arrays where each arrays[i] is sorted in strictly increasing order,
// return an integer array representing the longest common subsequence among all the arrays.
// A subsequence is a sequence that can be derived from another sequence
// by deleting some elements (possibly none) without changing the order of the remaining elements.
// Example 1:
// Input: arrays = [[1,3,4],
// [1,4,7,9]]
// Output: [1,4]
// Explanation: The longest common subsequence in the two arrays is [1,4].
// Example 2:
// Input: arrays = [[2,3,6,8],
// [1,2,3,5,6,7,10],
// [2,3,4,6,9]]
// Output: [2,3,6]
// Explanation: The longest common subsequence in all three arrays is [2,3,6].
// Example 3:
// Input: arrays = [[1,2,3,4,5],
// [6,7,8]]
// Output: []
// Explanation: There is no common subsequence between the two arrays.
// Constraints:
// 2 <= arrays.length <= 100
// 1 <= arrays[i].length <= 100
// 1 <= arrays[i][j] <= 100
// arrays[i] is sorted in strictly increasing order.
import "fmt"
import "sort"
func longestCommonSubsequence(arrays [][]int) []int {
res, mp := []int{}, make(map[int]bool)
for _, v := range arrays[0] {
mp[v] = true
}
for _, rows := range arrays[1:] {
mp1 := make(map[int]bool)
for _, v := range rows {
if mp[v] {
mp1[v] = true
}
}
if len(mp1) == 0 { return res }
mp = mp1
}
for i := range mp {
res = append(res, i)
}
sort.Ints(res)
return res
}
func longestCommonSubsequence1(arrays [][]int) []int { // 【 计数解法: 】
n := len(arrays)
res, count := []int{}, make([]int, 101)
for _, rows := range arrays {
for _, v := range rows {
count[v] += 1
}
}
for i := 1; i <= 100; i++ {
if count[i] == n {
res = append(res, i)
}
}
return res
}
func main() {
// Example 1:
// Input: arrays = [[1,3,4],
// [1,4,7,9]]
// Output: [1,4]
// Explanation: The longest common subsequence in the two arrays is [1,4].
arr1 := [][]int{
{1,3,4},
{1,4,7,9},
}
fmt.Println(longestCommonSubsequence(arr1)) // [1,4]
// Example 2:
// Input: arrays = [[2,3,6,8],
// [1,2,3,5,6,7,10],
// [2,3,4,6,9]]
// Output: [2,3,6]
// Explanation: The longest common subsequence in all three arrays is [2,3,6].
arr2 := [][]int{
{2,3,6,8},
{1,2,3,5,6,7,10},
{2,3,4,6,9},
}
fmt.Println(longestCommonSubsequence(arr2)) // [2,3,6]
// Example 3:
// Input: arrays = [[1,2,3,4,5],
// [6,7,8]]
// Output: []
// Explanation: There is no common subsequence between the two arrays.
arr3 := [][]int{
{1,2,3,4,5},
{6,7,8},
}
fmt.Println(longestCommonSubsequence(arr3)) // [2 3 6]
arr4 := [][]int{
{2,3,6,8},
{1,2,3,5,6,7,10},
{2,3,4,6,9},
}
fmt.Println(longestCommonSubsequence(arr4)) // []
fmt.Println(longestCommonSubsequence1(arr1)) // [1,4]
fmt.Println(longestCommonSubsequence1(arr2)) // [2,3,6]
fmt.Println(longestCommonSubsequence1(arr3)) // [2 3 6]
fmt.Println(longestCommonSubsequence1(arr4)) // []
}