We have some permutation A of [0, 1, ..., N - 1], where N is the length of A.

The number of (global) inversions is the number of i < j with 0 <= i < j < N and A[i] > A[j].

The number of local inversions is the number of i with 0 <= i < N and A[i] > A[i+1].

Return true if and only if the number of global inversions is equal to the number of local inversions.

Example 1:

Input: A = [1,0,2]
Output: true
Explanation: There is 1 global inversion, and 1 local inversion.

Example 2:

Input: A = [1,2,0]
Output: false
Explanation: There are 2 global inversions, and 1 local inversion.

Note:

  • A will be a permutation of [0, 1, ..., A.length - 1].
  • A will have length in range [1, 5000].
  • The time limit for this problem has been reduced.

这道题给了一个长度为n的数组,里面是0到n-1数字的任意排序。又定义了两种倒置方法,全局倒置和局部倒置。其中全局倒置说的是坐标小的值大,局部倒置说的是相邻的两个数,坐标小的值大。那么我们可以发现,其实局部倒置是全局倒置的一种特殊情况,即局部倒置一定是全局倒置,而全局倒置不一定是局部倒置,这是解这道题的关键点。题目让我们判断该数组的全局倒置和局部倒置的个数是否相同,那么我们想,什么情况下会不相同?如果所有的倒置都是局部倒置,那么由于局部倒置一定是全局倒置,则二者个数一定相等。如果出现某个全局倒置不是局部倒置的情况,那么二者的个数一定不会相等。所以问题的焦点就变成了是否能找出不是局部倒置的全局倒置。所以为了和局部倒置区别开来,我们不能比较相邻的两个,而是至少要隔一个来比较。我们可以从后往前遍历数组,遍历到第三个数字停止,然后维护一个 [i, n-1] 范围内的最小值,每次和 A[i - 2] 比较,如果小于 A[i - 2],说明这是个全局的倒置,并且不是局部倒置,那么我们直接返回false即可,参见代码如下:

解法一:

class Solution {
public:
bool isIdealPermutation(vector<int>& A) {
int n = A.size(), mn = INT_MAX;
for (int i = n - ; i >= ; --i) {
mn = min(mn, A[i]);
if (A[i - ] > mn) return false;
}
return true;
}
};

同理,我们可以反其道行之,我们可以从前往后遍历数组,遍历到倒数第三个数字停止,然后维护一个 [0, i] 范围内的最大值,每次和 A[i + 2] 比较,如果大于 A[i + 2],说明这是个全局的倒置,并且不是局部倒置,那么我们直接返回false即可,参见代码如下:

解法二:

class Solution {
public:
bool isIdealPermutation(vector<int>& A) {
int n = A.size(), mx = INT_MIN;
for (int i = ; i < n - ; ++i) {
mx = max(mx, A[i]);
if (A[i + ] < mx) return false;
}
return true;
}
};

其实这道题最叼最炫酷的解法是下面这种解法,最早由grandyang大神的帖子中提出,嗯??grandyang大神??没错,就是博主本人啦,哈哈,秀不秀?!?这个解法也是博主脑子灵光一现而想出的,由于原数组正常的顺序应该是 [0, 1, 2, 3, 4...] 这种,即数字和其下标是相同的,所以如果我们发现乱序数组中某个数字和其坐标差的绝对值大于1的话,那么一定是有非局部倒置的全局倒置的存在。猛然这么一说,可能你会问为啥啊?因为0到n-1中每个数字都是在数组中存在的,如果当前数字 A[i] 比起坐标 i 大1的话,比如 A[i] = 3, i = 1 的时候,那么数组的第二个数字是3了,前三个数字suppose是 0,1,2 的,但是由于第二个数字是3了,那么一定会有一个小于3的数字被挤出前三个数字,这个小于3的数字最多出现在下标为3的位置上,那么由于数字3出现在了下标为1的位置上,所以non-local的全局倒置就出现了。同理,如果当前数字 A[i] 比其坐标 i 小1的话,比如 A[i] = 1, i = 3 的时候,那么就是后 n-i 个数字中有一个大于 A[i] 的数字被挤到了前面去了,而且其跟 A[i] 的距离最小为2,所以non-local的全局倒置就出现了,大声告诉博主,这个解法精彩不精彩?

解法三:

class Solution {
public:
bool isIdealPermutation(vector<int>& A) {
for (int i = ; i < A.size(); ++i) {
if (abs(A[i] - i) > ) return false;
}
return true;
}
};

参考资料:

https://leetcode.com/problems/global-and-local-inversions/solution/

https://leetcode.com/problems/global-and-local-inversions/discuss/113656/My-3-lines-C++-Solution

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Global and Local Inversions 全局与局部的倒置的更多相关文章

  1. [Swift]LeetCode775. 全局倒置与局部倒置 | Global and Local Inversions

    We have some permutation Aof [0, 1, ..., N - 1], where N is the length of A. The number of (global) ...

  2. 775. Global and Local Inversions局部取反和全局取反

    [抄题]: We have some permutation A of [0, 1, ..., N - 1], where N is the length of A. The number of (g ...

  3. 【LeetCode】775. Global and Local Inversions 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/global-a ...

  4. 【leetcode】Global and Local Inversions

    题目如下: We have some permutation A of [0, 1, ..., N - 1], where N is the length of A. The number of (g ...

  5. 775. Global and Local Inversions

    We have some permutation A of [0, 1, ..., N - 1], where N is the length of A. The number of (global) ...

  6. python 局部local和全局global变量

    global和local变量 虽然简单,但是还是记录一下,主要是转载 转载自:http://blog.sina.com.cn/s/blog_436992740102ux8z.html   先看一段代码 ...

  7. 侧脸生成正脸概论与精析(一)Global and Local Perception GAN

    侧脸生成正脸我一直很感兴趣,老早就想把这块理一理的.今天来给大家分享一篇去年的老文章,如果有不对的地方,请斧正. Beyond Face Rotation: Global and Local Perc ...

  8. [leetcode-775-Global and Local Inversions]

    We have some permutation A of [0, 1, ..., N - 1], where N is the length of A. The number of (global) ...

  9. JNDI在server.xml中的配置(全局和局部的)

    总结: 全局就是在数据源server.xml中配置,然后通过和项目名相同的xml来进行映射.对所有的项目都起作用.那个项目需要就在对应的tomcat下配置一个与项目名相同的xml映射文件. 局部的就是 ...

随机推荐

  1. 第十七节: EF的CodeFirst模式的四种初始化策略和通过Migration进行数据的迁移

    一. 四种初始化策略 EF的CodeFirst模式下数据库的初始化有四种策略: 1. CreateDatabaseIfNotExists:EF的默认策略,数据库不存在,生成数据库:一旦model发生变 ...

  2. [再寄小读者之数学篇](2014-10-08 乘积型 Sobolev 不等式)

    $$\bex n\geq 2, 1\leq p<n\ra \sen{f}_{L^\frac{np}{n-p}(\bbR^n)} \leq C\prod_{k=1}^n \sen{\p_k f}_ ...

  3. [物理学与PDEs]第5章第2节 变形的描述, 应变张量 2.1 变形梯度张量

    $$\bex \rd{\bf y}={\bf F}\rd {\bf x}, \eex$$ 其中 ${\bf F}=\n_x{\bf y}=\sex{\cfrac{\p y_i}{\p x_j}}$ 为 ...

  4. 应用留数定理计算实积分 $\dps{I(x)=\int_{-1}^1\frac{\rd t}{\sqrt{1-t^2}(t-x)}\ (|x|>1,x\in\bbR)}$ [华中师范大学2010年复变函数复试试题]

    应用留数定理计算实积分 $\dps{I(x)=\int_{-1}^1\frac{\rd t}{\sqrt{1-t^2}(t-x)}\ (|x|>1,x\in\bbR)}$ [华中师范大学2010 ...

  5. fstat函数

    一.函数原型 #include<sys/stat.h> #include<unistd.h> int fstat(int fildes,struct stat *buf); 返 ...

  6. java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or.....

    1,<activity android:name=".DialogActivity" android:theme="@android:style/Theme.Dia ...

  7. 有趣的F-String

    F-String 让人上瘾 一个工具脚本的例子 https://www.pydanny.com/python-f-string-are-fun.html 在Python3.6的发布中,我们看到他们采纳 ...

  8. CENTOS 7 安装 TINYPROXY 代理服务器

    https://www.cnblogs.com/new_2050/p/7658508.html

  9. JAVA 列表输入学生的信息

    package Code429; import java.util.ArrayList; public class CodeArrayListStudent { public static void ...

  10. Java面试题复习笔记(框架)

    1.什么是框架? 为解决一个开放性问题而设计的具有一定约束性的支撑结构,再次结构上可以根据具体问题扩展,安插更多的组成部分,从而更迅速和方便地构建完整解决问题的方案. 2.MVC模式? 用一种业务逻辑 ...