Skip to content

Latest commit

 

History

History
85 lines (79 loc) · 4.03 KB

README.md

File metadata and controls

85 lines (79 loc) · 4.03 KB

You are given an integer array nums. We consider an array good if it is a permutation of an array base[n].

base[n] = [1, 2, ..., n - 1, n, n] (in other words, it is an array of length n + 1 which contains 1 to n - 1 exactly once, plus two occurrences of n). For example, base[1] = [1, 1] and base[3] = [1, 2, 3, 3].

Return true if the given array is good, otherwise return false.

Note: A permutation of integers represents an arrangement of these numbers.

 

Example 1:

Input: nums = [2, 1, 3]
Output: false
Explanation: Since the maximum element of the array is 3, the only candidate n for which this array could be a permutation of base[n], is n = 3. However, base[3] has four elements but array nums has three. Therefore, it can not be a permutation of base[3] = [1, 2, 3, 3]. So the answer is false.

Example 2:

Input: nums = [1, 3, 3, 2]
Output: true
Explanation: Since the maximum element of the array is 3, the only candidate n for which this array could be a permutation of base[n], is n = 3. It can be seen that nums is a permutation of base[3] = [1, 2, 3, 3] (by swapping the second and fourth elements in nums, we reach base[3]). Therefore, the answer is true.

Example 3:

Input: nums = [1, 1]
Output: true
Explanation: Since the maximum element of the array is 1, the only candidate n for which this array could be a permutation of base[n], is n = 1. It can be seen that nums is a permutation of base[1] = [1, 1]. Therefore, the answer is true.

Example 4:

Input: nums = [3, 4, 4, 1, 2, 1]
Output: false
Explanation: Since the maximum element of the array is 4, the only candidate n for which this array could be a permutation of base[n], is n = 4. However, base[4] has five elements but array nums has six. Therefore, it can not be a permutation of base[4] = [1, 2, 3, 4, 4]. So the answer is false.

 

Constraints:

  • 1 <= nums.length <= 100
  • 1 <= num[i] <= 200

Related Topics:
Array, Hash Table, Sorting

Solution 1.

// OJ: https://leetcode.com/problems/check-if-array-is-good
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(1)
class Solution {
public:
    bool isGood(vector<int>& A) {
        int N = A.size() - 1;
        for (int i = 0; i < N; ) {
            if (A[i] != i + 1) {
                if (A[i] < i + 1 || A[i] > N || (A[i] != N && A[A[i] - 1] == A[i])) return false;
                if (A[i] == N && A[A[i] - 1] == N) {
                    if (A[N] == N) return false;
                    swap(A[i], A[N]);
                }
                else swap(A[i], A[A[i] - 1]);
            } else ++i;
        }
        return A[N] == N;
    }
};

Solution 2. Cycle Sort

// OJ: https://leetcode.com/problems/check-if-array-is-good
// Author: github.com/lzl124631x
// Time: O()
// Space: O()
class Solution {
public:
    bool isGood(vector<int>& A) {
        int N = A.size();
        for (int i = 0; i < N;) {
            if (A[i] >= N) return false;
            int pos = A[i] - 1;
            if (A[pos] == A[i]) ++i;
            else swap(A[pos], A[i]);
        }
        for (int i = 0; i < N - 1; ++i) {
            if (A[i] != i + 1) return false;
        }
        return A[N - 1] == N - 1;
    }
};