diff --git "a/Index/\346\225\260\345\255\246.md" "b/Index/\346\225\260\345\255\246.md" index 2c3a4e6c..7a6df2a6 100644 --- "a/Index/\346\225\260\345\255\246.md" +++ "b/Index/\346\225\260\345\255\246.md" @@ -47,6 +47,7 @@ | [645. 错误的集合](https://leetcode-cn.com/problems/set-mismatch/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/set-mismatch/solution/gong-shui-san-xie-yi-ti-san-jie-ji-shu-s-vnr9/) | 简单 | 🤩🤩🤩 | | [650. 只有两个键的键盘](https://leetcode-cn.com/problems/2-keys-keyboard/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/2-keys-keyboard/solution/gong-shui-san-xie-yi-ti-san-jie-dong-tai-f035/) | 中等 | 🤩🤩🤩🤩 | | [754. 到达终点数字](https://leetcode.cn/problems/reach-a-number/) | [LeetCode 题解链接](https://leetcode.cn/problems/reach-a-number/solution/by-ac_oier-o4ze/) | 中等 | 🤩🤩🤩🤩 | +| [775. 全局倒置与局部倒置](https://leetcode.cn/problems/global-and-local-inversions/) | [LeetCode 题解链接](https://leetcode.cn/problems/global-and-local-inversions/solution/by-ac_oier-jc7a/) | 中等 | 🤩🤩🤩🤩 | | [780. 到达终点](https://leetcode-cn.com/problems/reaching-points/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reaching-points/solution/by-ac_oier-hw11/) | 困难 | 🤩🤩🤩🤩🤩 | | [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/) | 困难 | 🤩🤩🤩🤩 | diff --git "a/Index/\346\240\221\347\212\266\346\225\260\347\273\204.md" "b/Index/\346\240\221\347\212\266\346\225\260\347\273\204.md" index cd4b61f9..932e2049 100644 --- "a/Index/\346\240\221\347\212\266\346\225\260\347\273\204.md" +++ "b/Index/\346\240\221\347\212\266\346\225\260\347\273\204.md" @@ -7,6 +7,7 @@ | [354. 俄罗斯套娃信封问题](https://leetcode-cn.com/problems/russian-doll-envelopes/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/russian-doll-envelopes/solution/zui-chang-shang-sheng-zi-xu-lie-bian-xin-6s8d/) | 困难 | 🤩🤩🤩 | | [467. 环绕字符串中唯一的子字符串](https://leetcode.cn/problems/unique-substrings-in-wraparound-string/) | [LeetCode 题解链接](https://leetcode.cn/problems/unique-substrings-in-wraparound-string/solution/by-ac_oier-qteu/) | 中等 | 🤩🤩🤩🤩 | | [673. 最长递增子序列的个数](https://leetcode-cn.com/problems/number-of-longest-increasing-subsequence/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/number-of-longest-increasing-subsequence/solution/gong-shui-san-xie-lis-de-fang-an-shu-wen-obuz/) | 中等 | 🤩🤩🤩🤩 | +| [775. 全局倒置与局部倒置](https://leetcode.cn/problems/global-and-local-inversions/) | [LeetCode 题解链接](https://leetcode.cn/problems/global-and-local-inversions/solution/by-ac_oier-jc7a/) | 中等 | 🤩🤩🤩🤩 | | [1310. 子数组异或查询](https://leetcode-cn.com/problems/xor-queries-of-a-subarray/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/xor-queries-of-a-subarray/solution/gong-shui-san-xie-yi-ti-shuang-jie-shu-z-rcgu/) | 中等 | 🤩🤩🤩🤩 | | [1395. 统计作战单位数](https://leetcode.cn/problems/count-number-of-teams/) | [LeetCode 题解链接](https://leetcode.cn/problems/count-number-of-teams/solution/by-ac_oier-qm3a/) | 中等 | 🤩🤩🤩🤩🤩 | | [1893. 检查是否区域内所有整数都被覆盖](https://leetcode-cn.com/problems/check-if-all-the-integers-in-a-range-are-covered/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/check-if-all-the-integers-in-a-range-are-covered/solution/gong-shui-san-xie-yi-ti-shuang-jie-mo-ni-j83x/) | 简单 | 🤩🤩🤩🤩 | diff --git "a/LeetCode/771-780/775. \345\205\250\345\261\200\345\200\222\347\275\256\344\270\216\345\261\200\351\203\250\345\200\222\347\275\256\357\274\210\344\270\255\347\255\211\357\274\211.md" "b/LeetCode/771-780/775. \345\205\250\345\261\200\345\200\222\347\275\256\344\270\216\345\261\200\351\203\250\345\200\222\347\275\256\357\274\210\344\270\255\347\255\211\357\274\211.md" new file mode 100644 index 00000000..2f456e1d --- /dev/null +++ "b/LeetCode/771-780/775. \345\205\250\345\261\200\345\200\222\347\275\256\344\270\216\345\261\200\351\203\250\345\200\222\347\275\256\357\274\210\344\270\255\347\255\211\357\274\211.md" @@ -0,0 +1,139 @@ +### 题目描述 + +这是 LeetCode 上的 **[775. 全局倒置与局部倒置](https://leetcode.cn/problems/global-and-local-inversions/solution/by-ac_oier-jc7a/)** ,难度为 **中等**。 + +Tag : 「树状数组」、「数学」 + + + +给你一个长度为 `n` 的整数数组 `nums`,表示由范围 $[0, n - 1]$ 内所有整数组成的一个排列。 + +全局倒置 的数目等于满足下述条件不同下标对 (i, j) 的数目: + +* $0 <= i < j < n$ +* $nums[i] > nums[j]$ + +局部倒置 的数目等于满足下述条件的下标 i 的数目: + +* $0 <= i < n - 1$ +* $nums[i] > nums[i + 1]$ + +当数组 `nums` 中 全局倒置 的数量等于 局部倒置 的数量时,返回 `true` ;否则,返回 `false` 。 + +示例 1: +``` +输入:nums = [1,0,2] + +输出:true + +解释:有 1 个全局倒置,和 1 个局部倒置。 +``` +示例 2: +``` +输入:nums = [1,2,0] + +输出:false + +解释:有 2 个全局倒置,和 1 个局部倒置。 +``` + +提示: +* $n == nums.length$ +* $1 <= n <= 10^5$ +* $0 <= nums[i] < n$ +* `nums` 中的所有整数 互不相同 +* `nums` 是范围 $[0, n - 1]$ 内所有数字组成的一个排列 + +--- + +### 树状数组 + +根据题意,对于每个 $nums[i]$ 而言: + +* 其左边比它大的 $nums[j]$ 的个数,是以 $nums[i]$ 为右端点的“全局倒置”数量,统计所有以 $nums[i]$ 为右端点的“全局倒置”数量即是总的“全局倒置”数量 `a` + +* 同时我们可以将每个 $nums[i]$ 与前一个值进行比较,从而统计总的“局部倒置”数量 `b`,其中 $i$ 的取值范围为 $[1, n - 1)$ + +一个容易想到的做法是利用「树状数组」,虽然该做法没有利用到核心条件「$nums$ 是一个 $[0, n - 1]$ 的排列」,但根据数据范围 $n$ 可知该复杂度为 $O(n\log{n})$ 的做法可过,且依赖的条件更少,适用范围更广。 + +代码: +```Java +class Solution { + int n; + int[] tr; + int lowbit(int x) { + return x & -x; + } + void add(int x) { + for (int i = x; i <= n; i += lowbit(i)) tr[i]++; + } + int query(int x) { + int ans = 0; + for (int i = x; i > 0; i -= lowbit(i)) ans += tr[i]; + return ans; + } + public boolean isIdealPermutation(int[] nums) { + n = nums.length; + tr = new int[n + 10]; + add(nums[0] + 1); + int a = 0, b = 0; + for (int i = 1; i < n; i++) { + a += query(n) - query(nums[i] + 1); + b += nums[i] < nums[i - 1] ? 1 : 0; + add(nums[i] + 1); + } + return a == b; + } +} +``` +* 时间复杂度:$O(n\log{n})$ +* 空间复杂度:$O(n)$ + +--- + +### 数学 + +解法一中并没有利用到核心条件「$nums$ 是一个 $[0, n - 1]$ 的排列」,我们可以从该条件以及两类倒置的定义出发进行分析。 + +#### 提示一:由“局部倒置”组成的集合为由“全局倒置”组成的集合的子集 + +任意一个“局部倒置”均满足“全局倒置”的定义,因此要判定两者数量是否相同,可转换为统计是否存在「不满足“局部倒置”定义的“全局倒置”」。 + +#### 提示二:何为不满足“局部倒置”定义的“全局倒置” + +结合题意,若存在坐标 $j$ 和 $i$,满足 $nums[j] > nums[i]$ 且 $j + 1 < i$,那么该倒置满足“全局倒置”定义,且不满足“局部倒置”定义。 + +若存在这样的逆序对,不满足,则有两类倒置数量不同。 + +#### 提示三:考虑「如何构造」或「如何避免构造」不满足“全局倒置”定义的“局部倒置” + +如果我们能够总结出「如何构造」或「如何避免构造」一个不满足“全局倒置”定义的“局部倒置” 所需的条件,问题可以转换为检查 `nums` 是否满足这样的条件,来得知 `nums` 是否存在不满足“全局倒置”定义的“局部倒置”。 + +我们可以结合「$nums$ 是一个 $[0, n - 1]$ 的排列」来分析,若需要避免所有 $nums[j] > nums[i]$ 的逆序对均不满足 $j + 1 < i$,只能是所有逆序对均由相邻数值产生。 + +代码: +```Java +class Solution { + public boolean isIdealPermutation(int[] nums) { + for (int i = 0; i < nums.length; i++) { + if (Math.abs(nums[i] - i) >= 2) return false; + } + return true; + } +} +``` +* 时间复杂度:$O(n)$ +* 空间复杂度:$O(1)$ + +--- + +### 最后 + +这是我们「刷穿 LeetCode」系列文章的第 `No.775` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 + +在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 + +为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。 + +在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。 +