Skip to content

Commit 72b54b5

Browse files
committed
✨feat: Add 1713
1 parent ca368c0 commit 72b54b5

File tree

4 files changed

+172
-0
lines changed

4 files changed

+172
-0
lines changed

Index/二分.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
| [1438. 绝对差不超过限制的最长连续子数组](https://leetcode-cn.com/problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/solution/xiang-jie-er-fen-hua-dong-chuang-kou-dan-41g1/) | 中等 | 🤩🤩🤩 |
2626
| [1482. 制作 m 束花所需的最少天数](https://leetcode-cn.com/problems/minimum-number-of-days-to-make-m-bouquets/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/minimum-number-of-days-to-make-m-bouquets/solution/gong-shui-san-xie-li-yong-er-duan-xing-z-ysv4/) | 中等 | 🤩🤩🤩 |
2727
| [1707. 与数组中元素的最大异或值](https://leetcode-cn.com/problems/maximum-xor-with-an-element-from-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-xor-with-an-element-from-array/solution/gong-shui-san-xie-jie-zhe-ge-wen-ti-lai-lypqr/) | 困难 | 🤩🤩🤩 |
28+
| [1713. 得到子序列的最少操作次数](https://leetcode-cn.com/problems/minimum-operations-to-make-a-subsequence/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/minimum-operations-to-make-a-subsequence/solution/gong-shui-san-xie-noxiang-xin-ke-xue-xi-oj7yu/) | 困难 | 🤩🤩🤩 |
2829
| [1751. 最多可以参加的会议数目 II](https://leetcode-cn.com/problems/maximum-number-of-events-that-can-be-attended-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-number-of-events-that-can-be-attended-ii/solution/po-su-dp-er-fen-dp-jie-fa-by-ac_oier-88du/) | 困难 | 🤩🤩🤩 |
2930
| [1818. 绝对差值和](https://leetcode-cn.com/problems/minimum-absolute-sum-difference/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/minimum-absolute-sum-difference/solution/gong-shui-san-xie-tong-guo-er-fen-zhao-z-vrmq/) | 中等 | 🤩🤩🤩🤩🤩 |
3031
| [1838. 最高频元素的频数](https://leetcode-cn.com/problems/frequency-of-the-most-frequent-element/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/frequency-of-the-most-frequent-element/solution/gong-shui-san-xie-cong-mei-ju-dao-pai-xu-kxnk/) | 中等 | 🤩🤩🤩 |

Index/序列 DP.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@
66
| [978. 最长湍流子数组](https://leetcode-cn.com/problems/longest-turbulent-subarray/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/longest-turbulent-subarray/solution/xiang-jie-dong-tai-gui-hua-ru-he-cai-dp-3spgj/) | 中等 | 🤩🤩🤩 |
77
| [1035. 不相交的线](https://leetcode-cn.com/problems/uncrossed-lines/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/uncrossed-lines/solution/gong-shui-san-xie-noxiang-xin-ke-xue-xi-bkaas/) | 中等 | 🤩🤩🤩🤩 |
88
| [1143. 最长公共子序列](https://leetcode-cn.com/problems/longest-common-subsequence/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/longest-common-subsequence/solution/gong-shui-san-xie-zui-chang-gong-gong-zi-xq0h/) | 中等 | 🤩🤩🤩🤩 |
9+
| [1713. 得到子序列的最少操作次数](https://leetcode-cn.com/problems/minimum-operations-to-make-a-subsequence/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/minimum-operations-to-make-a-subsequence/solution/gong-shui-san-xie-noxiang-xin-ke-xue-xi-oj7yu/) | 困难 | 🤩🤩🤩🤩🤩 |
910

Index/贪心算法.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
| [781. 森林中的兔子](https://leetcode-cn.com/problems/rabbits-in-forest/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/rabbits-in-forest/solution/gong-shui-san-xie-noxiang-xin-ke-xue-xi-v17p5/) | 中等 | 🤩🤩🤩🤩 |
99
| [995. K 连续位的最小翻转次数](https://leetcode-cn.com/problems/minimum-number-of-k-consecutive-bit-flips/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/minimum-number-of-k-consecutive-bit-flips/solution/po-su-tan-xin-jie-fa-yu-tan-xin-chai-fen-4lyy/) | 困难 | 🤩🤩🤩 |
1010
| [1707. 与数组中元素的最大异或值](https://leetcode-cn.com/problems/maximum-xor-with-an-element-from-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-xor-with-an-element-from-array/solution/gong-shui-san-xie-jie-zhe-ge-wen-ti-lai-lypqr/) | 困难 | 🤩🤩🤩 |
11+
| [1713. 得到子序列的最少操作次数](https://leetcode-cn.com/problems/minimum-operations-to-make-a-subsequence/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/minimum-operations-to-make-a-subsequence/solution/gong-shui-san-xie-noxiang-xin-ke-xue-xi-oj7yu/) | 困难 | 🤩🤩🤩🤩🤩 |
1112
| [1736. 替换隐藏数字得到的最晚时间](https://leetcode-cn.com/problems/latest-time-by-replacing-hidden-digits/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/latest-time-by-replacing-hidden-digits/solution/gong-shui-san-xie-ti-huan-yin-cang-shu-z-2l1h/) | 简单 | 🤩🤩🤩🤩🤩 |
1213
| [1833. 雪糕的最大数量](https://leetcode-cn.com/problems/maximum-ice-cream-bars/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-ice-cream-bars/solution/gong-shui-san-xie-noxiang-xin-ke-xue-xi-yrhjx/) | 中等 | 🤩🤩🤩🤩🤩 |
1314
| [1846. 减小和重新排列数组后的最大元素](https://leetcode-cn.com/problems/maximum-element-after-decreasing-and-rearranging/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-element-after-decreasing-and-rearranging/solution/gong-shui-san-xie-noxiang-xin-ke-xue-xi-yh9qt/) | 中等 | 🤩🤩🤩🤩🤩 |
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[1713. 得到子序列的最少操作次数](https://leetcode-cn.com/problems/minimum-operations-to-make-a-subsequence/solution/gong-shui-san-xie-noxiang-xin-ke-xue-xi-oj7yu/)** ,难度为 **中等**
4+
5+
Tag : 「最长公共子序列」、「最长上升子序列」、「贪心」、「二分」
6+
7+
8+
9+
给你一个数组 target ,包含若干 互不相同 的整数,以及另一个整数数组 arr ,arr 可能 包含重复元素。
10+
11+
每一次操作中,你可以在 arr 的任意位置插入任一整数。比方说,如果 arr = [1,4,1,2] ,那么你可以在中间添加 3 得到 [1,4,3,1,2] 。你可以在数组最开始或最后面添加整数。
12+
13+
请你返回 最少 操作次数,使得 target 成为 arr 的一个子序列。
14+
15+
一个数组的 子序列 指的是删除原数组的某些元素(可能一个元素都不删除),同时不改变其余元素的相对顺序得到的数组。比方说,[2,7,4] 是 [4,2,3,7,2,1,4] 的子序列(加粗元素),但 [2,4,2] 不是子序列。
16+
17+
18+
示例 1:
19+
```
20+
输入:target = [5,1,3], arr = [9,4,2,3,4]
21+
22+
输出:2
23+
24+
解释:你可以添加 5 和 1 ,使得 arr 变为 [5,9,4,1,2,3,4] ,target 为 arr 的子序列。
25+
```
26+
示例 2:
27+
```
28+
输入:target = [6,4,8,1,3,2], arr = [4,7,6,2,3,8,6,1]
29+
30+
输出:3
31+
```
32+
33+
提示:
34+
* 1 <= target.length, arr.length <= $10^5$
35+
* 1 <= target[i], arr[i] <= $10^9$
36+
* target 不包含任何重复元素。
37+
38+
---
39+
40+
### 基本分析
41+
42+
为了方便,我们令 $target$ 长度为 $n$,$arr$ 长度为 $m$,$target$ 和 $arr$ 的最长公共子序列长度为 $max$,不难发现最终答案为 $n - max$。
43+
44+
因此从题面来说,这是一道最长公共子序列问题(LCS)。
45+
46+
但朴素求解 LCS 问题复杂度为 $O(n * m)$,使用状态定义「**$f[i][j]$ 为考虑 `a` 数组的前 $i$ 个元素和 `b` 数组的前 $j$ 个元素的最长公共子序列长度为多少**」进行求解。
47+
48+
而本题的数据范围为 $10^5$,使用朴素求解 LCS 的做法必然超时。
49+
50+
一个很显眼的切入点是 $target$ 数组元素各不相同,当 LCS 问题增加某些条件限制之后,会存在一些很有趣的性质。
51+
52+
其中一个经典的性质就是:**当其中一个数组元素各不相同时,最长公共子序列问题(LCS)可以转换为最长上升子序列问题(LIS)进行求解。同时最长上升子序列问题(LIS)存在使用「维护单调序列 + 二分」的贪心解法,复杂度为 $O(n\log{n})$。**
53+
54+
**因此本题可以通过「抽象成 LCS 问题」->「利用 $target$ 数组元素各不相同,转换为 LIS 问题」->「使用 LIS 的贪心解法」,做到 $O(n\log{n})$ 的复杂度。**
55+
56+
基本方向确定后,我们证明一下第 $2$ 步和第 $3$ 步的合理性与正确性。
57+
58+
---
59+
60+
### 证明
61+
62+
#### 1. 为何其中一个数组元素各不相同,LCS 问题可以转换为 LIS 问题?
63+
64+
**本质是利用「当其中一个数组元素各不相同时,这时候每一个“公共子序列”都对应一个不重复元素数组的下标数组“上升子序列”,反之亦然」。**
65+
66+
我们可以使用题目给定的两个数组($target$ 和 $arr$)理解上面的话。
67+
68+
由于 $target$ 元素各不相同,那么首先 $target$ 元素和其对应下标,具有唯一的映射关系。
69+
70+
然后我们可以**将重点放在两者的公共元素上(忽略非公共元素),每一个“公共子序列”自然对应了一个下标数组“上升子序列”,反之亦然**
71+
72+
注意:下图只画出了两个数组的某个片段,不要错误理解为两数组等长。
73+
74+
![image.png](https://pic.leetcode-cn.com/1627265496-KtDwZl-image.png)
75+
76+
如果存在某个“公共子序列”,根据“子序列”的定义,那么对应下标序列必然递增,也就是对应了一个“上升子序列”。
77+
78+
反过来,对于下标数组的某个“上升子序列”,首先意味着元素在 $target$ 出现过,并且出现顺序递增,符合“公共子序列”定义,即对应了一个“公共子序列”。
79+
80+
至此,我们将原问题 LCS 转换为了 LIS 问题。
81+
82+
#### 2. 贪心求解 LIS 问题的正确性证明?
83+
84+
朴素的 LIS 问题求解,我们需要定义一个 $f[i]$ 数组代表以 $nums[i]$ 为结尾的最长上升子序列的长度为多少。
85+
86+
对于某个 $f[i]$ 而言,我们需要往回检查 $[0, i - 1]$ 区间内,所有可以将 $nums[i]$ 接到后面的位置 $j$,在所有的 $f[j] + 1$ 中取最大值更新 $f[i]$。因此朴素的 LIS 问题复杂度是 $O(n^2)$ 的。
87+
88+
**LIS 的贪心解法则是维护一个额外 $g$ 数组,$g[len] = x$ 代表上升子序列长度为 $len$ 的上升子序列的「最小结尾元素」为 $x$。**
89+
90+
整理一下,我们总共有两个数组:
91+
* $f$ 动规数组:与朴素 LIS 解法的动规数组含义一致。$f[i]$ 代表以 $nums[i]$ 为结尾的上升子序列的最大长度;
92+
* $g$ 贪心数组:$g[len] = x$ 代表上升子序列长度为 $len$ 的上升子序列的「最小结尾元素」为 $x$。
93+
94+
由于我们计算 $f[i]$ 时,需要找到满足 $nums[j] < nums[i]$,同时取得最大 $f[j]$ 的位置 $j$。
95+
96+
我们期望通过 $g$ 数组代替线性遍历。
97+
98+
显然,如果 $g$ 数组具有「单调递增」特性的话,我们可以通过「二分」找到符合 $g[idx] < nums[i]$ 分割点 $idx$(下标最大),即利用 $O(\log{n})$ 复杂度找到最佳转移位置。
99+
100+
我们可以很容易 **通过反证法结合 $g$ 数组的定义来证明 $g$ 数组具有「单调递增」特性。**
101+
102+
假设存在某个位置 $i$ 和 $j$,且 $i < j$,不满足「单调递增」,即如下两种可能:
103+
104+
* $g[i] = g[j] = x$:这意味着某个值 $x$ 既能作为长度 $i$ 的上升子序列的最后一位,也能作为长度为 $j$ 的上升子序列的最后一位。
105+
根据我们对 $g$ 数组的定义,$g[i] = x$ 意味在所有长度为 $i$ 上升子序列中「最小结尾元素」为 $x$,但同时由于 $g[j] = x$,而且「上升子序列」必然是「严格单调」,因此我们可以通过删除长度为 $j$ 的子序列后面的元素(调整出一个长度为 $i$ 的子序列)来找到一个比 $g[i]$ 小的合法值。
106+
也就是我们找到了一个长度为 $i$ 的上升子序列,且最后一位元素必然严格小于 $x$。因此 $g[i] = g[j] = x$ 恒不成立;
107+
108+
* $g[i] > g[j] = x$:同理,如果存在一个长度为 $j$ 的合法上升子序列的「最小结尾元素」为 $x$ 的话,那么必然能够找到一个比 $x$ 小的值来更新 $g[i]$。即 $g[i] > g[j]$ 恒不成立。
109+
110+
根据全序关系,在证明 $g[i] = g[j]$ 和 $g[i] > g[j]$ 恒不成立后,可得 $g[i] < g[j]$ 恒成立。
111+
112+
至此,我们证明了 $g$ 数组具有单调性,从而证明了每一个 $f[i]$ 均与朴素 LIS 解法得到的值相同,即贪心解是正确的。
113+
114+
---
115+
116+
### 动态规划 + 贪心 + 二分
117+
118+
根据「基本分析 & 证明」,通过维护一个贪心数组 $g$,来更新动规数组 $f$,在求得「最长上升子序列」长度之后,利用「“公共子序列”和“上升子序列”」的一一对应关系,可以得出“最长公共子序列”长度,从而求解出答案。
119+
120+
代码:
121+
```Java
122+
class Solution {
123+
public int minOperations(int[] t, int[] arr) {
124+
int n = t.length, m = arr.length;
125+
Map<Integer, Integer> map = new HashMap<>();
126+
for (int i = 0; i < n; i++) {
127+
map.put(t[i], i);
128+
}
129+
List<Integer> list = new ArrayList<>();
130+
for (int i = 0; i < m; i++) {
131+
int x = arr[i];
132+
if (map.containsKey(x)) list.add(map.get(x));
133+
}
134+
int len = list.size();
135+
int[] f = new int[len], g = new int[len + 1];
136+
Arrays.fill(g, Integer.MAX_VALUE);
137+
int max = 0;
138+
for (int i = 0; i < len; i++) {
139+
int l = 0, r = len;
140+
while (l < r) {
141+
int mid = l + r + 1 >> 1;
142+
if (g[mid] < list.get(i)) l = mid;
143+
else r = mid - 1;
144+
}
145+
int clen = r + 1;
146+
f[i] = clen;
147+
g[clen] = Math.min(g[clen], list.get(i));
148+
max = Math.max(max, clen);
149+
}
150+
return n - max;
151+
}
152+
}
153+
```
154+
* 时间复杂度:通过 $O(n)$ 复杂度得到 $target$ 的下标映射关系;通过 $O(m)$ 复杂度得到映射数组 $list$;贪心求解 LIS 的复杂度为 $O(m\log{m})$。整体复杂度为 $O(n + m\log{m})$
155+
* 空间复杂度:$O(n + m)$
156+
157+
158+
---
159+
160+
### 最后
161+
162+
这是我们「刷穿 LeetCode」系列文章的第 `No.1713` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
163+
164+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
165+
166+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
167+
168+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
169+

0 commit comments

Comments
 (0)