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.

题目大意:全局反转次数是0 <= i <j <N且A [i]> A [j]的i <j的个数,局部反转的次数是0 <= i <N且A [i]> A [i + 1]的i的个数。当且仅当全局反转的次数等于本地反转的次数时才返回true~

分析:局部反转属于全局反转,所以说这个数组里只允许出现相邻的两个数字是大小反转的,其他的若是间隔的两个数字,都只能是从小到大的顺序

1. 我第一次想到的方法:如果A的大小<=2,直接return true即可;否则,对于第i+2个数字来说,必须保证前i个数字都比第i+2个数字小,也就是说对于前i个数字的最大值都得小于i+2处的数字。所以用maxn标记前i个数字中最大的,让它和A[i+2]比较,如果出现了maxn比A[i+2]大的情况,则说明不可能满足条件,返回false,否则最后返回true~

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

2. 我第二次想到的方法:A[i]必定只能在它本应在的位置或这个位置的左边一个或者右边一个,不然必定会把比它大的两个数字挤到前面或者把比它小的两个数字挤到后面,因为题目中已经说明它所有的数字是0~n-1的一个排列,那么A[i]-i的绝对值必定<=1,否则就不满足条件~

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

LeetCode 775. Global and Local Inversions相关推荐

  1. leetcode 775. Global and Local Inversions | 775. 全局倒置与局部倒置(Java)

    题目 https://leetcode.com/problems/global-and-local-inversions/ 题解 看了评论区答案. 局部倒置一定是全局倒置:局部倒置是连续递减: 所以只 ...

  2. 图像拼接--Construction and Refinement of Panoramic Mosaics with Global and Local Alignment

    Construction and Refinement of Panoramic Mosaics with Global and Local Alignment International Confe ...

  3. 论文笔记之:Let there be Color!: Joint End-to-end Learning of Global and Local Image Priors for Automatic

    前言:不知道你是否被这张实验效果图所震撼?Yes, I do. 那么他是怎么做到的呢?本文提出了一种给灰度图像自动上色的框架,结合了图像的局部和全局先验知识 (both global priors a ...

  4. 人脸生成:Beyond Face Rotation: Global and Local Perception GAN

    Beyond Face Rotation: Global and Local Perception GAN for Photorealistic and Identity Preserving Fro ...

  5. GLAMD: Global and Local Attention Mask Distillation for Object Detectors

    全局-局部 注意力的mask蒸馏 传统KD关注fore,而忽视的backg,关注全局,忽略local.本文GLAMD,提取了全局+局部,将future map 分为 几个 patch,并对global ...

  6. (已发布源码)图像修复——上下文编码器以及加入全局判别器的改进(Context Encoder and Global and Local Discriminator)

    Context Encoder and Global and Local Discriminator Context Encoder ,Global and Local Discriminator C ...

  7. 操作分区表对global和local索引的影响

    by  http://blog.chinaunix.net/uid-23284114-id-3421922.html 分类: Oracle 使用分区表,要非常注意索引.因为删除.move分区都会造成索 ...

  8. MICCAI 2022 | PHTrans: Parallelly Aggregating Global and Local Representations for Medical Image Seg

    MICCAI 2022 | PHTrans: Parallelly Aggregating Global and Local Representations for Medical Image Seg ...

  9. [NAS+Transformer]GLiT: Neural Architecture Search for Global and Local Image Transformer

    GLiT:NAS搜索局部和全局Transformer
 Abstract Section I Introduction Section II Related work Section III Meth ...

最新文章

  1. IE浏览器导出问题。。好要命
  2. rufus中gpt和mrb磁盘_UEFI/BIOS/MBR/GPT启动过程详解与常见系统启动问题
  3. 汉诺塔III HDU - 2064
  4. 字节3-1前端面试官自学Vue的正确姿势
  5. YBTOJ:红与蓝(博弈论)
  6. ffmpeg 分辨率 压缩_用GPU加速FFmpeg中的超分辨率功能
  7. 作者:崔代锐(1985-),男,百度外卖研发中心技术总监。
  8. Nestable 可移动拖拽的树型结构的使用(jQuery)
  9. java 弹幕游戏_JAVA 弹幕小游戏 1.0版本
  10. SharePoint2013的头像显示和读取
  11. dubbo 分布式服务框架 介绍
  12. Netty源码分析(四):EventLoopGroup
  13. 选数洛谷c语言,洛谷OJ-P1036 选数 题解(递归DFS)
  14. Fragstats官方入门教程4 移动窗口分析
  15. 一拳超人最强之男手游辅助脚本 快速升级提高提高战力
  16. 美通企业日报 | 人们对传统教育系统的信心正在动摇;90后渴望健康却管不住嘴迈不开腿...
  17. 程序员成长之路(一)
  18. 1011: 圆柱体表面积 Java
  19. linux服务器,docker部署es6.8.7,开启密码认证
  20. LeetCode344

热门文章

  1. Android学习小Demo(12)TodoList实现ListView的分组实现
  2. android-- apktool反编译工具使用详解
  3. 使用php发送Http请求,抓取网页数据
  4. JAVA-SWING:生成透明JTable的改进2
  5. 给妹子讲python-S01E01好用的列表
  6. 通用线程 -- sed 实例
  7. php开发工具 zend studio 12.5.1 中文版汉化
  8. python:threading多线程模块-使用Queue模块保持线程同步
  9. MyEclipse 修改 默认的 工作空间(转)
  10. T-SQL笔记7:临时表和表变量