diff --git "a/Index/\346\225\260\345\255\246.md" "b/Index/\346\225\260\345\255\246.md" index b09217bf..78e4eaaf 100644 --- "a/Index/\346\225\260\345\255\246.md" +++ "b/Index/\346\225\260\345\255\246.md" @@ -50,6 +50,7 @@ | [789. 逃脱阻碍者](https://leetcode-cn.com/problems/escape-the-ghosts/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/escape-the-ghosts/solution/gong-shui-san-xie-noxiang-xin-ke-xue-xi-w69gr/) | 中等 | 🤩🤩🤩🤩🤩 | | [793. 阶乘函数后 K 个零](https://leetcode.cn/problems/preimage-size-of-factorial-zeroes-function/) | [LeetCode 题解链接](https://leetcode.cn/problems/preimage-size-of-factorial-zeroes-function/solution/by-ac_oier-pk9g/) | 困难 | 🤩🤩🤩🤩 | | [810. 黑板异或游戏](https://leetcode-cn.com/problems/chalkboard-xor-game/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/chalkboard-xor-game/solution/gong-shui-san-xie-noxiang-xin-ke-xue-xi-ges7k/) | 困难 | 🤩🤩🤩🤩 | +| [828. 统计子串中的唯一字符](https://leetcode.cn/problems/count-unique-characters-of-all-substrings-of-a-given-string/) | [LeetCode 题解链接](https://leetcode.cn/problems/count-unique-characters-of-all-substrings-of-a-given-string/solution/by-ac_oier-922k/) | 困难 | 🤩🤩🤩🤩 | | [829. 连续整数求和](https://leetcode.cn/problems/consecutive-numbers-sum/) | [LeetCode 题解链接](https://leetcode.cn/problems/consecutive-numbers-sum/solution/by-ac_oier-220q/) | 困难 | 🤩🤩🤩🤩 | | [869. 重新排序得到 2 的幂](https://leetcode-cn.com/problems/reordered-power-of-2/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reordered-power-of-2/solution/gong-shui-san-xie-yi-ti-shuang-jie-dfs-c-3s1e/) | 中等 | 🤩🤩🤩🤩 | | [879. 盈利计划](https://leetcode-cn.com/problems/profitable-schemes/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/profitable-schemes/solution/gong-shui-san-xie-te-shu-duo-wei-fei-yon-7su9/) | 困难 | 🤩🤩🤩🤩🤩 | diff --git "a/Index/\346\236\204\351\200\240.md" "b/Index/\346\236\204\351\200\240.md" index a3812394..84dc7ceb 100644 --- "a/Index/\346\236\204\351\200\240.md" +++ "b/Index/\346\236\204\351\200\240.md" @@ -2,6 +2,7 @@ | ------------------------------------------------------------ | ------------------------------------------------------------ | ---- | -------- | | [324. 摆动排序 II](https://leetcode.cn/problems/wiggle-sort-ii/) | [LeetCode 题解链接](https://leetcode.cn/problems/wiggle-sort-ii/solution/by-ac_oier-22bq/) | 中等 | 🤩🤩🤩🤩 | | [406. 根据身高重建队列](https://leetcode.cn/problems/queue-reconstruction-by-height/) | [LeetCode 题解链接](https://leetcode.cn/problems/queue-reconstruction-by-height/solution/by-ac_oier-fda2/) | 中等 | 🤩🤩🤩🤩 | +| [667. 优美的排列 II](https://leetcode.cn/problems/beautiful-arrangement-ii/) | [LeetCode 题解链接](https://leetcode.cn/problems/beautiful-arrangement-ii/solution/by-ac_oier-lyns/) | 中等 | 🤩🤩🤩🤩🤩 | | [761. 特殊的二进制序列](https://leetcode.cn/problems/special-binary-string/) | [LeetCode 题解链接](https://leetcode.cn/problems/special-binary-string/solution/by-ac_oier-cz6h/) | 困难 | 🤩🤩🤩🤩🤩 | | [782. 变为棋盘](https://leetcode.cn/problems/transform-to-chessboard/) | [LeetCode 题解链接](https://leetcode.cn/problems/transform-to-chessboard/solution/by-ac_oier-vf1m/) | 困难 | 🤩🤩🤩🤩 | | [899. 有序队列](https://leetcode.cn/problems/orderly-queue/) | [LeetCode 题解链接](https://leetcode.cn/problems/orderly-queue/solution/by-ac_oier-443m/) | 困难 | 🤩🤩🤩 | diff --git "a/Index/\346\250\241\346\213\237.md" "b/Index/\346\250\241\346\213\237.md" index e25ec4fc..a6b4e495 100644 --- "a/Index/\346\250\241\346\213\237.md" +++ "b/Index/\346\250\241\346\213\237.md" @@ -107,6 +107,7 @@ | [819. 最常见的单词](https://leetcode-cn.com/problems/most-common-word/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/most-common-word/solution/by-ac_oier-6aqd/) | 简单 | 🤩🤩🤩🤩 | | [821. 字符的最短距离](https://leetcode-cn.com/problems/shortest-distance-to-a-character/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/shortest-distance-to-a-character/solution/by-ac_oier-5bjs/) | 简单 | 🤩🤩🤩🤩 | | [824. 山羊拉丁文](https://leetcode-cn.com/problems/goat-latin/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/goat-latin/solution/by-ac_oier-t7hj/) | 简单 | 🤩🤩🤩🤩 | +| [828. 统计子串中的唯一字符](https://leetcode.cn/problems/count-unique-characters-of-all-substrings-of-a-given-string/) | [LeetCode 题解链接](https://leetcode.cn/problems/count-unique-characters-of-all-substrings-of-a-given-string/solution/by-ac_oier-922k/) | 困难 | 🤩🤩🤩🤩 | | [846. 一手顺子](https://leetcode-cn.com/problems/hand-of-straights/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/hand-of-straights/solution/gong-shui-san-xie-shu-ju-jie-gou-mo-ni-t-4hxw/) | 中等 | 🤩🤩🤩 | | [859. 亲密字符串](https://leetcode-cn.com/problems/buddy-strings/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/buddy-strings/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-q056/) | 简单 | 🤩🤩🤩🤩🤩 | | [867. 转置矩阵](https://leetcode-cn.com/problems/transpose-matrix/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/transpose-matrix/solution/yi-you-wei-jin-huo-xu-ni-neng-kan-kan-zh-m53m/) | 简单 | 🤩🤩🤩🤩 | diff --git "a/Index/\350\204\221\347\255\213\346\200\245\350\275\254\345\274\257.md" "b/Index/\350\204\221\347\255\213\346\200\245\350\275\254\345\274\257.md" index 6ab1a2cc..baf7ac25 100644 --- "a/Index/\350\204\221\347\255\213\346\200\245\350\275\254\345\274\257.md" +++ "b/Index/\350\204\221\347\255\213\346\200\245\350\275\254\345\274\257.md" @@ -4,6 +4,7 @@ | [406. 根据身高重建队列](https://leetcode.cn/problems/queue-reconstruction-by-height/) | [LeetCode 题解链接](https://leetcode.cn/problems/queue-reconstruction-by-height/solution/by-ac_oier-fda2/) | 中等 | 🤩🤩🤩 | | [419. 甲板上的战舰](https://leetcode-cn.com/problems/battleships-in-a-board/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/battleships-in-a-board/solution/gong-shui-san-xie-ji-chong-sao-miao-xian-trmc/) | 中等 | 🤩🤩🤩🤩 | | [423. 从英文中重建数字](https://leetcode-cn.com/problems/reconstruct-original-digits-from-english/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reconstruct-original-digits-from-english/solution/gong-shui-san-xie-nao-jin-ji-zhuan-wan-m-vg7a/) | 中等 | 🤩🤩🤩🤩 | +| [667. 优美的排列 II](https://leetcode.cn/problems/beautiful-arrangement-ii/) | [LeetCode 题解链接](https://leetcode.cn/problems/beautiful-arrangement-ii/solution/by-ac_oier-lyns/) | 中等 | 🤩🤩🤩🤩 | | [908. 最小差值 I](https://leetcode.cn/problems/smallest-range-i/) | [LeetCode 题解链接](https://leetcode.cn/problems/smallest-range-i/solution/by-ac_oier-7fh0/) | 简单 | 🤩🤩🤩🤩 | | [2038. 如果相邻两个颜色均相同则删除当前颜色](https://leetcode-cn.com/problems/remove-colored-pieces-if-both-neighbors-are-the-same-color/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/remove-colored-pieces-if-both-neighbors-are-the-same-color/solution/gong-shui-san-xie-nao-jin-ji-zhuan-wan-y-a8xm/) | 中等 | 🤩🤩🤩🤩🤩 | | [2069. 模拟行走机器人 II](https://leetcode-cn.com/problems/walking-robot-simulation-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/walking-robot-simulation-ii/solution/by-ac_oier-6zib/) | 中等 | 🤩🤩🤩🤩 | diff --git "a/LeetCode/661-670/667. \344\274\230\347\276\216\347\232\204\346\216\222\345\210\227 II\357\274\210\344\270\255\347\255\211\357\274\211.md" "b/LeetCode/661-670/667. \344\274\230\347\276\216\347\232\204\346\216\222\345\210\227 II\357\274\210\344\270\255\347\255\211\357\274\211.md" new file mode 100644 index 00000000..f51f2cc3 --- /dev/null +++ "b/LeetCode/661-670/667. \344\274\230\347\276\216\347\232\204\346\216\222\345\210\227 II\357\274\210\344\270\255\347\255\211\357\274\211.md" @@ -0,0 +1,87 @@ +### 题目描述 + +这是 LeetCode 上的 **[667. 优美的排列 II](https://leetcode.cn/problems/beautiful-arrangement-ii/solution/by-ac_oier-lyns/)** ,难度为 **中等**。 + +Tag : 「构造」、「脑筋急转弯」 + + + +给你两个整数 `n` 和 `k` ,请你构造一个答案列表 `answer`,该列表应当包含从 `1` 到 `n` 的 `n` 个不同正整数,并同时满足下述条件: + +假设该列表是 $answer = [a_1, a_2, a_3, ... , a_n]$ ,那么列表 $[|a_1 - a_2|, |a_2 - a_3|, |a_3 - a_4|, ... , |a_{n-1} - a_n|]$ 中应该有且仅有 k 个不同整数。 + +返回列表 `answer`。如果存在多种答案,只需返回其中 任意一种 。 + +示例 1: +``` +输入:n = 3, k = 1 + +输出:[1, 2, 3] + +解释:[1, 2, 3] 包含 3 个范围在 1-3 的不同整数,并且 [1, 1] 中有且仅有 1 个不同整数:1 +``` +示例 2: +``` +输入:n = 3, k = 2 + +输出:[1, 3, 2] + +解释:[1, 3, 2] 包含 3 个范围在 1-3 的不同整数,并且 [2, 1] 中有且仅有 2 个不同整数:1 和 2 +``` + +提示: +* $1 <= k < n <= 10^4$ + +--- + +### 构造 + +给定范围在 $[1, n]$ 的 $n$ 个数,当「直接升序/降序」排列时,相邻项差值为 $1$,仅一种;而从首位开始按照「升序」间隔排列,次位开始按照「降序」间隔排列(即排列为 `[1, n, 2, n - 1, 3, ...]`)时,相邻差值会从 $n - 1$ 开始递减至 $1$,共 $n - 1$ 种。 + +那么当我们需要构造 $k$ 种序列时,我们可以先通过「直接升序」的方式构造出若干个 $1$,然后再通过「间隔位分别升降序」的方式构造出从 $k$ 到 $1$ 的差值,共 $k$ 个。 + +显然,我们需要 $k + 1$ 个数来构造出 $k$ 个差值。因此我们可以先从 $1$ 开始,使用 $n - (k + 1)$ 个数来直接升序(通过方式一构造出若干个 $1$),然后从 $n - k$ 开始间隔升序排列,按照 $n$ 开始间隔降序排列,构造出剩下的序列。 + +Java 代码: +```Java +class Solution { + public int[] constructArray(int n, int k) { + int[] ans = new int[n]; + int t = n - k - 1; + for (int i = 0; i < t; i++) ans[i] = i + 1; + for (int i = t, a = n - k, b = n; i < n; ) { + ans[i++] = a++; + if (i < n) ans[i++] = b--; + } + return ans; + } +} +``` +TypeScript 代码: +```TypeScript +function constructArray(n: number, k: number): number[] { + const ans = new Array(n).fill(0) + const t = n - k - 1 + for (let i = 0; i < t; i++) ans[i] = i + 1 + for (let i = t, a = n - k, b = n; i < n; ) { + ans[i++] = a++ + if (i < n) ans[i++] = b-- + } + return ans +}; +``` +* 时间复杂度:$O(n)$ +* 空间复杂度:$O(n)$ + +--- + +### 最后 + +这是我们「刷穿 LeetCode」系列文章的第 `No.667` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 + +在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 + +为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。 + +在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。 + diff --git "a/LeetCode/821-830/828. \347\273\237\350\256\241\345\255\220\344\270\262\344\270\255\347\232\204\345\224\257\344\270\200\345\255\227\347\254\246\357\274\210\345\233\260\351\232\276\357\274\211.md" "b/LeetCode/821-830/828. \347\273\237\350\256\241\345\255\220\344\270\262\344\270\255\347\232\204\345\224\257\344\270\200\345\255\227\347\254\246\357\274\210\345\233\260\351\232\276\357\274\211.md" new file mode 100644 index 00000000..3477bff6 --- /dev/null +++ "b/LeetCode/821-830/828. \347\273\237\350\256\241\345\255\220\344\270\262\344\270\255\347\232\204\345\224\257\344\270\200\345\255\227\347\254\246\357\274\210\345\233\260\351\232\276\357\274\211.md" @@ -0,0 +1,122 @@ +### 题目描述 + +这是 LeetCode 上的 **[828. 统计子串中的唯一字符](https://leetcode.cn/problems/count-unique-characters-of-all-substrings-of-a-given-string/solution/by-ac_oier-922k/)** ,难度为 **困难**。 + +Tag : 「模拟」、「数学」 + + + +我们定义了一个函数 `countUniqueChars(s)` 来统计字符串 `s` 中的唯一字符,并返回唯一字符的个数。 + +例如:`s = "LEETCODE"` ,则其中 `"L"`, `"T"`,`"C"`,`"O"`,`"D"` 都是唯一字符,因为它们只出现一次,所以 `countUniqueChars(s) = 5`。 + +本题将会给你一个字符串 `s` ,我们需要返回 `countUniqueChars(t)` 的总和,其中 `t` 是 `s` 的子字符串。输入用例保证返回值为 `32` 位整数。 + +注意,某些子字符串可能是重复的,但你统计时也必须算上这些重复的子字符串(也就是说,你必须统计 `s` 的所有子字符串中的唯一字符)。 + +示例 1: +``` +输入: s = "ABC" + +输出: 10 + +解释: 所有可能的子串为:"A","B","C","AB","BC" 和 "ABC"。 + 其中,每一个子串都由独特字符构成。 + 所以其长度总和为:1 + 1 + 1 + 2 + 2 + 3 = 10 +``` +示例 2: +``` +输入: s = "ABA" + +输出: 8 + +解释: 除了 countUniqueChars("ABA") = 1 之外,其余与示例 1 相同。 +``` +示例 3: +``` +输入:s = "LEETCODE" + +输出:92 +``` + +提示: +* $1 <= s.length <= 10^5$ +* `s` 只包含大写英文字符 + +--- + +### 模拟 + 乘法原理 + +这道题和 [907. 子数组的最小值之和](https://mp.weixin.qq.com/s?__biz=MzU4NDE3MTEyMA==&mid=2247493485&idx=1&sn=c6d6ac25c171e2617c5a95173aa58825) 如出一辙,只不过无须考虑运用「单调栈」。 + +原问题为求所有子数组的唯一字符数量和,其可等价为求每个 $s[i]$ 对答案的贡献,即每个 $s[i]$ 可作为多少个子数组的唯一元素。 + +假定我们能预处理出两数组 `l` 和 `r` 分别代表 $s[i]$ 作为子数组唯一字符时,其所能到达的最远两端: +* `l[i] = a` 代表下标 $a$ 为 $s[i]$ 能够作为子数组唯一字符时的最远左边界,即为 $s[i]$ 左边第一个与 $s[i]$ 值相同的位置(若不存在,则为 $a = -1$) +* `r[i] = b` 代表跳表 $b$ 为 $s[i]$ 能够作为子数组唯一字符时的最远右边界,即为 $s[i]$ 右边第一个与 $s[i]$ 值相同的位置(若不存在,则为 $b = n$) + +子数组左端点个数为 $(i - a)$ 个,右端点个数为 $(b - i)$ 个,根据乘法原理可知,子数组个数为两者乘积。 + +预处理 `l` 和 `r` 只需要使用遍历计数即可。 + +Java 代码: +```Java +class Solution { + public int uniqueLetterString(String s) { + char[] cs = s.toCharArray(); + int n = cs.length, ans = 0; + int[] l = new int[n], r = new int[n]; + int[] idx = new int[26]; + Arrays.fill(idx, -1); + for (int i = 0; i < n; i++) { + int u = cs[i] - 'A'; + l[i] = idx[u]; + idx[u] = i; + } + Arrays.fill(idx, n); + for (int i = n - 1; i >= 0; i--) { + int u = cs[i] - 'A'; + r[i] = idx[u]; + idx[u] = i; + } + for (int i = 0; i < n; i++) ans += (i - l[i]) * (r[i] - i); + return ans; + } +} +``` +Typescript 代码: +```Typescript +function uniqueLetterString(s: string): number { + let n = s.length, ans = 0 + const l = new Array(n), r = new Array(n) + const idx = new Array(26).fill(-1) + for (let i = 0; i < n; i++) { + const u = s.charCodeAt(i) - 65 + l[i] = idx[u] + idx[u] = i + } + idx.fill(n) + for (let i = n - 1; i >= 0; i--) { + const u = s.charCodeAt(i) - 65 + r[i] = idx[u] + idx[u] = i + } + for (let i = 0; i < n; i++) ans += (i - l[i]) * (r[i] - i) + return ans +}; +``` +* 时间复杂度:$O(n)$ +* 空间复杂度:$O(n)$ + +--- + +### 最后 + +这是我们「刷穿 LeetCode」系列文章的第 `No.828` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 + +在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 + +为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。 + +在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。 +