6090. 极大极小游戏
给你一个下标从 0 开始的整数数组 nums ,其长度是 2 的幂。
对 nums 执行下述算法:
- 设 n 等于 nums 的长度,如果 n == 1 ,终止 算法过程。否则,创建 一个新的整数数组 newNums ,新数组长度为 n / 2 ,下标从 0 开始。
 - 对于满足 0 <= i < n / 2 的每个 偶数 下标 i ,将 newNums[i] 赋值 为 min(nums[2 i], nums[2 i + 1]) 。
 - 对于满足 0 <= i < n / 2 的每个 奇数 下标 i ,将 newNums[i] 赋值 为 max(nums[2 i], nums[2 i + 1]) 。
 - 用 newNums 替换 nums 。
 - 从步骤 1 开始 重复 整个过程。
 
执行算法后,返回 nums 中剩下的那个数字。
示例 1:
输入:nums = [1,3,5,2,4,8,2,2] 输出:1 解释:重复执行算法会得到下述数组。 第一轮:nums = [1,5,4,2] 第二轮:nums = [1,4] 第三轮:nums = [1] 1 是最后剩下的那个数字,返回 1 。 
示例 2:
输入:nums = [3] 输出:3 解释:3 就是最后剩下的数字,返回 3 。 
提示:
- 1 <= nums.length <= 1024
 - 1 <= nums[i] <= 109
 - nums.length 是 2 的幂
 
思路:
递归
class Solution {public int minMaxGame(int[] nums) {return dfs(nums, nums.length);}int dfs(int[] nums, int n) {if (n == 1)return nums[0];int idx = 0;for (int i = 0; i < n; i++) {int x = nums[i++], y = nums[i];if (idx % 2 == 0) {nums[idx] = Math.min(x, y);} else {nums[idx] = Math.max(x, y);}idx++;}return dfs(nums, idx);}}
6091. 划分数组使最大差为 K
给你一个整数数组 nums 和一个整数 k 。你可以将 nums 划分成一个或多个 子序列 ,使 nums 中的每个元素都 恰好 出现在一个子序列中。
在满足每个子序列中最大值和最小值之间的差值最多为 k 的前提下,返回需要划分的 最少 子序列数目。
子序列 本质是一个序列,可以通过删除另一个序列中的某些元素(或者不删除)但不改变剩下元素的顺序得到。
示例 1:
输入:nums = [3,6,1,2,5], k = 2 输出:2 解释: 可以将 nums 划分为两个子序列 [3,1,2] 和 [6,5] 。 第一个子序列中最大值和最小值的差值是 3 - 1 = 2 。 第二个子序列中最大值和最小值的差值是 6 - 5 = 1 。 由于创建了两个子序列,返回 2 。可以证明需要划分的最少子序列数目就是 2 。 
示例 2:
输入:nums = [1,2,3], k = 1 输出:2 解释: 可以将 nums 划分为两个子序列 [1,2] 和 [3] 。 第一个子序列中最大值和最小值的差值是 2 - 1 = 1 。 第二个子序列中最大值和最小值的差值是 3 - 3 = 0 。 由于创建了两个子序列,返回 2 。注意,另一种最优解法是将 nums 划分成子序列 [1] 和 [2,3] 。 
示例 3:
输入:nums = [2,2,4,5], k = 0 输出:3 解释: 可以将 nums 划分为三个子序列 [2,2]、[4] 和 [5] 。 第一个子序列中最大值和最小值的差值是 2 - 2 = 0 。 第二个子序列中最大值和最小值的差值是 4 - 4 = 0 。 第三个子序列中最大值和最小值的差值是 5 - 5 = 0 。 由于创建了三个子序列,返回 3 。可以证明需要划分的最少子序列数目就是 3 。 
提示:
- 1 <= nums.length <= 105
 - 0 <= nums[i] <= 105
 - 0 <= k <= 105
 
思路:
贪心
class Solution {public int partitionArray(int[] nums, int k) {Arrays.sort(nums);int pre = nums[0];int cnt = 1;for (int x : nums) {if (x > pre + k) {cnt++;pre = x;}}return cnt;}}
6092. 替换数组中的元素
给你一个下标从 0 开始的数组 nums ,它包含 n 个 互不相同 的正整数。请你对这个数组执行 m 个操作,在第 i 个操作中,你需要将数字 operations[i][0] 替换成 operations[i][1] 。
题目保证在第 i 个操作中:
- operations[i][0] 在 nums 中存在。
 - operations[i][1] 在 nums 中不存在。
 
请你返回执行完所有操作后的数组。
示例 1:
输入:nums = [1,2,4,6], operations = [[1,3],[4,7],[6,1]] 输出:[3,2,7,1] 解释:我们对 nums 执行以下操作: - 将数字 1 替换为 3 。nums 变为 [3,2,4,6] 。 - 将数字 4 替换为 7 。nums 变为 [3,2,7,6] 。 - 将数字 6 替换为 1 。nums 变为 [3,2,7,1] 。 返回最终数组 [3,2,7,1] 。 
示例 2:
输入:nums = [1,2], operations = [[1,3],[2,1],[3,2]] 输出:[2,1] 解释:我们对 nums 执行以下操作: - 将数字 1 替换为 3 。nums 变为 [3,2] 。 - 将数字 2 替换为 1 。nums 变为 [3,1] 。 - 将数字 3 替换为 2 。nums 变为 [2,1] 。 返回最终数组 [2,1] 。 
提示:
- n == nums.length
 - m == operations.length
 - 1 <= n, m <= 105
 - nums 中所有数字 互不相同 。
 - operations[i].length == 2
 - 1 <= nums[i], operations[i][0], operations[i][1] <= 106
 - 在执行第 i 个操作时,operations[i][0] 在 nums 中存在。
 - 在执行第 i 个操作时,operations[i][1] 在 nums 中不存在。
 
思路:
染色问题
倒序 + 并查集
补充,这个思路想复杂了,本题没那么难,每个数的修改都是独立的。。。
class Solution {public int[] arrayChange(int[] nums, int[][] op) {int[] hash = new int[1000010];Arrays.fill(hash, -1);for (int i = op.length - 1; i >= 0; i--) {int a = op[i][0], b = op[i][1];if (hash[b] != -1)hash[a] = hash[b];elsehash[a] = b;}for (int i = 0; i < nums.length; i++) {int x = nums[i];if (hash[x] != -1)nums[i] = hash[x];}return nums;}}
// 本题的顺序遍历class Solution {public int[] arrayChange(int[] nums, int[][] operations) {Map<Integer, Integer> map = new HashMap<>();for (int i = 0; i < nums.length; i++) {map.put(nums[i], i);}for (int[] p : operations) {int x = p[0], y = p[1];int id = map.get(x);map.put(y, id);nums[id] = y;}return nums;}}
6093. 设计一个文本编辑器
- 通过的用户数947
 - 尝试过的用户数1511
 - 用户总通过次数1038
 - 用户总提交次数2821
 - 题目难度Hard
 
请你设计一个带光标的文本编辑器,它可以实现以下功能:
- 添加:在光标所在处添加文本。
 - 删除:在光标所在处删除文本(模拟键盘的删除键)。
 - 移动:将光标往左或者往右移动。
 
当删除文本时,只有光标左边的字符会被删除。光标会留在文本内,也就是说任意时候 0 <= cursor.position <= currentText.length 都成立。
请你实现 TextEditor 类:
- TextEditor() 用空文本初始化对象。
 - void addText(string text) 将 text 添加到光标所在位置。添加完后光标在 text 的右边。
 - int deleteText(int k) 删除光标左边 k 个字符。返回实际删除的字符数目。
 - string cursorLeft(int k) 将光标向左移动 k 次。返回移动后光标左边 min(10, len) 个字符,其中 len 是光标左边的字符数目。
 - string cursorRight(int k) 将光标向右移动 k 次。返回移动后光标左边 min(10, len) 个字符,其中 len 是光标左边的字符数目。
 
示例 1:
输入:
[“TextEditor”, “addText”, “deleteText”, “addText”, “cursorRight”, “cursorLeft”, “deleteText”, “cursorLeft”, “cursorRight”]
[[], [“leetcode”], [4], [“practice”], [3], [8], [10], [2], [6]]
输出:
[null, null, 4, null, “etpractice”, “leet”, 4, “”, “practi”]
解释:
TextEditor textEditor = new TextEditor(); // 当前 text 为 “|” 。(’|’ 字符表示光标)
textEditor.addText(“leetcode”); // 当前文本为 “leetcode|” 。
textEditor.deleteText(4); // 返回 4
// 当前文本为 “leet|” 。
// 删除了 4 个字符。
textEditor.addText(“practice”); // 当前文本为 “leetpractice|” 。
textEditor.cursorRight(3); // 返回 “etpractice”
// 当前文本为 “leetpractice|”.
// 光标无法移动到文本以外,所以无法移动。
// “etpractice” 是光标左边的 10 个字符。
textEditor.cursorLeft(8); // 返回 “leet”
// 当前文本为 “leet|practice” 。
// “leet” 是光标左边的 min(10, 4) = 4 个字符。
textEditor.deleteText(10); // 返回 4
// 当前文本为 “|practice” 。
// 只有 4 个字符被删除了。
textEditor.cursorLeft(2); // 返回 “”
// 当前文本为 “|practice” 。
// 光标无法移动到文本以外,所以无法移动。
// “” 是光标左边的 min(10, 0) = 0 个字符。
textEditor.cursorRight(6); // 返回 “practi”
// 当前文本为 “practi|ce” 。
// “practi” 是光标左边的 min(10, 6) = 6 个字符。
提示:
- 1 <= text.length, k <= 40
 - text 只含有小写英文字母。
 - 调用 addText ,deleteText ,cursorLeft 和 cursorRight 的 总 次数不超过 2 * 104 次。
 
思路:
两个双端队列模拟
class TextEditor {Deque<Character> pre = new ArrayDeque<>();Deque<Character> last = new ArrayDeque<>();StringBuilder sb = new StringBuilder();public TextEditor() {}public void addText(String text) {sb.append(text);for (int i = 0; i < text.length(); i++)pre.offer(text.charAt(i));}public int deleteText(int k) {int cnt = 0;while (!pre.isEmpty() && k-- > 0) {cnt ++;pre.pollLast();sb.deleteCharAt(sb.length() - 1);}return cnt;}public String cursorLeft(int k) {while (!pre.isEmpty() && k-- > 0) {char c = pre.pollLast();last.offerFirst(c);sb.deleteCharAt(sb.length() - 1);}return sb.substring(Math.max(0, sb.length() - 10), sb.length());}public String cursorRight(int k) {while (!last.isEmpty() && k-- > 0) {char c = last.poll();pre.offer(c);sb.append(c);}return sb.substring(Math.max(0, sb.length() - 10), sb.length());}}/*** Your TextEditor object will be instantiated and called as such:* TextEditor obj = new TextEditor();* obj.addText(text);* int param_2 = obj.deleteText(k);* String param_3 = obj.cursorLeft(k);* String param_4 = obj.cursorRight(k);*/

