Skip to content

Commit e70a566

Browse files
committed
chore: update 3397. Maximum Number of Distinct Elements After Operations
1 parent 9af9e14 commit e70a566

4 files changed

Lines changed: 142 additions & 0 deletions

File tree

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
---
2+
title: 3397. Maximum Number of Distinct Elements After Operations
3+
description: You are given an integer array nums and an integer k.You are allowed to perform the following operation on each element of the array at most once:Add an integer in the range [-k, k] to the element.Return the maximum possible number of distinct elements in nums after performing the operations.
4+
keywords:
5+
[
6+
LeetCode,
7+
3397. Maximum Number of Distinct Elements After Operations,
8+
Medium,
9+
Array,
10+
Greedy,
11+
Sorting,
12+
]
13+
tags: [LeetCode, Medium, Array, Greedy, Sorting]
14+
---
15+
16+
# [{frontMatter.title}](https://leetcode.com/problems/maximum-number-of-distinct-elements-after-operations)
17+
18+
import Tabs from "@theme/Tabs";
19+
import TabItem from "@theme/TabItem";
20+
import CodeBlock from "@theme/CodeBlock";
21+
import DifficultyBadge from "@site/src/components/Badges/DifficultyBadge";
22+
import Solution from "!!raw-loader!./solution.js";
23+
import Description from "./_Description.md";
24+
import Examples from "./_Examples.md";
25+
26+
<DifficultyBadge difficulty="Medium" />
27+
28+
<Tabs>
29+
<TabItem value="description" label="題目描述" default>
30+
<details open>
31+
<summary>Description</summary>
32+
<Description />
33+
<Examples />
34+
</details>
35+
</TabItem>
36+
37+
<TabItem value="solution" label="解答">
38+
## Solution
39+
<CodeBlock language="js">{Solution}</CodeBlock>
40+
</TabItem>
41+
</Tabs>
42+
43+
### 解題思路
44+
45+
題目給的是一個整數陣列 `nums` 和一個整數 `k`,每個數最多可加上或減去一個介於 `[-k, k]` 的值(也可以不加減),目標是找出最多有幾個不同的數字。
46+
47+
1. 首先由小到大排序 `nums`,並定義兩個變數 `count``prev`
48+
49+
:::info
50+
51+
需要由小到大排序的原因是要確保貪婪演算法的策略正確
52+
例如 [3,1,1], k=1,若不排序:
53+
54+
1. 先處理 3, 根據貪婪演算法放 num - k = 2,
55+
2. 接下來處理 1 ,根據貪婪演算法放 prev + 1 = 3 ,但範圍是 [0,2], 3 超出範圍不能放。
56+
57+
排序後 [1,1,3]
58+
59+
1. 前面的 1 可以放 0,1,2,根據貪婪演算法放 0
60+
2. 第 2 個 1 放 prev + 1 = 1
61+
3. 後面的 3 放 num - k = 3
62+
63+
這樣就可以塞更多不重複數。
64+
65+
:::
66+
67+
```js
68+
nums.sort((a, b) => a - b);
69+
70+
let count = 0; // 記錄目前有幾個不同的數字
71+
let prev = -Infinity; // 記錄上一個不重複的數字,初始值為負無限大,確保一定會比下一個數字小
72+
```
73+
74+
1. 逐步處理每個數,主要邏輯是找出這個數最小且不重複的可以放的值。
75+
76+
```js
77+
for (const num of nums) {
78+
let curr = Math.max(num - k, prev + 1);
79+
// num - k 代表當前這個數最小能變成的值
80+
// prev + 1 代表必須比前一個大才不重複
81+
// Math.max 用以取兩個值中較大的,確保合規定又不重複
82+
if (curr <= num + k) {
83+
// 如果這個數可以放
84+
count++;
85+
prev = curr; // 更新 prev 來記錄上一個數字
86+
}
87+
}
88+
```
89+
90+
3. 最後回傳 `count` 就是答案。
91+
92+
```js
93+
return count;
94+
```
95+
96+
### 心得
97+
98+
貪婪演算法
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
You are given an integer array nums and an integer k.
2+
3+
You are allowed to perform the following operation on each element of the array at most once:
4+
5+
- Add an integer in the range [-k, k] to the element.
6+
7+
Return the maximum possible number of distinct elements in nums after performing the operations.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#### Example 1:
2+
3+
> **Input:** nums = [1,2,2,3,3,4], k = 2
4+
> **Output:** 6
5+
> **Explanation:**
6+
> nums changes to [-1, 0, 1, 2, 3, 4] after performing operations on the first four elements.
7+
8+
#### Example 2:
9+
10+
> **Input:** nums = [4,4,4,4], k = 1
11+
> **Output:** 3
12+
> **Explanation:**
13+
> By adding -1 to nums[0] and 1 to nums[1], nums changes to [3, 5, 4, 4].
14+
15+
#### Constraints:
16+
17+
- `1 <= nums.length <= 105`
18+
- `-1 <= nums[i] <= 109`
19+
- `0 <= k <= 109`
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
var maxDistinctElements = function (nums, k) {
7+
nums.sort((a, b) => a - b);
8+
let count = 0;
9+
let prev = -Infinity;
10+
for (const num of nums) {
11+
let curr = Math.max(num - k, prev + 1);
12+
if (curr <= num + k) {
13+
count++;
14+
prev = curr;
15+
}
16+
}
17+
return count;
18+
};

0 commit comments

Comments
 (0)