Skip to content

Commit 8e60613

Browse files
committed
feat: add solutions to lc problem: No.2344
No.2344.Minimum Deletions to Make Array Divisible
1 parent 383a3a6 commit 8e60613

File tree

6 files changed

+306
-56
lines changed

6 files changed

+306
-56
lines changed

solution/2300-2399/2344.Minimum Deletions to Make Array Divisible/README.md

Lines changed: 129 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@
4646

4747
<!-- 这里可写通用的实现逻辑 -->
4848

49+
**方法一:数学 + 排序**
50+
51+
如果一个元素能整除数组 `numsDivide` 所有元素,那么这个元素是所有 $numsDivide[i]$ 的最大公约数 $x$ 的因子。因此,我们可以先求出 `numsDivide` 的最大公约数 $x$。
52+
53+
接下来,将数组 `nums` 排序,然后从头到尾遍历数组 `nums`,找到第一个是最大公约数 $x$ 的因子的元素,返回当前元素下标即可。
54+
55+
时间复杂度 $O(m + \log M + n \times \log n)$,其中 $n$ 和 $m$ 分别是数组 `nums``numsDivide` 的长度,而 $M$ 是数组 `numsDivide` 中的最大值。
56+
57+
实际上,我们也可以不用排序数组 `nums`,而是直接遍历数组 `nums`,找到最小的能整除 $x$ 的元素,然后我们再遍历一次数组 `nums`,统计小于等于这个元素的元素个数即可。
58+
59+
时间复杂度 $O(m + \log M + n)$。
60+
4961
<!-- tabs:start -->
5062

5163
### **Python3**
@@ -68,21 +80,29 @@ class Solution:
6880
```python
6981
class Solution:
7082
def minOperations(self, nums: List[int], numsDivide: List[int]) -> int:
71-
x = reduce(gcd, numsDivide)
83+
x = gcd(*numsDivide)
7284
nums.sort()
7385
return next((i for i, v in enumerate(nums) if x % v == 0), -1)
7486
```
7587

88+
```python
89+
class Solution:
90+
def minOperations(self, nums: List[int], numsDivide: List[int]) -> int:
91+
x = gcd(*numsDivide)
92+
y = min((v for v in nums if x % v == 0), default=0)
93+
return sum(v < y for v in nums) if y else -1
94+
```
95+
7696
### **Java**
7797

7898
<!-- 这里可写当前语言的特殊实现逻辑 -->
7999

80100
```java
81101
class Solution {
82102
public int minOperations(int[] nums, int[] numsDivide) {
83-
int x = numsDivide[0];
84-
for (int i = 1; i < numsDivide.length; ++i) {
85-
x = gcd(x, numsDivide[i]);
103+
int x = 0;
104+
for (int v : numsDivide) {
105+
x = gcd(x, v);
86106
}
87107
Arrays.sort(nums);
88108
for (int i = 0; i < nums.length; ++i) {
@@ -99,22 +119,80 @@ class Solution {
99119
}
100120
```
101121

122+
```java
123+
class Solution {
124+
public int minOperations(int[] nums, int[] numsDivide) {
125+
int x = 0;
126+
for (int v : numsDivide) {
127+
x = gcd(x, v);
128+
}
129+
int y = 1 << 30;
130+
for (int v : nums) {
131+
if (x % v == 0) {
132+
y = Math.min(y, v);
133+
}
134+
}
135+
if (y == 1 << 30) {
136+
return -1;
137+
}
138+
int ans = 0;
139+
for (int v : nums) {
140+
if (v < y) {
141+
++ans;
142+
}
143+
}
144+
return ans;
145+
}
146+
147+
private int gcd(int a, int b) {
148+
return b == 0 ? a : gcd(b, a % b);
149+
}
150+
}
151+
```
152+
102153
### **C++**
103154

104155
```cpp
105156
class Solution {
106157
public:
107158
int minOperations(vector<int>& nums, vector<int>& numsDivide) {
108-
int x = numsDivide[0];
109-
for (int i = 1; i < numsDivide.size(); ++i) x = gcd(x, numsDivide[i]);
159+
int x = 0;
160+
for (int& v : numsDivide) {
161+
x = gcd(x, v);
162+
}
110163
sort(nums.begin(), nums.end());
111-
for (int i = 0; i < nums.size(); ++i)
112-
if (x % nums[i] == 0) return i;
164+
for (int i = 0; i < nums.size(); ++i) {
165+
if (x % nums[i] == 0) {
166+
return i;
167+
}
168+
}
113169
return -1;
114170
}
171+
};
172+
```
115173
116-
int gcd(int a, int b) {
117-
return b == 0 ? a : gcd(b, a % b);
174+
```cpp
175+
class Solution {
176+
public:
177+
int minOperations(vector<int>& nums, vector<int>& numsDivide) {
178+
int x = 0;
179+
for (int& v : numsDivide) {
180+
x = gcd(x, v);
181+
}
182+
int y = 1 << 30;
183+
for (int& v : nums) {
184+
if (x % v == 0) {
185+
y = min(y, v);
186+
}
187+
}
188+
if (y == 1 << 30) {
189+
return -1;
190+
}
191+
int ans = 0;
192+
for (int& v : nums) {
193+
ans += v < y;
194+
}
195+
return ans;
118196
}
119197
};
120198
```
@@ -123,8 +201,8 @@ public:
123201

124202
```go
125203
func minOperations(nums []int, numsDivide []int) int {
126-
x := numsDivide[0]
127-
for _, v := range numsDivide[1:] {
204+
x := 0
205+
for _, v := range numsDivide {
128206
x = gcd(x, v)
129207
}
130208
sort.Ints(nums)
@@ -144,6 +222,45 @@ func gcd(a, b int) int {
144222
}
145223
```
146224

225+
```go
226+
func minOperations(nums []int, numsDivide []int) int {
227+
x := 0
228+
for _, v := range numsDivide {
229+
x = gcd(x, v)
230+
}
231+
y := 1 << 30
232+
for _, v := range nums {
233+
if x%v == 0 {
234+
y = min(y, v)
235+
}
236+
}
237+
if y == 1<<30 {
238+
return -1
239+
}
240+
ans := 0
241+
for _, v := range nums {
242+
if v < y {
243+
ans++
244+
}
245+
}
246+
return ans
247+
}
248+
249+
func min(a, b int) int {
250+
if a < b {
251+
return a
252+
}
253+
return b
254+
}
255+
256+
func gcd(a, b int) int {
257+
if b == 0 {
258+
return a
259+
}
260+
return gcd(b, a%b)
261+
}
262+
```
263+
147264
### **TypeScript**
148265

149266
```ts

solution/2300-2399/2344.Minimum Deletions to Make Array Divisible/README_EN.md

Lines changed: 117 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,27 @@ class Solution:
6262
```python
6363
class Solution:
6464
def minOperations(self, nums: List[int], numsDivide: List[int]) -> int:
65-
x = reduce(gcd, numsDivide)
65+
x = gcd(*numsDivide)
6666
nums.sort()
6767
return next((i for i, v in enumerate(nums) if x % v == 0), -1)
6868
```
6969

70+
```python
71+
class Solution:
72+
def minOperations(self, nums: List[int], numsDivide: List[int]) -> int:
73+
x = gcd(*numsDivide)
74+
y = min((v for v in nums if x % v == 0), default=0)
75+
return sum(v < y for v in nums) if y else -1
76+
```
77+
7078
### **Java**
7179

7280
```java
7381
class Solution {
7482
public int minOperations(int[] nums, int[] numsDivide) {
75-
int x = numsDivide[0];
76-
for (int i = 1; i < numsDivide.length; ++i) {
77-
x = gcd(x, numsDivide[i]);
83+
int x = 0;
84+
for (int v : numsDivide) {
85+
x = gcd(x, v);
7886
}
7987
Arrays.sort(nums);
8088
for (int i = 0; i < nums.length; ++i) {
@@ -91,22 +99,80 @@ class Solution {
9199
}
92100
```
93101

102+
```java
103+
class Solution {
104+
public int minOperations(int[] nums, int[] numsDivide) {
105+
int x = 0;
106+
for (int v : numsDivide) {
107+
x = gcd(x, v);
108+
}
109+
int y = 1 << 30;
110+
for (int v : nums) {
111+
if (x % v == 0) {
112+
y = Math.min(y, v);
113+
}
114+
}
115+
if (y == 1 << 30) {
116+
return -1;
117+
}
118+
int ans = 0;
119+
for (int v : nums) {
120+
if (v < y) {
121+
++ans;
122+
}
123+
}
124+
return ans;
125+
}
126+
127+
private int gcd(int a, int b) {
128+
return b == 0 ? a : gcd(b, a % b);
129+
}
130+
}
131+
```
132+
94133
### **C++**
95134

96135
```cpp
97136
class Solution {
98137
public:
99138
int minOperations(vector<int>& nums, vector<int>& numsDivide) {
100-
int x = numsDivide[0];
101-
for (int i = 1; i < numsDivide.size(); ++i) x = gcd(x, numsDivide[i]);
139+
int x = 0;
140+
for (int& v : numsDivide) {
141+
x = gcd(x, v);
142+
}
102143
sort(nums.begin(), nums.end());
103-
for (int i = 0; i < nums.size(); ++i)
104-
if (x % nums[i] == 0) return i;
144+
for (int i = 0; i < nums.size(); ++i) {
145+
if (x % nums[i] == 0) {
146+
return i;
147+
}
148+
}
105149
return -1;
106150
}
151+
};
152+
```
107153
108-
int gcd(int a, int b) {
109-
return b == 0 ? a : gcd(b, a % b);
154+
```cpp
155+
class Solution {
156+
public:
157+
int minOperations(vector<int>& nums, vector<int>& numsDivide) {
158+
int x = 0;
159+
for (int& v : numsDivide) {
160+
x = gcd(x, v);
161+
}
162+
int y = 1 << 30;
163+
for (int& v : nums) {
164+
if (x % v == 0) {
165+
y = min(y, v);
166+
}
167+
}
168+
if (y == 1 << 30) {
169+
return -1;
170+
}
171+
int ans = 0;
172+
for (int& v : nums) {
173+
ans += v < y;
174+
}
175+
return ans;
110176
}
111177
};
112178
```
@@ -115,8 +181,8 @@ public:
115181

116182
```go
117183
func minOperations(nums []int, numsDivide []int) int {
118-
x := numsDivide[0]
119-
for _, v := range numsDivide[1:] {
184+
x := 0
185+
for _, v := range numsDivide {
120186
x = gcd(x, v)
121187
}
122188
sort.Ints(nums)
@@ -136,6 +202,45 @@ func gcd(a, b int) int {
136202
}
137203
```
138204

205+
```go
206+
func minOperations(nums []int, numsDivide []int) int {
207+
x := 0
208+
for _, v := range numsDivide {
209+
x = gcd(x, v)
210+
}
211+
y := 1 << 30
212+
for _, v := range nums {
213+
if x%v == 0 {
214+
y = min(y, v)
215+
}
216+
}
217+
if y == 1<<30 {
218+
return -1
219+
}
220+
ans := 0
221+
for _, v := range nums {
222+
if v < y {
223+
ans++
224+
}
225+
}
226+
return ans
227+
}
228+
229+
func min(a, b int) int {
230+
if a < b {
231+
return a
232+
}
233+
return b
234+
}
235+
236+
func gcd(a, b int) int {
237+
if b == 0 {
238+
return a
239+
}
240+
return gcd(b, a%b)
241+
}
242+
```
243+
139244
### **TypeScript**
140245

141246
```ts

0 commit comments

Comments
 (0)