Skip to content

feat: add solutions to lc problems: No.2784,2785 #2442

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 54 additions & 26 deletions solution/2700-2799/2784.Check if Array is Good/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,25 @@

## 解法

### 方法一
### 方法一:计数

我们可以用一个哈希表或数组 $cnt$ 记录数组 $nums$ 中每个元素出现的次数。然后我们判断是否满足以下条件:

1. $cnt[n] = 2$,即数组中最大的元素出现了两次;
2. 对于 $1 \leq i \leq n-1$,均满足 $cnt[i] = 1$,即数组中除了最大的元素外,其他元素都只出现了一次。

如果满足以上两个条件,那么数组 $nums$ 是一个好数组,否则不是。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $nums$ 的长度。

<!-- tabs:start -->

```python
class Solution:
def isGood(self, nums: List[int]) -> bool:
n = len(nums) - 1
cnt = Counter(nums)
cnt[n] -= 2
for i in range(1, n):
cnt[i] -= 1
return all(v == 0 for v in cnt.values())
n = len(nums) - 1
return cnt[n] == 2 and all(cnt[i] for i in range(1, n))
```

```java
Expand All @@ -78,12 +84,11 @@ class Solution {
for (int x : nums) {
++cnt[x];
}
cnt[n] -= 2;
for (int i = 1; i < n; ++i) {
cnt[i] -= 1;
if (cnt[n] != 2) {
return false;
}
for (int x : cnt) {
if (x != 0) {
for (int i = 1; i < n; ++i) {
if (cnt[i] != 1) {
return false;
}
}
Expand All @@ -97,16 +102,15 @@ class Solution {
public:
bool isGood(vector<int>& nums) {
int n = nums.size() - 1;
vector<int> cnt(201);
int cnt[201]{};
for (int x : nums) {
++cnt[x];
}
cnt[n] -= 2;
for (int i = 1; i < n; ++i) {
--cnt[i];
if (cnt[n] != 2) {
return false;
}
for (int x : cnt) {
if (x) {
for (int i = 1; i < n; ++i) {
if (cnt[i] != 1) {
return false;
}
}
Expand All @@ -122,12 +126,11 @@ func isGood(nums []int) bool {
for _, x := range nums {
cnt[x]++
}
cnt[n] -= 2
for i := 1; i < n; i++ {
cnt[i]--
if cnt[n] != 2 {
return false
}
for _, x := range cnt {
if x != 0 {
for i := 1; i < n; i++ {
if cnt[i] != 1 {
return false
}
}
Expand All @@ -138,15 +141,40 @@ func isGood(nums []int) bool {
```ts
function isGood(nums: number[]): boolean {
const n = nums.length - 1;
const cnt: number[] = new Array(201).fill(0);
const cnt: number[] = Array(201).fill(0);
for (const x of nums) {
++cnt[x];
}
cnt[n] -= 2;
if (cnt[n] !== 2) {
return false;
}
for (let i = 1; i < n; ++i) {
cnt[i]--;
if (cnt[i] !== 1) {
return false;
}
}
return true;
}
```

```cs
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;
}
return cnt.every(x => x >= 0);
}
```

Expand Down
80 changes: 54 additions & 26 deletions solution/2700-2799/2784.Check if Array is Good/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,25 @@

## Solutions

### Solution 1
### 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$.

<!-- tabs:start -->

```python
class Solution:
def isGood(self, nums: List[int]) -> bool:
n = len(nums) - 1
cnt = Counter(nums)
cnt[n] -= 2
for i in range(1, n):
cnt[i] -= 1
return all(v == 0 for v in cnt.values())
n = len(nums) - 1
return cnt[n] == 2 and all(cnt[i] for i in range(1, n))
```

```java
Expand All @@ -78,12 +84,11 @@ class Solution {
for (int x : nums) {
++cnt[x];
}
cnt[n] -= 2;
for (int i = 1; i < n; ++i) {
cnt[i] -= 1;
if (cnt[n] != 2) {
return false;
}
for (int x : cnt) {
if (x != 0) {
for (int i = 1; i < n; ++i) {
if (cnt[i] != 1) {
return false;
}
}
Expand All @@ -97,16 +102,15 @@ class Solution {
public:
bool isGood(vector<int>& nums) {
int n = nums.size() - 1;
vector<int> cnt(201);
int cnt[201]{};
for (int x : nums) {
++cnt[x];
}
cnt[n] -= 2;
for (int i = 1; i < n; ++i) {
--cnt[i];
if (cnt[n] != 2) {
return false;
}
for (int x : cnt) {
if (x) {
for (int i = 1; i < n; ++i) {
if (cnt[i] != 1) {
return false;
}
}
Expand All @@ -122,12 +126,11 @@ func isGood(nums []int) bool {
for _, x := range nums {
cnt[x]++
}
cnt[n] -= 2
for i := 1; i < n; i++ {
cnt[i]--
if cnt[n] != 2 {
return false
}
for _, x := range cnt {
if x != 0 {
for i := 1; i < n; i++ {
if cnt[i] != 1 {
return false
}
}
Expand All @@ -138,15 +141,40 @@ func isGood(nums []int) bool {
```ts
function isGood(nums: number[]): boolean {
const n = nums.length - 1;
const cnt: number[] = new Array(201).fill(0);
const cnt: number[] = Array(201).fill(0);
for (const x of nums) {
++cnt[x];
}
cnt[n] -= 2;
if (cnt[n] !== 2) {
return false;
}
for (let i = 1; i < n; ++i) {
cnt[i]--;
if (cnt[i] !== 1) {
return false;
}
}
return true;
}
```

```cs
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;
}
return cnt.every(x => x >= 0);
}
```

Expand Down
11 changes: 5 additions & 6 deletions solution/2700-2799/2784.Check if Array is Good/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ class Solution {
public:
bool isGood(vector<int>& nums) {
int n = nums.size() - 1;
vector<int> cnt(201);
int cnt[201]{};
for (int x : nums) {
++cnt[x];
}
cnt[n] -= 2;
for (int i = 1; i < n; ++i) {
--cnt[i];
if (cnt[n] != 2) {
return false;
}
for (int x : cnt) {
if (x) {
for (int i = 1; i < n; ++i) {
if (cnt[i] != 1) {
return false;
}
}
Expand Down
18 changes: 18 additions & 0 deletions solution/2700-2799/2784.Check if Array is Good/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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;
}
}
9 changes: 4 additions & 5 deletions solution/2700-2799/2784.Check if Array is Good/Solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ func isGood(nums []int) bool {
for _, x := range nums {
cnt[x]++
}
cnt[n] -= 2
for i := 1; i < n; i++ {
cnt[i]--
if cnt[n] != 2 {
return false
}
for _, x := range cnt {
if x != 0 {
for i := 1; i < n; i++ {
if cnt[i] != 1 {
return false
}
}
Expand Down
9 changes: 4 additions & 5 deletions solution/2700-2799/2784.Check if Array is Good/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ public boolean isGood(int[] nums) {
for (int x : nums) {
++cnt[x];
}
cnt[n] -= 2;
for (int i = 1; i < n; ++i) {
cnt[i] -= 1;
if (cnt[n] != 2) {
return false;
}
for (int x : cnt) {
if (x != 0) {
for (int i = 1; i < n; ++i) {
if (cnt[i] != 1) {
return false;
}
}
Expand Down
7 changes: 2 additions & 5 deletions solution/2700-2799/2784.Check if Array is Good/Solution.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
class Solution:
def isGood(self, nums: List[int]) -> bool:
n = len(nums) - 1
cnt = Counter(nums)
cnt[n] -= 2
for i in range(1, n):
cnt[i] -= 1
return all(v == 0 for v in cnt.values())
n = len(nums) - 1
return cnt[n] == 2 and all(cnt[i] for i in range(1, n))
12 changes: 8 additions & 4 deletions solution/2700-2799/2784.Check if Array is Good/Solution.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
function isGood(nums: number[]): boolean {
const n = nums.length - 1;
const cnt: number[] = new Array(201).fill(0);
const cnt: number[] = Array(201).fill(0);
for (const x of nums) {
++cnt[x];
}
cnt[n] -= 2;
if (cnt[n] !== 2) {
return false;
}
for (let i = 1; i < n; ++i) {
cnt[i]--;
if (cnt[i] !== 1) {
return false;
}
}
return cnt.every(x => x >= 0);
return true;
}
8 changes: 7 additions & 1 deletion solution/2700-2799/2785.Sort Vowels in a String/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@

## 解法

### 方法一
### 方法一:排序

我们先将字符串中的所有元音字母存到数组或列表 $vs$ 中,然后我们对 $vs$ 进行排序。

接下来,我们遍历字符串 $s$,保持辅音字母不变,如果是元音字母,则依次按顺序替换为 $vs$ 数组中的字母。

时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 是字符串 $s$ 的长度。

<!-- tabs:start -->

Expand Down
Loading