返回介绍

solution / 2700-2799 / 2784.Check if Array is Good / README_EN

发布于 2024-06-17 01:03:00 字数 5738 浏览 0 评论 0 收藏 0

2784. Check if Array is Good

中文文档

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 - 1exactly once, plus two occurrences of n). For example, base[1] = [1, 1] andbase[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

Solutions

Solution 1: Counting

We can use a hash table or array $cnt$ to record the number of occurrences of each element in the array $nums$. Then we determine whether the following conditions are met:

  1. $cnt[n] = 2$, i.e., the largest element in the array appears twice;
  2. For $1 \leq i \leq n-1$, it holds that $cnt[i] = 1$, i.e., except for the largest element, all other elements appear only once.

If the above two conditions are met, then the array $nums$ is a good array, otherwise it is not.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the array $nums$.

class Solution:
  def isGood(self, nums: List[int]) -> bool:
    cnt = Counter(nums)
    n = len(nums) - 1
    return cnt[n] == 2 and all(cnt[i] for i in range(1, n))
class Solution {
  public boolean isGood(int[] nums) {
    int n = nums.length - 1;
    int[] cnt = new int[201];
    for (int x : nums) {
      ++cnt[x];
    }
    if (cnt[n] != 2) {
      return false;
    }
    for (int i = 1; i < n; ++i) {
      if (cnt[i] != 1) {
        return false;
      }
    }
    return true;
  }
}
class Solution {
public:
  bool isGood(vector<int>& nums) {
    int n = nums.size() - 1;
    int cnt[201]{};
    for (int x : nums) {
      ++cnt[x];
    }
    if (cnt[n] != 2) {
      return false;
    }
    for (int i = 1; i < n; ++i) {
      if (cnt[i] != 1) {
        return false;
      }
    }
    return true;
  }
};
func isGood(nums []int) bool {
  n := len(nums) - 1
  cnt := [201]int{}
  for _, x := range nums {
    cnt[x]++
  }
  if cnt[n] != 2 {
    return false
  }
  for i := 1; i < n; i++ {
    if cnt[i] != 1 {
      return false
    }
  }
  return true
}
function isGood(nums: number[]): boolean {
  const n = nums.length - 1;
  const cnt: number[] = Array(201).fill(0);
  for (const x of nums) {
    ++cnt[x];
  }
  if (cnt[n] !== 2) {
    return false;
  }
  for (let i = 1; i < n; ++i) {
    if (cnt[i] !== 1) {
      return false;
    }
  }
  return true;
}
public class Solution {
  public bool IsGood(int[] nums) {
    int n = nums.Length - 1;
    int[] cnt = new int[201];
    foreach (int x in nums) {
      ++cnt[x];
    }
    if (cnt[n] != 2) {
      return false;
    }
    for (int i = 1; i < n; ++i) {
      if (cnt[i] != 1) {
        return false;
      }
    }
    return true;
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文