[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) 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.
思路:
All local inversions are global inversions.
If the number of global inversions is equal to the number of local inversions,
it means that all global inversions in permutations are local inversions.
It also means that we can not find A[i] > A[j]
with i+2<=j
.
In other words, max(A[i]) < A[i+2]
In this first solution, I traverse A
and keep the current biggest number cmax
.
Then I check the condition cmax < A[i+2]
Here come this solutions:
bool isIdealPermutation(vector<int>& A)
{
int n = A.size() ,tmax= ;
for(int i = ;i<n-;i++)
{
tmax = max(tmax,A[i]);
if(tmax > A[i+]) return false;
}
return true;
}
还有就是根据题目给出的数据特点:数组中每一个元素都不重复 而且就是从0 到n-1的n个数,如果没有逆序存在的话,
那么每一个数字都会在自己的位置上;
I could only place i
at A[i-1]
,A[i]
or A[i+1]
. So it came up to me, It will be easier just to check if all A[i] - i
equals to -1, 0 or 1.
bool isIdealPermutation(vector<int>& A) {
for (int i = ; i < A.size(); ++i) if (abs(A[i] - i) > ) return false;
return true;
}
参考:
[leetcode-775-Global and Local Inversions]的更多相关文章
- 【LeetCode】775. Global and Local Inversions 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/global-a ...
- 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) ...
- 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】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] 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) ...
- [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) ...
- oracle11.2中分区功能测试之add&split partition对global&local index的影响
生产库中某些大表的分区异常,需要对现有表进行在线操作,以添加丢失分区,因为是生产库,还是谨慎点好,今天有空,针对add&split分区对global&local索引的影响进行了测试,测 ...
- 侧脸生成正脸概论与精析(一)Global and Local Perception GAN
侧脸生成正脸我一直很感兴趣,老早就想把这块理一理的.今天来给大家分享一篇去年的老文章,如果有不对的地方,请斧正. Beyond Face Rotation: Global and Local Perc ...
- 论文笔记:Improving Deep Visual Representation for Person Re-identification by Global and Local Image-language Association
Improving Deep Visual Representation for Person Re-identification by Global and Local Image-language ...
随机推荐
- 工具 | Axure基础操作 No.3
下午了,再来补一些学习,今天东西不多哦,感觉慢慢上手了. 1.设置元件禁用状态 2.设置单选按钮唯一选中 注意这里在浏览器中就只能唯一选中了. 3.设置图片上的文字 4.图片的切割和裁剪 5.嵌入多媒 ...
- Swift_错误处理
Swift_错误处理 点击查看源码 //错误处理 func test() { //错误枚举 需ErrorType协议 enum ErrorEnum: Error { case `default` // ...
- 关于c++随机种子srand( time(NULL) )的设置问题
设置随机种子srand( time(NULL) ) ,在程序中只需要设置一次就好,而且不能被调用多次,直接看列子. a:每次都重新设置随机种子 #include<iostream> #in ...
- Elasticsearch 5.x安装
node1 elasticsearch node2 elasticsearch node3 elasticsearch 前期准备 JDK1.8 修改/etc/security/limits.conf ...
- PHP的抽象类和抽象方法以及接口总结
PHP中的抽象类和抽象方法自己用的不多,但是经常会在项目中看到别人使用,同样,今天在看别人的代码的时候,发现使用了抽象类,就总结下: 抽象类:1.如果一个类中有一个方法是抽象方法,则这个类就是抽象类: ...
- PredicateBuilder
using System; using System.Linq; using System.Linq.Expressions; namespace Oyang.Tool { public static ...
- Windows常用命令,想要看什么命令直接在全文“CTRL+F”检索(转)
原文地址:https://www.cnblogs.com/kekec/p/3662125.html 打开"运行"对话框(Win+R),输入cmd,打开控制台命令窗口... 也可以通 ...
- 农民工自学java到找到工作的前前后后
我是一名地地道道的农民工,生活在经济落后的农村,有一个哥哥和一个弟弟,父母都是地道的农民,日出而作,日落而息,我从小到大学习一直很好,从小学到高一都,成绩在全级一直名列前茅,这样我也顺利了考上省的重点 ...
- win10安装kali组双系统
一.镜像下载: 根据需求下载自己需要的版本 从官网下载kali 2018.2 的安装包:https://www.kali.org/downloads/ 二.烧录: 这里推荐用 win32 disk i ...
- 部署zabbix,自动发现lnmp环境,监控主机状态,实现 邮件及微信报警(配置server端)
二.配置server端监控 1.监控apache 首先在本机下载模板:https://github.com/rdvn/zabbix-templates/archive/master.zip 该 zi ...