Skip to content

Commit 0603c3f

Browse files
authored
feat: add solutions to lc problem: No.3046 (doocs#2375)
No.3046.Split the Array
1 parent 8d7a71a commit 0603c3f

File tree

7 files changed

+149
-8
lines changed

7 files changed

+149
-8
lines changed

solution/3000-3099/3046.Split the Array/README.md

+52-4
Original file line numberDiff line numberDiff line change
@@ -48,24 +48,72 @@
4848

4949
## 解法
5050

51-
### 方法一
51+
### 方法一:计数
52+
53+
根据题意,我们需要将数组分成两部分,每部分的元素都是互不相同的。因此,我们可以统计数组中每个元素的出现次数,如果某个元素出现的次数大于等于 $3$ 次,那么就无法满足题意。否则,我们可以将数组分成两部分。
54+
55+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组的长度。
5256

5357
<!-- tabs:start -->
5458

5559
```python
56-
60+
class Solution:
61+
def isPossibleToSplit(self, nums: List[int]) -> bool:
62+
return max(Counter(nums).values()) < 3
5763
```
5864

5965
```java
60-
66+
class Solution {
67+
public boolean isPossibleToSplit(int[] nums) {
68+
int[] cnt = new int[101];
69+
for (int x : nums) {
70+
if (++cnt[x] >= 3) {
71+
return false;
72+
}
73+
}
74+
return true;
75+
}
76+
}
6177
```
6278

6379
```cpp
64-
80+
class Solution {
81+
public:
82+
bool isPossibleToSplit(vector<int>& nums) {
83+
int cnt[101]{};
84+
for (int x : nums) {
85+
if (++cnt[x] >= 3) {
86+
return false;
87+
}
88+
}
89+
return true;
90+
}
91+
};
6592
```
6693
6794
```go
95+
func isPossibleToSplit(nums []int) bool {
96+
cnt := [101]int{}
97+
for _, x := range nums {
98+
cnt[x]++
99+
if cnt[x] >= 3 {
100+
return false
101+
}
102+
}
103+
return true
104+
}
105+
```
68106

107+
```ts
108+
function isPossibleToSplit(nums: number[]): boolean {
109+
const cnt: number[] = Array(101).fill(0);
110+
for (const x of nums) {
111+
if (++cnt[x] >= 3) {
112+
return false;
113+
}
114+
}
115+
return true;
116+
}
69117
```
70118

71119
<!-- tabs:end -->

solution/3000-3099/3046.Split the Array/README_EN.md

+52-4
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,72 @@
4444

4545
## Solutions
4646

47-
### Solution 1
47+
### Solution 1: Counting
48+
49+
According to the problem, we need to divide the array into two parts, and the elements in each part are all distinct. Therefore, we can count the occurrence of each element in the array. If an element appears three or more times, it cannot satisfy the problem's requirements. Otherwise, we can divide the array into two parts.
50+
51+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the array.
4852

4953
<!-- tabs:start -->
5054

5155
```python
52-
56+
class Solution:
57+
def isPossibleToSplit(self, nums: List[int]) -> bool:
58+
return max(Counter(nums).values()) < 3
5359
```
5460

5561
```java
56-
62+
class Solution {
63+
public boolean isPossibleToSplit(int[] nums) {
64+
int[] cnt = new int[101];
65+
for (int x : nums) {
66+
if (++cnt[x] >= 3) {
67+
return false;
68+
}
69+
}
70+
return true;
71+
}
72+
}
5773
```
5874

5975
```cpp
60-
76+
class Solution {
77+
public:
78+
bool isPossibleToSplit(vector<int>& nums) {
79+
int cnt[101]{};
80+
for (int x : nums) {
81+
if (++cnt[x] >= 3) {
82+
return false;
83+
}
84+
}
85+
return true;
86+
}
87+
};
6188
```
6289
6390
```go
91+
func isPossibleToSplit(nums []int) bool {
92+
cnt := [101]int{}
93+
for _, x := range nums {
94+
cnt[x]++
95+
if cnt[x] >= 3 {
96+
return false
97+
}
98+
}
99+
return true
100+
}
101+
```
64102

103+
```ts
104+
function isPossibleToSplit(nums: number[]): boolean {
105+
const cnt: number[] = Array(101).fill(0);
106+
for (const x of nums) {
107+
if (++cnt[x] >= 3) {
108+
return false;
109+
}
110+
}
111+
return true;
112+
}
65113
```
66114

67115
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
bool isPossibleToSplit(vector<int>& nums) {
4+
int cnt[101]{};
5+
for (int x : nums) {
6+
if (++cnt[x] >= 3) {
7+
return false;
8+
}
9+
}
10+
return true;
11+
}
12+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
func isPossibleToSplit(nums []int) bool {
2+
cnt := [101]int{}
3+
for _, x := range nums {
4+
cnt[x]++
5+
if cnt[x] >= 3 {
6+
return false
7+
}
8+
}
9+
return true
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public boolean isPossibleToSplit(int[] nums) {
3+
int[] cnt = new int[101];
4+
for (int x : nums) {
5+
if (++cnt[x] >= 3) {
6+
return false;
7+
}
8+
}
9+
return true;
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def isPossibleToSplit(self, nums: List[int]) -> bool:
3+
return max(Counter(nums).values()) < 3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function isPossibleToSplit(nums: number[]): boolean {
2+
const cnt: number[] = Array(101).fill(0);
3+
for (const x of nums) {
4+
if (++cnt[x] >= 3) {
5+
return false;
6+
}
7+
}
8+
return true;
9+
}

0 commit comments

Comments
 (0)