PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#CC0000028. Chèn ít nhất để tạo dãy con (Minimum Operations to Make a Subsequence)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Chèn ít nhất để tạo dãy con (Minimum Operations to Make a Subsequence)
Minimum Operations to Make a Subsequence
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
You are given arrays target and arr. All values in target are distinct; arr may contain duplicates. In one operation, you may insert any integer anywhere in arr. Determine the minimum number of insertions required so that target becomes a subsequence of arr. A subsequence preserves order but need not consist of consecutive elements.
Input
The first line contains , the lengths of target and arr. The second line contains target values; the third contains array values.
Output
Print the minimum number of insertions.
Subtasks
- Subtask 1 (20 points): ; ; all elements of
targetare distinct. - Subtask 2 (30 points): ; ; all elements of
targetare distinct. - Subtask 3 (50 points): ; ; all elements of
targetare distinct.
Examples
Input
3 5
5 1 3
9 4 2 3 4
Output
2
Explanation
We can keep the existing 3 and insert 5 and 1 before it, requiring two insertions.