Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d59fceb

Browse files
author
robot
committed
feat: 1713
1 parent d85a52b commit d59fceb

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,7 @@ leetcode 题解,记录自己的 leetcode 解题之路。
494494
- [1649. 通过指令创建有序数组](./problems/1649.create-sorted-array-through-instructions.md)
495495
- [1671. 得到山形数组的最少删除次数](./problems/1671.minimum-number-of-removals-to-make-mountain-array.md)
496496
- [1707. 与数组中元素的最大异或值](./problems/5640.maximum-xor-with-an-element-from-array.md)
497+
- [1713. 得到子序列的最少操作次数](./problems/1713.minimum-operations-to-make-a-subsequence.md)
497498
- [1723. 完成所有工作的最短时间](./problems/1723.find-minimum-time-to-finish-all-jobs.md)
498499
- [1787. 使所有区间的异或结果为零](./problems/1787.make-the-xor-of-all-segments-equal-to-zero.md)
499500
- [1835. 所有数对按位与结果的异或和](./problems/1835.find-xor-sum-of-all-pairs-bitwise-and.md)

‎SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@
323323
- [1649. 通过指令创建有序数组](./problems/1649.create-sorted-array-through-instructions.md)
324324
- [1671. 得到山形数组的最少删除次数](./problems/1671.minimum-number-of-removals-to-make-mountain-array.md)
325325
- [1707. 与数组中元素的最大异或值](./problems/5640.maximum-xor-with-an-element-from-array.md)
326+
- [1713. 得到子序列的最少操作次数](./problems/1713.minimum-operations-to-make-a-subsequence.md)
326327
- [1723. 完成所有工作的最短时间](./problems/1723.find-minimum-time-to-finish-all-jobs.md) 🆕
327328
- [1787. 使所有区间的异或结果为零](./problems/1787.make-the-xor-of-all-segments-equal-to-zero.md) 🆕
328329
- [1835. 所有数对按位与结果的异或和](./problems/1835.find-xor-sum-of-all-pairs-bitwise-and.md) 🆕
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
## 题目地址(1713. 得到子序列的最少操作次数)
2+
3+
https://leetcode-cn.com/problems/minimum-operations-to-make-a-subsequence/
4+
5+
## 题目描述
6+
7+
```
8+
给你一个数组 target ,包含若干 互不相同 的整数,以及另一个整数数组 arr ,arr 可能 包含重复元素。
9+
10+
每一次操作中,你可以在 arr 的任意位置插入任一整数。比方说,如果 arr = [1,4,1,2] ,那么你可以在中间添加 3 得到 [1,4,3,1,2] 。你可以在数组最开始或最后面添加整数。
11+
12+
请你返回 最少 操作次数,使得 target 成为 arr 的一个子序列。
13+
14+
一个数组的 子序列 指的是删除原数组的某些元素(可能一个元素都不删除),同时不改变其余元素的相对顺序得到的数组。比方说,[2,7,4] 是 [4,2,3,7,2,1,4] 的子序列(加粗元素),但 [2,4,2] 不是子序列。
15+
16+
17+
18+
示例 1:
19+
20+
输入:target = [5,1,3], arr = [9,4,2,3,4]
21+
输出:2
22+
解释:你可以添加 5 和 1 ,使得 arr 变为 [5,9,4,1,2,3,4] ,target 为 arr 的子序列。
23+
24+
25+
示例 2:
26+
27+
输入:target = [6,4,8,1,3,2], arr = [4,7,6,2,3,8,6,1]
28+
输出:3
29+
30+
31+
32+
33+
提示:
34+
35+
1 <= target.length, arr.length <= 105
36+
1 <= target[i], arr[i] <= 109
37+
target 不包含任何重复元素。
38+
```
39+
40+
## 前置知识
41+
42+
- 动态规划
43+
- LIS
44+
45+
## 公司
46+
47+
- 暂无
48+
49+
## 思路
50+
51+
这道题属于最长上升子序列的换皮题。关于最长上升子序列可以参考我之前写的文章[穿上衣服我就不认识你了?来聊聊最长上升子序列](https://lucifer.ren/blog/2020/06/20/LIS/)
52+
53+
具体的思路为:
54+
55+
1. 新建一个空的列表 B
56+
2. 遍历 arr, 如果 arr 中的数字存在于 target 中,将其索引添加到列表 B 中
57+
3. 求列表的最长上升子序列的长度(典型算法),这个长度实际上就是我们可以利用 arr 中的数所能组成的 **最长的** target 的子序列,换句话说,我们添加最少的数就可以构成 target。
58+
4. 答案就是 target 的长度减去最长子序列的长度。
59+
60+
- 为了加快第 2 步的速度,可以建立 target 的反向索引方便能够在 $O(1)$ 时间根据 val 获取到索引。
61+
- 由于这道题数据范围是 10ドル^5,ドル因此只能使用 $NlogN$ 的贪心才行。
62+
63+
> 关于为什么 10 ^ 5 就必须使用 $NlogN$ 甚至更优的算法我在[刷题技巧](https://lucifer.ren/blog/2020/12/21/shuati-silu3/)提过。更多复杂度速查可参考我的刷题插件,公众号《力扣加加》回复插件获取即可
64+
65+
## 关键点
66+
67+
- LIS
68+
69+
## 代码
70+
71+
```py
72+
class Solution:
73+
def minOperations(self, target: List[int], A: List[int]) -> int:
74+
def LIS(A):
75+
d = []
76+
for a in A:
77+
i = bisect.bisect_left(d, a)
78+
if d and i < len(d):
79+
d[i] = a
80+
else:
81+
d.append(a)
82+
return len(d)
83+
B = []
84+
target = { t:i for i, t in enumerate(target)}
85+
for a in A:
86+
if a in target: B.append(target[a])
87+
return len(target) - LIS(B)
88+
```
89+
90+
**复杂度分析**
91+
92+
- 时间复杂度:$O(max(BlogB, A))$。
93+
- 空间复杂度:由于 target 大小大于 B 的大小,因此空间复杂度为 $O(target)$。
94+
95+
以上就是本文的全部内容了。大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 40K star 啦。大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。
96+
97+
关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。
98+
99+
![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /