1713:得到子序列的最少操作次数

This commit is contained in:
huangge1199@hotmail.com 2021-07-26 22:12:56 +08:00
parent 62fce5b6e4
commit 4ca840ca8d
3 changed files with 129 additions and 1 deletions

View File

@ -0,0 +1,95 @@
//给你一个数组 target 包含若干 互不相同 的整数以及另一个整数数组 arr arr 可能 包含重复元素
//
// 每一次操作中你可以在 arr 的任意位置插入任一整数比方说如果 arr = [1,4,1,2] 那么你可以在中间添加 3 得到 [1,4,3,1,
//2] 你可以在数组最开始或最后面添加整数
//
// 请你返回 最少 操作次数使得 target 成为 arr 的一个子序列
//
// 一个数组的 子序列 指的是删除原数组的某些元素可能一个元素都不删除同时不改变其余元素的相对顺序得到的数组比方说[2,7,4] [4,2,3,
//7,2,1,4] 的子序列加粗元素 [2,4,2] 不是子序列
//
//
//
// 示例 1
//
// 输入target = [5,1,3], arr = [9,4,2,3,4]
//输出2
//解释你可以添加 5 1 使得 arr 变为 [5,9,4,1,2,3,4] target arr 的子序列
//
//
// 示例 2
//
// 输入target = [6,4,8,1,3,2], arr = [4,7,6,2,3,8,6,1]
//输出3
//
//
//
//
// 提示
//
//
// 1 <= target.length, arr.length <= 105
// 1 <= target[i], arr[i] <= 109
// target 不包含任何重复元素
//
// Related Topics 贪心 数组 哈希表 二分查找
// 👍 119 👎 0
package leetcode.editor.cn;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//1713:得到子序列的最少操作次数
class MinimumOperationsToMakeASubsequence {
public static void main(String[] args) {
//测试代码
Solution solution = new MinimumOperationsToMakeASubsequence().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int minOperations(int[] target, int[] arr) {
int n = target.length;
Map<Integer, Integer> pos = new HashMap<Integer, Integer>();
for (int i = 0; i < n; ++i) {
pos.put(target[i], i);
}
List<Integer> d = new ArrayList<Integer>();
for (int val : arr) {
if (pos.containsKey(val)) {
int idx = pos.get(val);
int it = binarySearch(d, idx);
if (it != d.size()) {
d.set(it, idx);
} else {
d.add(idx);
}
}
}
return n - d.size();
}
public int binarySearch(List<Integer> d, int target) {
int size = d.size();
if (size == 0 || d.get(size - 1) < target) {
return size;
}
int low = 0, high = size - 1;
while (low < high) {
int mid = (high - low) / 2 + low;
if (d.get(mid) < target) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,33 @@
<p>给你一个数组 <code>target</code> ,包含若干 <strong>互不相同</strong> 的整数,以及另一个整数数组 <code>arr</code> <code>arr</code> <strong>可能</strong> 包含重复元素。</p>
<p>每一次操作中,你可以在 <code>arr</code> 的任意位置插入任一整数。比方说,如果 <code>arr = [1,4,1,2]</code> ,那么你可以在中间添加 <code>3</code> 得到 <code>[1,4,<strong>3</strong>,1,2]</code> 。你可以在数组最开始或最后面添加整数。</p>
<p>请你返回 <strong>最少</strong> 操作次数,使得<em> </em><code>target</code><em> </em>成为 <code>arr</code> 的一个子序列。</p>
<p>一个数组的 <strong>子序列</strong> 指的是删除原数组的某些元素(可能一个元素都不删除),同时不改变其余元素的相对顺序得到的数组。比方说,<code>[2,7,4]</code> 是 <code>[4,<strong>2</strong>,3,<strong>7</strong>,2,1,<strong>4</strong>]</code> 的子序列(加粗元素),但 <code>[2,4,2]</code> 不是子序列。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre><b>输入:</b>target = [5,1,3], <code>arr</code> = [9,4,2,3,4]
<b>输出:</b>2
<b>解释:</b>你可以添加 5 和 1 ,使得 arr 变为 [<strong>5</strong>,9,4,<strong>1</strong>,2,3,4] target 为 arr 的子序列。
</pre>
<p><strong>示例 2</strong></p>
<pre><b>输入:</b>target = [6,4,8,1,3,2], <code>arr</code> = [4,7,6,2,3,8,6,1]
<b>输出:</b>3
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= target.length, arr.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= target[i], arr[i] &lt;= 10<sup>9</sup></code></li>
<li><code>target</code> 不包含任何重复元素。</li>
</ul>
<div><div>Related Topics</div><div><li>贪心</li><li>数组</li><li>哈希表</li><li>二分查找</li></div></div>\n<div><li>👍 120</li><li>👎 0</li></div>

File diff suppressed because one or more lines are too long