This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public boolean isIdealPermutation(int[] A) { | |
if (A == null || A.length < 3) { | |
return true; | |
} | |
int len = A.length; | |
for (int i = 0; i < len - 1; i++) { | |
if (A[i] == i) { | |
continue; | |
} else if (A[i] == i+1 && A[i + 1]== i) { | |
i++; | |
} else { | |
return false; | |
} | |
} | |
return true; | |
// int globalInversions = 0; | |
// for(int i = 0; i < len - 1; i++) { | |
// for(int j = i + 1; j < len; j++) { | |
// if (A[i] > A[j]) {globalInversions++;} | |
// } | |
// } | |
// int localInversions = 0; | |
// for(int i = 0; i < len - 1; i++) { | |
// if (A[i] > A[i+1]) {localInversions++;} | |
// } | |
// return globalInversions == localInversions; | |
} | |
} |
No comments:
Post a Comment