Skip to content

feat: add solutions to lc problem: No.1887 #4051

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
Feb 10, 2025
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 @@ -57,8 +57,8 @@ tags:
<strong>输出:</strong>4
<strong>解释:</strong>需要 4 次操作使 nums 中的所有元素相等:
1. largest = 3 下标为 4 。nextLargest = 2 。将 nums[4] 减少到 2 。nums = [1,1,2,2,<strong>2</strong>] 。
2. largest = 2 下标为 2 。nextLargest = 1 。将 nums[2] 减少到 1 。nums = [1,1,<strong>1</strong>,2,2] 。
3. largest = 2 下标为 3 。nextLargest = 1 。将 nums[3] 减少到 1 。nums = [1,1,1,<strong>1</strong>,2] 。
2. largest = 2 下标为 2 。nextLargest = 1 。将 nums[2] 减少到 1 。nums = [1,1,<strong>1</strong>,2,2] 。
3. largest = 2 下标为 3 。nextLargest = 1 。将 nums[3] 减少到 1 。nums = [1,1,1,<strong>1</strong>,2] 。
4. largest = 2 下标为 4 。nextLargest = 1 。将 nums[4] 减少到 1 。nums = [1,1,1,1,<strong>1</strong>] 。
</pre>

Expand All @@ -79,11 +79,9 @@ tags:

### 方法一:排序

对 $nums$ 进行排序,用 $cnt$ 表示元素所需的操作次数,初始时 $cnt=0$
我们首先对数组 $\textit{nums}$ 进行排序,然后从数组的第二个元素开始遍历,如果当前元素和前一个元素不相等,那么我们就将 $\textit{cnt}$ 加一,表示我们需要将当前元素减小到最小值的操作次数。然后我们将 $\textit{ans}$ 加上 $\textit{cnt}$,继续遍历下一个元素

遍历 $nums[1..n-1]$,如果当前元素 $nums[i]$ 不等于 $nums[i-1]$,则将 $cnt$ 加一。累加当前 $cnt$ 到答案 $ans$。

时间复杂度 $O(nlogn)$。
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 是数组 $\textit{nums}$ 的长度。

<!-- tabs:start -->

Expand All @@ -94,8 +92,8 @@ class Solution:
def reductionOperations(self, nums: List[int]) -> int:
nums.sort()
ans = cnt = 0
for i, v in enumerate(nums[1:]):
if v != nums[i]:
for a, b in pairwise(nums):
if a != b:
cnt += 1
ans += cnt
return ans
Expand Down Expand Up @@ -125,7 +123,7 @@ class Solution {
class Solution {
public:
int reductionOperations(vector<int>& nums) {
sort(nums.begin(), nums.end());
ranges::sort(nums);
int ans = 0, cnt = 0;
for (int i = 1; i < nums.size(); ++i) {
cnt += nums[i] != nums[i - 1];
Expand All @@ -139,16 +137,16 @@ public:
#### Go

```go
func reductionOperations(nums []int) int {
func reductionOperations(nums []int) (ans int) {
sort.Ints(nums)
ans, cnt := 0, 0
for i, v := range nums[1:] {
if v != nums[i] {
cnt := 0
for i, x := range nums[1:] {
if x != nums[i] {
cnt++
}
ans += cnt
}
return ans
return
}
```

Expand All @@ -157,10 +155,9 @@ func reductionOperations(nums []int) int {
```ts
function reductionOperations(nums: number[]): number {
nums.sort((a, b) => a - b);
let ans = 0;
let cnt = 0;
let [ans, cnt] = [0, 0];
for (let i = 1; i < nums.length; ++i) {
if (nums[i] != nums[i - 1]) {
if (nums[i] !== nums[i - 1]) {
++cnt;
}
ans += cnt;
Expand All @@ -169,81 +166,43 @@ function reductionOperations(nums: number[]): number {
}
```

#### JavaScript

```js
/**
* @param {number[]} nums
* @return {number}
*/
var reductionOperations = function (nums) {
nums.sort((a, b) => a - b);
let [ans, cnt] = [0, 0];
for (let i = 1; i < nums.length; ++i) {
if (nums[i] !== nums[i - 1]) {
++cnt;
}
ans += cnt;
}
return ans;
};
```

#### C#

```cs
public class Solution {
public int ReductionOperations(int[] nums) {
Array.Sort(nums);
int ans = 0, up = 0;
int ans = 0, cnt = 0;
for (int i = 1; i < nums.Length; i++) {
if (nums[i] != nums[i - 1]) {
up++;
++cnt;
}
ans += up;
}
return ans;
}
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- solution:start -->

### 方法二

<!-- tabs:start -->

#### Python3

```python
class Solution:
def reductionOperations(self, nums: List[int]) -> int:
ans = cnt = 0
for _, v in sorted(Counter(nums).items()):
ans += cnt * v
cnt += 1
return ans
```

#### Java

```java
class Solution {
public int reductionOperations(int[] nums) {
Map<Integer, Integer> tm = new TreeMap<>();
for (int v : nums) {
tm.put(v, tm.getOrDefault(v, 0) + 1);
}
int ans = 0, cnt = 0;
for (int v : tm.values()) {
ans += cnt * v;
++cnt;
ans += cnt;
}
return ans;
}
}
```

#### C++

```cpp
class Solution {
public:
int reductionOperations(vector<int>& nums) {
map<int, int> m;
for (int v : nums) ++m[v];
int ans = 0, cnt = 0;
for (auto [_, v] : m) {
ans += cnt * v;
++cnt;
}
return ans;
}
};
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Sorting

We first sort the array $\textit{nums}$, then iterate from the second element of the array. If the current element is not equal to the previous element, we increment $\textit{cnt}$, indicating the number of operations needed to reduce the current element to the minimum value. Then we add $\textit{cnt}$ to $\textit{ans}$ and continue to the next element.

The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$. Here, $n$ is the length of the array $\textit{nums}$.

<!-- tabs:start -->

Expand All @@ -86,8 +90,8 @@ class Solution:
def reductionOperations(self, nums: List[int]) -> int:
nums.sort()
ans = cnt = 0
for i, v in enumerate(nums[1:]):
if v != nums[i]:
for a, b in pairwise(nums):
if a != b:
cnt += 1
ans += cnt
return ans
Expand Down Expand Up @@ -117,7 +121,7 @@ class Solution {
class Solution {
public:
int reductionOperations(vector<int>& nums) {
sort(nums.begin(), nums.end());
ranges::sort(nums);
int ans = 0, cnt = 0;
for (int i = 1; i < nums.size(); ++i) {
cnt += nums[i] != nums[i - 1];
Expand All @@ -131,16 +135,16 @@ public:
#### Go

```go
func reductionOperations(nums []int) int {
func reductionOperations(nums []int) (ans int) {
sort.Ints(nums)
ans, cnt := 0, 0
for i, v := range nums[1:] {
if v != nums[i] {
cnt := 0
for i, x := range nums[1:] {
if x != nums[i] {
cnt++
}
ans += cnt
}
return ans
return
}
```

Expand All @@ -149,10 +153,9 @@ func reductionOperations(nums []int) int {
```ts
function reductionOperations(nums: number[]): number {
nums.sort((a, b) => a - b);
let ans = 0;
let cnt = 0;
let [ans, cnt] = [0, 0];
for (let i = 1; i < nums.length; ++i) {
if (nums[i] != nums[i - 1]) {
if (nums[i] !== nums[i - 1]) {
++cnt;
}
ans += cnt;
Expand All @@ -161,83 +164,44 @@ function reductionOperations(nums: number[]): number {
}
```

#### JavaScript

```js
/**
* @param {number[]} nums
* @return {number}
*/
var reductionOperations = function (nums) {
nums.sort((a, b) => a - b);
let [ans, cnt] = [0, 0];
for (let i = 1; i < nums.length; ++i) {
if (nums[i] !== nums[i - 1]) {
++cnt;
}
ans += cnt;
}
return ans;
};
```

#### C#

```cs
public class Solution {
public int ReductionOperations(int[] nums) {
Array.Sort(nums);
int ans = 0, up = 0;
int ans = 0, cnt = 0;
for (int i = 1; i < nums.Length; i++) {
if (nums[i] != nums[i - 1]) {
up++;
++cnt;
}
ans += up;
}
return ans;
}
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- solution:start -->

### Solution 2

<!-- tabs:start -->

#### Python3

```python
class Solution:
def reductionOperations(self, nums: List[int]) -> int:
ans = cnt = 0
for _, v in sorted(Counter(nums).items()):
ans += cnt * v
cnt += 1
return ans
```

#### Java

```java
class Solution {
public int reductionOperations(int[] nums) {
Map<Integer, Integer> tm = new TreeMap<>();
for (int v : nums) {
tm.put(v, tm.getOrDefault(v, 0) + 1);
}
int ans = 0, cnt = 0;
for (int v : tm.values()) {
ans += cnt * v;
++cnt;
ans += cnt;
}
return ans;
}
}
```

#### C++

```cpp
class Solution {
public:
int reductionOperations(vector<int>& nums) {
map<int, int> m;
for (int v : nums) ++m[v];
int ans = 0, cnt = 0;
for (auto [_, v] : m) {
ans += cnt * v;
++cnt;
}
return ans;
}
};
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
class Solution {
public:
int reductionOperations(vector<int>& nums) {
sort(nums.begin(), nums.end());
ranges::sort(nums);
int ans = 0, cnt = 0;
for (int i = 1; i < nums.size(); ++i) {
cnt += nums[i] != nums[i - 1];
ans += cnt;
}
return ans;
}
};
};
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
public class Solution {
public int ReductionOperations(int[] nums) {
Array.Sort(nums);
int ans = 0, up = 0;
int ans = 0, cnt = 0;
for (int i = 1; i < nums.Length; i++) {
if (nums[i] != nums[i - 1]) {
up++;
++cnt;
}
ans += up;
ans += cnt;
}
return ans;
}
Expand Down
Loading