Created
May 14, 2026 16:46
-
-
Save tatsuyax25/bf01c00a138b7403112e54fdbc2356ed to your computer and use it in GitHub Desktop.
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 occurrence
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {number[]} nums | |
| * @return {boolean} | |
| */ | |
| var isGood = function(nums) { | |
| const n = Math.max(...nums); | |
| // Condition 1: length must be n + 1 | |
| if (nums.length !== n + 1) return false; | |
| // Count frequencies | |
| const freq = new Map(); | |
| for (let x of nums) { | |
| freq.set(x, (freq.get(x) || 0) + 1); | |
| } | |
| // Check 1..n-1 appear exactly once | |
| for (let i = 1; i < n; i++) { | |
| if (freq.get(i) !== 1) return false; | |
| } | |
| // Check n appears exactly twice | |
| return freq.get(n) === 2; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment