Codility PermCheck任务:方案性能满分、正确性80%,双元素测试用例失败求修复
Hey there! Let's figure out why your PermCheck solution is hitting a snag on double-element test cases—even though your local tests seem fine, Codility's edge cases are probably catching something you missed.
First, let's recap the core requirements of the PermCheck task: we need to verify if the input array is a permutation of integers from 1 to N, where N is the length of the array. That means three non-negotiable conditions must all be true:
- Every element is between 1 and N (inclusive).
- No element is repeated.
- All integers from 1 to N are present exactly once.
Common Double-Element Test Cases That Trip Up Solutions
Codility likely runs these edge cases that you might not have tested (or might have overlooked):
[1,1]: Should return 0 (duplicate element, missing 2)[2,3]: Should return 0 (elements exceed N=2)[0,1]: Should return 0 (element 0 is out of the 1~2 range)[1,3]: Should return 0 (missing 2, element 3 exceeds N=2)[2,1]: Should return 1 (valid permutation)
Why Your Code Might Be Failing
Here are the most common mistakes that cause correctness issues on these cases:
- Only checking the sum of elements: You might have used the formula
N*(N+1)/2to verify the total sum, but this doesn't account for duplicates or out-of-range elements. For example,[3,0]sums to 3 (which equals 2*3/2) but is not a valid permutation. - Forgetting to validate element ranges: If your code only checks for presence of 1~N but doesn't flag elements outside this range, cases like
[2,3]will slip through as valid (which they're not). - Incomplete set checks: If you're using a set to track elements, you might have made a mistake in verifying coverage. For example, using
range(1, N)instead ofrange(1, N+1)would miss checking for the number N itself—so[1,3]with N=2 would incorrectly pass. - Ignoring duplicates: If your code doesn't check for repeated values, cases like
[1,1]would be incorrectly marked as valid.
Fixes to Implement
Let's outline a bulletproof approach that covers all edge cases:
- Track seen elements: Use a boolean array (or a set) to mark which numbers from 1~N have been encountered.
- Validate element ranges on the fly: For every element, if it's less than 1 or greater than N, immediately return 0.
- Check for duplicates: If an element is already marked as seen, return 0.
- Ensure full coverage: After processing all elements, confirm every number from 1 to N was seen (though the above checks might make this redundant if implemented correctly).
Here's an example Python solution that hits 100% correctness and performance:
def solution(A): n = len(A) seen = [False] * (n + 1) # Indexes 0 to n; we'll use 1 to n for num in A: # Check if number is out of valid range if num < 1 or num > n: return 0 # Check for duplicate if seen[num]: return 0 seen[num] = True # If we made it here, all conditions are satisfied return 1
Next Steps for You
- Run the double-element test cases listed above through your current code.
- Identify which case returns an incorrect result—this will point directly to the missing condition in your code.
- Add the necessary check (range validation, duplicate detection, or full coverage verification) to fix the gap.
内容的提问来源于stack exchange,提问作者Muhammad Rahil Rafiq




