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.

思路:

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);
    }
}

  

原文地址:https://www.cnblogs.com/lxk2010012997/p/8419459.html

时间: 2024-08-02 06:12:31

775. Global and Local Inversions的相关文章

leetcode 775. Global and Local Inversions ---找规律

题解: 在global和local出现的情况相等时候,会发现,将local中出现逆序的情况反转,就能够得到一个升序排列的数组, 否则,如果swap两个逆序之后,不是升序的,会导致global的个数大于local的个数,如[1,2,0]中,2和0 交换后,不是升序 排列,除了2大于0,global的统计中1也大于0,因此glboal要比local的次数要多 bool isIdealPermutation(vector<int>& A) { if(A.size()<=1) retur

[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 <

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

[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 <

unity, Global和Local编辑模式

下图表示是在Local模式下: 下图表示是在Global模式下: 不要搞反.

global,local,static的区别

1.在函数内部使用global关键字定义的变量可以成为全局变量,如果该变量已经被定义了,那么他的值就是原来的值,否则就是一个新的全局变量(一句话:已存在就不再创建): 1 <?php 2 $a=1; 3 function run(){ 4 global $a; 5 $a=10; 6 $a++; 7 } 8 run(); 9 echo "Global variable a=$a \n"; 10 $b=100; 11 function run1(){ 12 $b=10; 13 $b+

PHP中Global和Local范围以及Static变量

1. Local scope function update_counter() { $counter++;//此处$counter为局部变量,与函数外的$counter非同一个 } $counter = 10; update_counter(); echo $counter; //输出:10 2. Global scope function update_counter() { global $counter;//利用global关键字在函数内进行声明即可获取全局域的$counter $cou

Global and Local Coordinate Systems

ansys 中的坐标系 整体和局部坐标系(主要在建模中涉及) 整体坐标系是以你建模的整个建筑为一体,来确定坐标系的.比如你建一个矩形平面的建筑,整体坐标系一般默认水平方向为X轴,竖直方向为Y轴,以垂直图面的方向为Z轴.局部坐标系是以单个构件为概念的设置的.比如说对一个工字形的钢柱,局部坐标系的X轴通常为这个截面的强轴方向,对于工字形柱来说一般就是与腹板垂直的方向:其Y轴为截面的弱轴方向,对于工字形柱来说一般就是与腹板平行的方向:Z轴对于梁柱等杆系构件来说一般是沿杆件的长度方向,对于板构件来说一般

【LeetCode】数组

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [1]Two Sum [4]Median of Two Sorted Arrays [11]Container With Most Water [15]3Sum [16]3Sum Closest [18]4Sum [26]Remove Duplicates from Sorted Array [27]Remove Element [31]Next Permutatio