File size: 3,024 Bytes
4097b71 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | {
"id": 2892,
"name": "check-if-array-is-good",
"difficulty": "Easy",
"link": "https://leetcode.com/problems/check-if-array-is-good/",
"date": "2023-07-08",
"task_description": "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`",
"test_case": [
{
"label": "Example 1",
"input": "nums = [2, 1, 3]",
"output": "false "
},
{
"label": "Example 2",
"input": "nums = [1, 3, 3, 2]",
"output": "true "
},
{
"label": "Example 3",
"input": "nums = [1, 1]",
"output": "true "
},
{
"label": "Example 4",
"input": "nums = [3, 4, 4, 1, 2, 1]",
"output": "false "
}
],
"constraints": [
"1 <= nums.length <= 100",
"1 <= num[i] <= 200"
],
"python_template": "class Solution(object):\n def isGood(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: bool\n \"\"\"\n ",
"java_template": "class Solution {\n public boolean isGood(int[] nums) {\n \n }\n}",
"metadata": {
"func_name": "isGood"
}
} |