Skip to content

feat: add solutions to lc problem: No.3012 #2247

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
Jan 22, 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
Original file line number Diff line number Diff line change
Expand Up @@ -74,24 +74,92 @@ nums 的长度无法进一步减小,所以答案为 1 。

## 解法

### 方法一
### 方法一:分情况讨论

我们不妨记数组 $nums$ 的最小的元素为 $mi$。

如果 $mi$ 只出现一次,那么我们将 $mi$ 与数组 $nums$ 的其他元素进行操作,可以将其他元素全部消去,最终剩下 $mi$ 一个元素,答案为 $1$。

如果 $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$。

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

<!-- tabs:start -->

```python

class Solution:
def minimumArrayLength(self, nums: List[int]) -> int:
mi = min(nums)
if any(x % mi for x in nums):
return 1
return (nums.count(mi) + 1) // 2
```

```java

class Solution {
public int minimumArrayLength(int[] nums) {
int mi = Arrays.stream(nums).min().getAsInt();
int cnt = 0;
for (int x : nums) {
if (x % mi != 0) {
return 1;
}
if (x == mi) {
++cnt;
}
}
return (cnt + 1) / 2;
}
}
```

```cpp

class Solution {
public:
int minimumArrayLength(vector<int>& nums) {
int mi = *min_element(nums.begin(), nums.end());
int cnt = 0;
for (int x : nums) {
if (x % mi) {
return 1;
}
cnt += x == mi;
}
return (cnt + 1) / 2;
}
};
```

```go
func minimumArrayLength(nums []int) int {
mi := slices.Min(nums)
cnt := 0
for _, x := range nums {
if x%mi != 0 {
return 1
}
if x == mi {
cnt++
}
}
return (cnt + 1) / 2
}
```

```ts
function minimumArrayLength(nums: number[]): number {
const mi = Math.min(...nums);
let cnt = 0;
for (const x of nums) {
if (x % mi) {
return 1;
}
if (x === mi) {
++cnt;
}
}
return (cnt + 1) >> 1;
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,92 @@ It can be shown that 1 is the minimum achievable length.</pre>

## Solutions

### Solution 1
### Solution 1: Case Discussion

Let's denote the smallest element in the array $nums$ as $mi$.

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$.

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$.

The time complexity is $O(n)$, where $n$ is the length of the array $nums$. The space complexity is $O(1)$.

<!-- tabs:start -->

```python

class Solution:
def minimumArrayLength(self, nums: List[int]) -> int:
mi = min(nums)
if any(x % mi for x in nums):
return 1
return (nums.count(mi) + 1) // 2
```

```java

class Solution {
public int minimumArrayLength(int[] nums) {
int mi = Arrays.stream(nums).min().getAsInt();
int cnt = 0;
for (int x : nums) {
if (x % mi != 0) {
return 1;
}
if (x == mi) {
++cnt;
}
}
return (cnt + 1) / 2;
}
}
```

```cpp

class Solution {
public:
int minimumArrayLength(vector<int>& nums) {
int mi = *min_element(nums.begin(), nums.end());
int cnt = 0;
for (int x : nums) {
if (x % mi) {
return 1;
}
cnt += x == mi;
}
return (cnt + 1) / 2;
}
};
```

```go
func minimumArrayLength(nums []int) int {
mi := slices.Min(nums)
cnt := 0
for _, x := range nums {
if x%mi != 0 {
return 1
}
if x == mi {
cnt++
}
}
return (cnt + 1) / 2
}
```

```ts
function minimumArrayLength(nums: number[]): number {
const mi = Math.min(...nums);
let cnt = 0;
for (const x of nums) {
if (x % mi) {
return 1;
}
if (x === mi) {
++cnt;
}
}
return (cnt + 1) >> 1;
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
public:
int minimumArrayLength(vector<int>& nums) {
int mi = *min_element(nums.begin(), nums.end());
int cnt = 0;
for (int x : nums) {
if (x % mi) {
return 1;
}
cnt += x == mi;
}
return (cnt + 1) / 2;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
func minimumArrayLength(nums []int) int {
mi := slices.Min(nums)
cnt := 0
for _, x := range nums {
if x%mi != 0 {
return 1
}
if x == mi {
cnt++
}
}
return (cnt + 1) / 2
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public int minimumArrayLength(int[] nums) {
int mi = Arrays.stream(nums).min().getAsInt();
int cnt = 0;
for (int x : nums) {
if (x % mi != 0) {
return 1;
}
if (x == mi) {
++cnt;
}
}
return (cnt + 1) / 2;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Solution:
def minimumArrayLength(self, nums: List[int]) -> int:
mi = min(nums)
if any(x % mi for x in nums):
return 1
return (nums.count(mi) + 1) // 2
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function minimumArrayLength(nums: number[]): number {
const mi = Math.min(...nums);
let cnt = 0;
for (const x of nums) {
if (x % mi) {
return 1;
}
if (x === mi) {
++cnt;
}
}
return (cnt + 1) >> 1;
}