LeetCode 775. Global inversion and local inversion (merge sort/binary search/one traversal)

1. Title

A is an array [0, 1, ..., N - 1]of one arrangement, N is the length of the array A.

  • Global inversion refers i,jto meet 0 <= i < j < N 并且 A[i] > A[j],
  • Partial inversion means that i is satisfied 0 <= i < N 并且 A[i] > A[i+1].

When the number of global inversions in array A is equal to the number of局部 inversions, return true.

示例 1:
输入: A = [1,0,2]
输出: true
解释:1 个全局倒置,和 1 个局部倒置。

示例 2:
输入: A = [1,2,0]
输出: false
解释:2 个全局倒置,和 1 个局部倒置。

注意:
A 是 [0, 1, ..., A.length - 1] 的一种排列
A 的长度在 [1, 5000]之间

Source: LeetCode
Link: https://leetcode-cn.com/problems/global-and-local-inversions
Copyright is owned by LeetCode . For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.

2. Problem solving

2.1 Merge sort to find the reverse order

Reference: Sword Finger Offer-Interview Question 51. Reversed pairs in an array (merge sort, find reversed pairs)

class Solution {
    
    
	int local = 0, global = 0;
	vector<int> tmp;
public:
    bool isIdealPermutation(vector<int>& A) {
    
    
    	if(A.size() <= 1) return true;
    	for(int i = 0; i < A.size()-1; ++i)
    		if(A[i] > A[i+1])
    			local++;
    	tmp.resize(A.size());
    	mergesort(A, 0, A.size()-1);
    	return local == global;//逆序度 == 局部逆序度
    }
    void mergesort(vector<int>& A, int l, int r)
    {
    
    
    	if(l >= r) return;
    	int mid = (l+r)/2;
    	mergesort(A, l, mid);
    	mergesort(A, mid+1, r);
    	int i = l, j = mid+1, k = 0;
    	while(i <= mid && j <= r)
    	{
    
    
    		if(A[i] <= A[j])
    			tmp[k++] = A[i++];
    		else//后序写入时,检查前面没有出队的(比我大,我在后面)
    		{
    
    
    			tmp[k++] = A[j++];
    			global += mid-i+1;
    		}
    	}
    	while(i <= mid)
            tmp[k++] = A[i++];
    	while(j <= r)
    		tmp[k++] = A[j++];
    	k = 0; i = l;
    	while(i <= r)
    		A[i++] = tmp[k++];
    }
};

Time complexity O (n log ⁡ n) O(n\log n)O ( nlogn )
192 ms 33.8 MB

2.2 Binary search

  • The number of global inversions must be >= the number of local inversions
  • Check each number A[i] A[i]A [ i ] , before it[0, i − 2] [0, i-2][0,i2 ] (Neighboring does not need to be checked) There is a larger number, which certainly does not satisfy the meaning of the question
class Solution {
    
    
public:
    bool isIdealPermutation(vector<int>& A) {
    
    
    	set<int> s;
    	s.insert(A[0]);
    	for(int i = 2; i < A.size(); ++i)
    	{
    
    
    		if(s.lower_bound(A[i]+1) != s.end())
    		{
    
    	//前面存在比 A[i] 大的数
    			return false;
    		}
    		s.insert(A[i-1]);
    	}
    	return true;
    }
};

Time complexity O (n log ⁡ n) O(n\log n)O ( nlogn )
396 ms 52.1 MB

2.3 One traversal

Optimization: On the basis of the second method, record the previous maximum value

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

Time complexity O (n) O(n)O ( n )
148 ms 32.5 MB


My CSDN blog address https://michael.blog.csdn.net/

Long press or scan the QR code to follow my official account (Michael Amin), come on together, learn and make progress together!
Michael Amin

Guess you like

Origin blog.csdn.net/qq_21201267/article/details/108779463