https://leetcode.com/problems/global-and-local-inversions/description/
https://leetcode.com/problems/global-and-local-inversions/discuss/113656/My-3-lines-C++-Solution
https://leetcode.com/problems/global-and-local-inversions/discuss/113644/Easy-and-Concise-Solution-C++JavaPython
X.
https://leetcode.com/problems/global-and-local-inversions/discuss/113661/Generalize-to-any-integer-array-(not-necessarily-a-0-greaterN-permutation)
https://leetcode.com/problems/global-and-local-inversions/discuss/113652/check-if-we-can-sort-the-array-with-only-local-inversions
X. https://blog.csdn.net/u011026968/article/details/79264504
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.
Basic on this idea, I tried to arrange an ideal permutation. Then I found that to place number
I could only place
i
,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.
The original order should be [0, 1, 2, 3, 4...], the number i should be on the position i. We just check the offset of each number, if the absolute value is larger than 1, means the number of global inversion must be bigger than local inversion, because a local inversion is a global inversion, but a global one may not be local.
bool isIdealPermutation(vector<int>& A) {
for (int i = 0; i < A.size(); ++i) {
if (abs(A[i] - i) > 1) 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; }
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
In other words,
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
Then I check the condition
A
and keep the current biggest number cmax
.Then I check the condition
cmax < A[i+2]
public boolean isIdealPermutation(int[] A) {
int cmax = 0;
for (int i = 0; i < A.size() - 2; ++i) {
cmax = Math.max(cmax, A[i]);
if (cmax > A[i + 2]) return false;
}
return true;
}
https://leetcode.com/problems/global-and-local-inversions/discuss/113661/Generalize-to-any-integer-array-(not-necessarily-a-0-greaterN-permutation)
https://leetcode.com/problems/global-and-local-inversions/discuss/113652/check-if-we-can-sort-the-array-with-only-local-inversions
- 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
public boolean isIdealPermutation(int[] a) {
if (a == null || a.length == 1)
return true;
int i = 0;
while (i + 1 < a.length) {
if (a[i] < a[i + 1]) {
i++;
} else {
swap(a, i, i + 1);
i = i + 2;
}
}
for (i = 0; i < a.length - 1; i++) {
if (a[i] > a[i + 1]) {
return false; // It is proven that the array is not sorted.
}
}
return true;
}
X. https://blog.csdn.net/u011026968/article/details/79264504
第一反应是暴力做法,O(nlogn))逆序数写出来bug了。需要重新写一下。
然后想了下可以O(n),只用修正local的逆序,然后看是不是一个有序数列就行
- int inverse_cnt(vector<int>&nums, int l, int r) {
- if (l >= r) {
- return 0;
- }
- if (l + 1 == r) {
- if(nums[l] > nums[r]) {
- swap(nums[l], nums[r]);
- return 1;
- } else {
- return 0;
- }
- }
- int mid = (l + r) / 2;
- int ret = inverse_cnt(nums, l, mid);
- ret += inverse_cnt(nums, mid+1, r);
- // int bigger = 0;
- int lptr = l, rptr = mid+1;
- vector<int> tmp;
- while (lptr <= mid && rptr <= r) {
- if (nums[lptr] > nums[rptr]) {
- tmp.push_back( nums[rptr] );
- rptr ++;
- ret += (mid - lptr + 1); //(rptr - mid + 1);
- } else {
- tmp.push_back( nums[lptr] );
- lptr ++;
- }
- }
- while (lptr <= mid) {
- tmp.push_back(nums[lptr++]);
- if (nums[lptr] > nums[r]) ret += (mid - lptr + 1);
- }
- while (rptr <= r) tmp.push_back(nums[rptr++]);
- for (int i = l; i <= r; i++) {
- nums[i]= tmp[i- l];
- }
- return ret;
- }
- int local_inverse_cnt(vector<int> &nums) {
- int ret = 0;
- for (int i = 0; i < nums.size()-1; i++) {
- if (nums[i] > nums[i+1]) ret ++;
- }
- return ret;
- }
- bool isIdealPermutation(vector<int>& A) {
- int a1 = local_inverse_cnt( A );
- int a2 = inverse_cnt(A, 0, A.size()-1);
- // return local_inverse_cnt( A ) == inverse_cnt(A, 0, A.size()-1);
- // cout << a1 << "->" << a2 << endl;
- return a1 == a2;
- }