[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 (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:
Awill be a permutation of[0, 1, ..., A.length - 1].Awill 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 全局与局部的倒置的更多相关文章
- [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) ...
- 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 ...
- 【LeetCode】775. Global and Local Inversions 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/global-a ...
- 【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 ...
- 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) ...
- python 局部local和全局global变量
global和local变量 虽然简单,但是还是记录一下,主要是转载 转载自:http://blog.sina.com.cn/s/blog_436992740102ux8z.html 先看一段代码 ...
- 侧脸生成正脸概论与精析(一)Global and Local Perception GAN
侧脸生成正脸我一直很感兴趣,老早就想把这块理一理的.今天来给大家分享一篇去年的老文章,如果有不对的地方,请斧正. Beyond Face Rotation: Global and Local Perc ...
- [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) ...
- JNDI在server.xml中的配置(全局和局部的)
总结: 全局就是在数据源server.xml中配置,然后通过和项目名相同的xml来进行映射.对所有的项目都起作用.那个项目需要就在对应的tomcat下配置一个与项目名相同的xml映射文件. 局部的就是 ...
随机推荐
- RubyMine 2017.3.2破解教程
下载地址:http://www.3322.cc/soft/35519.html RubyMine 2017.3.2破解版是一款专为Ruby和Rails开发者准备的IDE(被誉为最智能的Ruby和Rai ...
- python之路(1)数据类型
目录 整型 布尔值 字符串 列表 元组 字典 整型(int) 将字符串转换成整型 num = "123" v = int(num) 2. 将字符串按进制位转换成整型 num = & ...
- js值类型转换(boolean/String/number),js运算符,if条件,循环结构,函数,三种弹出框
js值类型转换 number | string | boolean boolean类型转换 num = 0; var b1 = Boolean(num); console.log(b1) 转化为数字类 ...
- 第五节: EF高级属性(一) 之 本地缓存、立即加载、延迟加载(不含导航属性)
一. 本地缓存 从这个章节开始,介绍一下EF的一些高级特性,这里介绍的首先介绍的EF的本地缓存,在前面的“EF增删改”章节中介绍过该特性(SaveChanges一次性会作用于本地缓存中所有的状态的变化 ...
- C#获取应用程序路径
string s = Environment.CurrentDirectory; //需添加Forms.DLL s = System.Windows.Forms.Application.Startup ...
- Pytorch里的CrossEntropyLoss详解
在使用Pytorch时经常碰见这些函数cross_entropy,CrossEntropyLoss, log_softmax, softmax.看得我头大,所以整理本文以备日后查阅. 首先要知道上面提 ...
- CF1119A Ilya and a Colorful Walk
题目地址:CF1119A Ilya and a Colorful Walk \(O(n^2)\) 肯定过不掉 记 \(p_i\) 为从下标 \(1\) 开始连续出现 \(i\) 的个数 那么对于每一个 ...
- POJ 3304 Segments(直线)
题目: Description Given n segments in the two dimensional space, write a program, which determines if ...
- DCM、PLL、PMCD、MMCM相关
摘自网上 : http://xilinx.eetop.cn/viewnews-1482 The DCM is a Digital Clock Manager - at its heart it is ...
- 轮播swiper配置选项
本文主要介绍了swiper配置选项,包含了轮播的无限滚动.懒加载.监听当前位置.上下翻页.过渡动画渐变.延时加载图片.自动轮播等: swiper官方链接DEMO <!DOCTYPE html&g ...