2784. Check if Array is Good Easy
1/**
2 * [2784] Check if Array is Good
3 *
4 * You are given an integer array nums. We consider an array good if it is a permutation of an array base[n].
5 * 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].
6 * Return true if the given array is good, otherwise return false.
7 * Note: A permutation of integers represents an arrangement of these numbers.
8 *
9 * <strong class="example">Example 1:
10 *
11 * Input: nums = [2, 1, 3]
12 * Output: false
13 * 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.
14 *
15 * <strong class="example">Example 2:
16 *
17 * Input: nums = [1, 3, 3, 2]
18 * Output: true
19 * 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.
20 * <strong class="example">Example 3:
21 *
22 * Input: nums = [1, 1]
23 * Output: true
24 * 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.
25 * <strong class="example">Example 4:
26 *
27 * Input: nums = [3, 4, 4, 1, 2, 1]
28 * Output: false
29 * 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.
30 *
31 *
32 * Constraints:
33 *
34 * 1 <= nums.length <= 100
35 * 1 <= num[i] <= 200
36 *
37 */
38pub struct Solution {}
39
40// problem: https://leetcode.com/problems/check-if-array-is-good/
41// discuss: https://leetcode.com/problems/check-if-array-is-good/discuss/?currentPage=1&orderBy=most_votes&query=
42
43// submission codes start here
44
45impl Solution {
46 pub fn is_good(nums: Vec<i32>) -> bool {
47 false
48 }
49}
50
51// submission codes end
52
53#[cfg(test)]
54mod tests {
55 use super::*;
56
57 #[test]
58 fn test_2784() {
59 }
60}
61
Back
© 2025 bowen.ge All Rights Reserved.