diff --git a/solution/3000-3099/3012.Minimize Length of Array Using Operations/README.md b/solution/3000-3099/3012.Minimize Length of Array Using Operations/README.md index 58886d1713a68..f5b7808cc5f88 100644 --- a/solution/3000-3099/3012.Minimize Length of Array Using Operations/README.md +++ b/solution/3000-3099/3012.Minimize Length of Array Using Operations/README.md @@ -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)$。 ```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& 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; +} ``` diff --git a/solution/3000-3099/3012.Minimize Length of Array Using Operations/README_EN.md b/solution/3000-3099/3012.Minimize Length of Array Using Operations/README_EN.md index 4154d122c0a03..75fe9da756bb7 100644 --- a/solution/3000-3099/3012.Minimize Length of Array Using Operations/README_EN.md +++ b/solution/3000-3099/3012.Minimize Length of Array Using Operations/README_EN.md @@ -70,24 +70,92 @@ It can be shown that 1 is the minimum achievable length. ## 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)$. ```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& 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; +} ``` diff --git a/solution/3000-3099/3012.Minimize Length of Array Using Operations/Solution.cpp b/solution/3000-3099/3012.Minimize Length of Array Using Operations/Solution.cpp new file mode 100644 index 0000000000000..1e3038b226f82 --- /dev/null +++ b/solution/3000-3099/3012.Minimize Length of Array Using Operations/Solution.cpp @@ -0,0 +1,14 @@ +class Solution { +public: + int minimumArrayLength(vector& 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; + } +}; \ No newline at end of file diff --git a/solution/3000-3099/3012.Minimize Length of Array Using Operations/Solution.go b/solution/3000-3099/3012.Minimize Length of Array Using Operations/Solution.go new file mode 100644 index 0000000000000..9eb9bc66398d7 --- /dev/null +++ b/solution/3000-3099/3012.Minimize Length of Array Using Operations/Solution.go @@ -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 +} \ No newline at end of file diff --git a/solution/3000-3099/3012.Minimize Length of Array Using Operations/Solution.java b/solution/3000-3099/3012.Minimize Length of Array Using Operations/Solution.java new file mode 100644 index 0000000000000..ddb8408263b09 --- /dev/null +++ b/solution/3000-3099/3012.Minimize Length of Array Using Operations/Solution.java @@ -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; + } +} \ No newline at end of file diff --git a/solution/3000-3099/3012.Minimize Length of Array Using Operations/Solution.py b/solution/3000-3099/3012.Minimize Length of Array Using Operations/Solution.py new file mode 100644 index 0000000000000..786d784e5d34f --- /dev/null +++ b/solution/3000-3099/3012.Minimize Length of Array Using Operations/Solution.py @@ -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 diff --git a/solution/3000-3099/3012.Minimize Length of Array Using Operations/Solution.ts b/solution/3000-3099/3012.Minimize Length of Array Using Operations/Solution.ts new file mode 100644 index 0000000000000..d2664e401cc2b --- /dev/null +++ b/solution/3000-3099/3012.Minimize Length of Array Using Operations/Solution.ts @@ -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; +}