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.

思路:

Key insights:

  • every local inversion is also a global inversion
  • so “local inversions == global inversions” can be interpreted as “there are only local inversions”
  • if there are only local inversions, the array will be sorted after making all local inversions
  • if there are inversions that are not local, the array won’t be sorted after making all local inversions
class Solution {
public boolean isIdealPermutation(int[] A) {
for(int i = 0;i < A.length-1;){
if(A[i]>A[i+1]){
int temp = A[i];
A[i] =A[i+1];
A[i+1] = temp;
i += 2;
}else{
i++;
}
}
for(int i = 0;i < A.length -1; i++){
if(A[i]>A[i+1]){
return false;
}
}
return true;
}
} public class Main {
public static void main(String[] args){
Solution solution = new Solution();
int[] A = {1,2,0};
solution.isIdealPermutation(A);
}
}

  

775. Global and Local Inversions的更多相关文章

  1. 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 ...

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

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

  3. [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) ...

  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 (global) ...

  5. 【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 ...

  6. [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) ...

  7. oracle11.2中分区功能测试之add&amp;split partition对global&amp;local index的影响

    生产库中某些大表的分区异常,需要对现有表进行在线操作,以添加丢失分区,因为是生产库,还是谨慎点好,今天有空,针对add&split分区对global&local索引的影响进行了测试,测 ...

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

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

  9. 论文笔记: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 ...

随机推荐

  1. mitmproxy,mitmdump

    mitmproxy,mitmdump # mitmproxy 官网:https://mitmproxy.org/ # githubhttps://github.com/mitmproxy # wind ...

  2. WPF学习:3.Border & Brush

    上一章<WPF学习:2.Layout-Panels-Countainers>主要介绍了布局,容器和面板.这一章主要开始介绍Border(边界)和Brush(画刷). 代码地址:http:/ ...

  3. Sleep和wait

    sleep()和wait()的区别及wait方法的一点注意事项   一.查看API sleep是Thread类的方法,导致此线程暂停执行指定时间,给其他线程执行机会,但是依然保持着监控状态,过了指定时 ...

  4. linux基础一篇就够了

    Linux学习笔记 粗略笔记第一版,全文约2000行50000字 1. 时间和日历 date:查看当前时间 cal:查看当月日历 cal 2018:查看年日历 cal 10 2018:指定某年某月日历 ...

  5. django请求生命周期,FBV和CBV,ORM拾遗,Git

    一.django 请求生命周期 流程图: 1. 当用户在浏览器中输入url时,浏览器会生成请求头和请求体发给服务端请求头和请求体中会包含浏览器的动作(action),这个动作通常为get或者post, ...

  6. c++ typeid

    简单使用 #include<iostream> #include<string> #include<new> #include<typeinfo> us ...

  7. Hadoop生态组件Hive,Sqoop安装及Sqoop从HDFS/hive抽取数据到关系型数据库Mysql

    一般Hive依赖关系型数据库Mysql,故先安装Mysql $: yum install mysql-server mysql-client [yum安装] $: /etc/init.d/mysqld ...

  8. 【转载】ImportFbx Errors

    [转自http://blog.csdn.net/chenggong2dm/article/details/39580735] 问题: 在导入动作的时候出现一个错误: ImportFBX Errors: ...

  9. python下载大文件

    1. wget def download_big_file_with_wget(url, target_file_name): """ 使用wget下载大文件 Note: ...

  10. fillder---工具栏隐藏/显示

    显示隐藏工具栏方法:view---show toolbar