Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 627156d

Browse files
feat: add solutions to lc problem: No.3012 (doocs#2247)
No.3012.Minimize Length of Array Using Operations
1 parent 8420b81 commit 627156d

File tree

7 files changed

+205
-8
lines changed

7 files changed

+205
-8
lines changed

‎solution/3000-3099/3012.Minimize Length of Array Using Operations/README.md‎

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,24 +74,92 @@ nums 的长度无法进一步减小,所以答案为 1 。
7474

7575
## 解法
7676

77-
### 方法一
77+
### 方法一:分情况讨论
78+
79+
我们不妨记数组 $nums$ 的最小的元素为 $mi$。
80+
81+
如果 $mi$ 只出现一次,那么我们将 $mi$ 与数组 $nums$ 的其他元素进行操作,可以将其他元素全部消去,最终剩下 $mi$ 一个元素,答案为 1ドル$。
82+
83+
如果 $mi$ 出现多次,我们判断数组 $nums$ 中的元素是否都是 $mi$ 的倍数。如果不是,即存在至少一个元素 $x,ドル使得 0ドル \lt x \bmod mi \lt mi,ドル说明我们可以通过操作,构造出一个小于 $mi$ 的元素,那么这个小于 $mi$ 的元素与其他元素进行操作,可以将其他元素全部消去,最终剩下这个小于 $mi$ 的元素,答案为 1ドル$;如果都是 $mi$ 的倍数,我们可以先借助 $mi,ドル将所有大于 $mi$ 的元素消去,最终剩下的元素都是 $mi,ドル个数为 $cnt,ドル两两配对,每两个元素进行一次操作,最终剩下 $\lceil cnt / 2 \rceil$ 个元素,答案为 $\lceil cnt / 2 \rceil$。
84+
85+
时间复杂度 $O(n),ドル其中 $n$ 是数组 $nums$ 的长度。空间复杂度 $O(1)$。
7886

7987
<!-- tabs:start -->
8088

8189
```python
82-
90+
class Solution:
91+
def minimumArrayLength(self, nums: List[int]) -> int:
92+
mi = min(nums)
93+
if any(x % mi for x in nums):
94+
return 1
95+
return (nums.count(mi) + 1) // 2
8396
```
8497

8598
```java
86-
99+
class Solution {
100+
public int minimumArrayLength(int[] nums) {
101+
int mi = Arrays.stream(nums).min().getAsInt();
102+
int cnt = 0;
103+
for (int x : nums) {
104+
if (x % mi != 0) {
105+
return 1;
106+
}
107+
if (x == mi) {
108+
++cnt;
109+
}
110+
}
111+
return (cnt + 1) / 2;
112+
}
113+
}
87114
```
88115

89116
```cpp
90-
117+
class Solution {
118+
public:
119+
int minimumArrayLength(vector<int>& nums) {
120+
int mi = *min_element(nums.begin(), nums.end());
121+
int cnt = 0;
122+
for (int x : nums) {
123+
if (x % mi) {
124+
return 1;
125+
}
126+
cnt += x == mi;
127+
}
128+
return (cnt + 1) / 2;
129+
}
130+
};
91131
```
92132
93133
```go
134+
func minimumArrayLength(nums []int) int {
135+
mi := slices.Min(nums)
136+
cnt := 0
137+
for _, x := range nums {
138+
if x%mi != 0 {
139+
return 1
140+
}
141+
if x == mi {
142+
cnt++
143+
}
144+
}
145+
return (cnt + 1) / 2
146+
}
147+
```
94148

149+
```ts
150+
function minimumArrayLength(nums: number[]): number {
151+
const mi = Math.min(...nums);
152+
let cnt = 0;
153+
for (const x of nums) {
154+
if (x % mi) {
155+
return 1;
156+
}
157+
if (x === mi) {
158+
++cnt;
159+
}
160+
}
161+
return (cnt + 1) >> 1;
162+
}
95163
```
96164

97165
<!-- tabs:end -->

‎solution/3000-3099/3012.Minimize Length of Array Using Operations/README_EN.md‎

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,24 +70,92 @@ It can be shown that 1 is the minimum achievable length.</pre>
7070

7171
## Solutions
7272

73-
### Solution 1
73+
### Solution 1: Case Discussion
74+
75+
Let's denote the smallest element in the array $nums$ as $mi$.
76+
77+
If $mi$ appears only once, we can perform operations with $mi$ and the other elements in the array $nums$ to eliminate all other elements, leaving only $mi$. The answer is 1ドル$.
78+
79+
If $mi$ appears multiple times, we need to check whether all elements in the array $nums$ are multiples of $mi$. If not, there exists at least one element $x$ such that 0ドル < x \bmod mi < mi$. This means we can construct an element smaller than $mi$ through operations. This smaller element can eliminate all other elements through operations, leaving only this smaller element. The answer is 1ドル$. If all elements are multiples of $mi,ドル we can first use $mi$ to eliminate all elements larger than $mi$. The remaining elements are all $mi,ドル with a count of $cnt$. Pair them up, and perform an operation for each pair. Finally, there will be $\lceil cnt / 2 \rceil$ elements left, so the answer is $\lceil cnt / 2 \rceil$.
80+
81+
The time complexity is $O(n),ドル where $n$ is the length of the array $nums$. The space complexity is $O(1)$.
7482

7583
<!-- tabs:start -->
7684

7785
```python
78-
86+
class Solution:
87+
def minimumArrayLength(self, nums: List[int]) -> int:
88+
mi = min(nums)
89+
if any(x % mi for x in nums):
90+
return 1
91+
return (nums.count(mi) + 1) // 2
7992
```
8093

8194
```java
82-
95+
class Solution {
96+
public int minimumArrayLength(int[] nums) {
97+
int mi = Arrays.stream(nums).min().getAsInt();
98+
int cnt = 0;
99+
for (int x : nums) {
100+
if (x % mi != 0) {
101+
return 1;
102+
}
103+
if (x == mi) {
104+
++cnt;
105+
}
106+
}
107+
return (cnt + 1) / 2;
108+
}
109+
}
83110
```
84111

85112
```cpp
86-
113+
class Solution {
114+
public:
115+
int minimumArrayLength(vector<int>& nums) {
116+
int mi = *min_element(nums.begin(), nums.end());
117+
int cnt = 0;
118+
for (int x : nums) {
119+
if (x % mi) {
120+
return 1;
121+
}
122+
cnt += x == mi;
123+
}
124+
return (cnt + 1) / 2;
125+
}
126+
};
87127
```
88128
89129
```go
130+
func minimumArrayLength(nums []int) int {
131+
mi := slices.Min(nums)
132+
cnt := 0
133+
for _, x := range nums {
134+
if x%mi != 0 {
135+
return 1
136+
}
137+
if x == mi {
138+
cnt++
139+
}
140+
}
141+
return (cnt + 1) / 2
142+
}
143+
```
90144

145+
```ts
146+
function minimumArrayLength(nums: number[]): number {
147+
const mi = Math.min(...nums);
148+
let cnt = 0;
149+
for (const x of nums) {
150+
if (x % mi) {
151+
return 1;
152+
}
153+
if (x === mi) {
154+
++cnt;
155+
}
156+
}
157+
return (cnt + 1) >> 1;
158+
}
91159
```
92160

93161
<!-- tabs:end -->
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
int minimumArrayLength(vector<int>& nums) {
4+
int mi = *min_element(nums.begin(), nums.end());
5+
int cnt = 0;
6+
for (int x : nums) {
7+
if (x % mi) {
8+
return 1;
9+
}
10+
cnt += x == mi;
11+
}
12+
return (cnt + 1) / 2;
13+
}
14+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
func minimumArrayLength(nums []int) int {
2+
mi := slices.Min(nums)
3+
cnt := 0
4+
for _, x := range nums {
5+
if x%mi != 0 {
6+
return 1
7+
}
8+
if x == mi {
9+
cnt++
10+
}
11+
}
12+
return (cnt + 1) / 2
13+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int minimumArrayLength(int[] nums) {
3+
int mi = Arrays.stream(nums).min().getAsInt();
4+
int cnt = 0;
5+
for (int x : nums) {
6+
if (x % mi != 0) {
7+
return 1;
8+
}
9+
if (x == mi) {
10+
++cnt;
11+
}
12+
}
13+
return (cnt + 1) / 2;
14+
}
15+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution:
2+
def minimumArrayLength(self, nums: List[int]) -> int:
3+
mi = min(nums)
4+
if any(x % mi for x in nums):
5+
return 1
6+
return (nums.count(mi) + 1) // 2
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function minimumArrayLength(nums: number[]): number {
2+
const mi = Math.min(...nums);
3+
let cnt = 0;
4+
for (const x of nums) {
5+
if (x % mi) {
6+
return 1;
7+
}
8+
if (x === mi) {
9+
++cnt;
10+
}
11+
}
12+
return (cnt + 1) >> 1;
13+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /