[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= 0;
   for(int i = 0;i<n-2;i++)
   {
     tmax = max(tmax,A[i]);
     if(tmax > A[i+2]) 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 = 0; i < A.size(); ++i) if (abs(A[i] - i) > 1) return false;
        return true;
 }

参考:

https://leetcode.com/problems/global-and-local-inversions/discuss/113644/Easy-and-Concise-Solution-C++JavaPython

原文地址:https://www.cnblogs.com/hellowooorld/p/8439303.html

时间: 2024-10-09 21:05:35

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

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 <

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

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