Skip to content

Commit 1ec4834

Browse files
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 26.3 MB (97.94%)
1 parent c835acc commit 1ec4834

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<p>You are given an integer array <code>nums</code>. We consider an array <strong>good </strong>if it is a permutation of an array <code>base[n]</code>.</p>
2+
3+
<p><code>base[n] = [1, 2, ..., n - 1, n, n] </code>(in other words, it is an array of length <code>n + 1</code> which contains <code>1</code> to <code>n - 1 </code>exactly once, plus two occurrences of <code>n</code>). For example, <code>base[1] = [1, 1]</code> and<code> base[3] = [1, 2, 3, 3]</code>.</p>
4+
5+
<p>Return <code>true</code> <em>if the given array is good, otherwise return</em><em> </em><code>false</code>.</p>
6+
7+
<p><strong>Note: </strong>A permutation of integers represents an arrangement of these numbers.</p>
8+
9+
<p>&nbsp;</p>
10+
<p><strong class="example">Example 1:</strong></p>
11+
12+
<pre>
13+
<strong>Input:</strong> nums = [2, 1, 3]
14+
<strong>Output:</strong> false
15+
<strong>Explanation:</strong> 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.
16+
</pre>
17+
18+
<p><strong class="example">Example 2:</strong></p>
19+
20+
<pre>
21+
<strong>Input:</strong> nums = [1, 3, 3, 2]
22+
<strong>Output:</strong> true
23+
<strong>Explanation:</strong> 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.</pre>
24+
25+
<p><strong class="example">Example 3:</strong></p>
26+
27+
<pre>
28+
<strong>Input:</strong> nums = [1, 1]
29+
<strong>Output:</strong> true
30+
<strong>Explanation:</strong> 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.</pre>
31+
32+
<p><strong class="example">Example 4:</strong></p>
33+
34+
<pre>
35+
<strong>Input:</strong> nums = [3, 4, 4, 1, 2, 1]
36+
<strong>Output:</strong> false
37+
<strong>Explanation:</strong> 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.
38+
</pre>
39+
40+
<p>&nbsp;</p>
41+
<p><strong>Constraints:</strong></p>
42+
43+
<ul>
44+
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
45+
<li><code>1 &lt;= num[i] &lt;= 200</code></li>
46+
</ul>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
bool isGood(vector<int>& nums) {
4+
sort(nums.begin(), nums.end());
5+
int n = nums.back();
6+
for (int i=0; i<n; i++) {
7+
if (nums[i] != i+1) return false;
8+
}
9+
int m = nums.size();
10+
if (m - n != 1) return false;
11+
for (int i=n; i<m; i++) {
12+
if (nums[i] != n) return false;
13+
}
14+
return true;
15+
}
16+
};

0 commit comments

Comments
 (0)