1037. Global and Local Inversions

Code(Language:C++)
class Solution {
public:
    /**
     * @param A: an array
     * @return: is the number of global inversions is equal to the number of local inversions
     */
    bool isIdealPermutation(vector<int> &A) {
        // Write your code here
        int maxV = A[0];
        for(int i = 2; i < A.size(); i++){
            if(A[i] < maxV){
                return false; 
            }
            maxV = max(maxV, A[i - 2]); 
        }
        return true; 
    }
};

Comments

Popular posts from this blog

1427. Split Array into Fibonacci Sequence

Amazon OA 763. Partition Labels

05/25 周一