-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1713-MinimumOperationsToMakeASubsequence.go
More file actions
73 lines (63 loc) · 2.38 KB
/
1713-MinimumOperationsToMakeASubsequence.go
File metadata and controls
73 lines (63 loc) · 2.38 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
package main
// 1713. Minimum Operations to Make a Subsequence
// You are given an array target that consists of distinct integers
// and another integer array arr that can have duplicates.
// In one operation, you can insert any integer at any position in arr.
// For example, if arr = [1,4,1,2], you can add 3 in the middle and make it [1,4,3,1,2].
// Note that you can insert the integer at the very beginning or end of the array.
// Return the minimum number of operations needed to make target a subsequence of arr.
// A subsequence of an array is a new array generated
// from the original array by deleting some elements (possibly none) without changing the remaining elements' relative order.
// For example, [2,7,4] is a subsequence of [4,2,3,7,2,1,4] (the underlined elements), while [2,4,2] is not.
// Example 1:
// Input: target = [5,1,3], arr = [9,4,2,3,4]
// Output: 2
// Explanation: You can add 5 and 1 in such a way that makes arr = [5,9,4,1,2,3,4], then target will be a subsequence of arr.
// Example 2:
// Input: target = [6,4,8,1,3,2], arr = [4,7,6,2,3,8,6,1]
// Output: 3
// Constraints:
// 1 <= target.length, arr.length <= 10^5
// 1 <= target[i], arr[i] <= 10^9
// target contains no duplicates.
import "fmt"
import "sort"
func minOperations(target []int, arr []int) int {
mp := make(map[int]int)
for i, v := range target {
mp[v] = i
}
for i, v := range arr {
if val, ok := mp[v]; ok {
arr[i] = val
} else {
arr[i] = -1
}
}
dp := make([]int,0)
for _, v := range arr {
if v == -1 { continue }
if len(dp) == 0 {
dp = append(dp,v)
} else if v > dp[len(dp) - 1] {
dp = append(dp, v)
} else {
j := sort.Search(len(dp), func (i int) bool {
return dp[i] >= v
})
dp[j] = v
}
}
return len(target) - len(dp)
}
func main() {
// Example 1:
// Input: target = [5,1,3], arr = [9,4,2,3,4]
// Output: 2
// Explanation: You can add 5 and 1 in such a way that makes arr = [5,9,4,1,2,3,4], then target will be a subsequence of arr.
fmt.Println(minOperations([]int{5,1,3}, []int{9,4,2,3,4})) // 2
// Example 2:
// Input: target = [6,4,8,1,3,2], arr = [4,7,6,2,3,8,6,1]
// Output: 3
fmt.Println(minOperations([]int{6,4,8,1,3,2}, []int{4,7,6,2,3,8,6,1})) // 3
}