Check if Array is Good (LeetCode 2784): Solution and Explanation
Decide whether an array is a reordering of base[n] = [1, 2, ..., n-1, n, n], the array that holds 1 to n-1 once each and n twice.
Intuition
There is no search over n. base[n] has n + 1 elements, so if nums is good at all, it must match base[len(nums) - 1].
Think of base[n] as a row of seats: one seat for each value from 1 to n - 1 and two seats for n. That is n + 1 seats, the same as the number of elements.
Walk through nums and sit each value down. A value larger than n has no seat, and a value that finds its seats already full has nowhere to go. Either one proves the array is not good.
If every element finds a free seat, then n + 1 numbers have filled n + 1 seats, so every seat is full and the multiset matches base[n] exactly.
Approach: Counting with fixed limits
- Set
n = len(nums) - 1and make a count array of sizen + 1. - For each
xinnums, return false ifx > n. - Increment
count[x]. Return false if it is now above its limit: 2 whenx == n, 1 otherwise. - Return true after the loop.
Step-by-step example
nums = [2,1,3]
- There are 3 elements, so n = 2 and the target is base[2] = [1, 2, 2].
- x = 2 equals n, which has two seats. count[2] = 1, fine.
- x = 1 takes the single seat for 1. count[1] = 1, fine.
- x = 3 is larger than n = 2, so there is no seat for it. Return false.
Code: Python, Java, C++
1class Solution:
2 def isGood(self, nums: List[int]) -> bool:
3 n = len(nums) - 1
4 count = [0] * (n + 1)
5 for x in nums:
6 if x > n:
7 return False
8 count[x] += 1
9 if count[x] > (2 if x == n else 1):
10 return False
11 return True1class Solution {
2 public boolean isGood(int[] nums) {
3 int n = nums.length - 1;
4 int[] count = new int[n + 1];
5 for (int x : nums) {
6 if (x > n) {
7 return false;
8 }
9 count[x]++;
10 if (count[x] > (x == n ? 2 : 1)) {
11 return false;
12 }
13 }
14 return true;
15 }
16}1class Solution {
2public:
3 bool isGood(vector<int>& nums) {
4 int n = nums.size() - 1;
5 vector<int> count(n + 1, 0);
6 for (int x : nums) {
7 if (x > n) {
8 return false;
9 }
10 count[x]++;
11 if (count[x] > (x == n ? 2 : 1)) {
12 return false;
13 }
14 }
15 return true;
16 }
17};Time and space complexity
O(n)
Each element is checked and counted once.
O(n)
The count array has n + 1 slots.
Other approaches
Sort and compare (time O(n log n), space O(1))
Sort nums and compare it element by element with [1, 2, ..., n, n]. Short to write, but it pays for a sort.
Counter equality (time O(n), space O(n))
Build a Counter of nums and compare it with a Counter of base[n]. Same linear time, but it builds a second table.
Common mistakes
- Taking n as the maximum of nums instead of the length minus one. With [2,1,3] the maximum is 3, but base[3] needs four elements.
- Indexing
count[x]before checkingx > n, which reads outside the array for values up to 200. - Giving every value a limit of one, which rejects the required second copy of n.
- Forgetting that a single-element array can never be good: base[0] does not exist because values start at 1.
Frequently asked questions
Why does the length decide n?
base[n] always has n + 1 elements, and a permutation keeps the length, so n must be len(nums) - 1.
Why is it enough to check for too many copies?
There are exactly n + 1 seats and n + 1 elements. If no seat overflows and no value is out of range, the elements fill every seat, so nothing can be missing.
Can a one-element array be good?
No. It would have to match base[0], which would hold the value 0, and every element is at least 1.