Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 50d41c2

Browse files
feat: add solutions to lc problem: No.3397 (#3878)
No.3397.Maximum Number of Distinct Elements After Operations
1 parent 1f07b03 commit 50d41c2

File tree

7 files changed

+225
-8
lines changed

7 files changed

+225
-8
lines changed

‎solution/3300-3399/3397.Maximum Number of Distinct Elements After Operations/README.md‎

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,32 +66,108 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3397.Ma
6666

6767
<!-- solution:start -->
6868

69-
### 方法一
69+
### 方法一:贪心 + 排序
70+
71+
我们不妨对数组 $\textit{nums}$ 排序,然后从左到右考虑每个元素 $x$。
72+
73+
对于第一个元素,我们可以贪心地将其变为 $x - k,ドル这样可以使得 $x$ 尽可能小,给后续的元素留下更多的空间。我们用变量 $\textit{pre}$ 当前使用到的元素的最大值,初始化为负无穷大。
74+
75+
对于后续的元素 $x,ドル我们可以贪心地将其变为 $\min(x + k, \max(x - k, \textit{pre} + 1))$。这里的 $\max(x - k, \textit{pre} + 1)$ 表示我们尽可能地将 $x$ 变得更小,但不能小于 $\textit{pre} + 1,ドル如果存在该值,且小于 $x + k,ドル我们就可以将 $x$ 变为该值,不重复元素数加一,然后我们更新 $\textit{pre}$ 为该值。
76+
77+
遍历结束,我们就得到了不重复元素的最大数量。
78+
79+
时间复杂度 $O(n \times \log n),ドル空间复杂度 $O(\log n)$。其中 $n$ 为数组 $\textit{nums}$ 的长度。
7080

7181
<!-- tabs:start -->
7282

7383
#### Python3
7484

7585
```python
76-
86+
class Solution:
87+
def maxDistinctElements(self, nums: List[int], k: int) -> int:
88+
nums.sort()
89+
ans = 0
90+
pre = -inf
91+
for x in nums:
92+
cur = min(x + k, max(x - k, pre + 1))
93+
if cur > pre:
94+
ans += 1
95+
pre = cur
96+
return ans
7797
```
7898

7999
#### Java
80100

81101
```java
82-
102+
class Solution {
103+
public int maxDistinctElements(int[] nums, int k) {
104+
Arrays.sort(nums);
105+
int n = nums.length;
106+
int ans = 0, pre = Integer.MIN_VALUE;
107+
for (int x : nums) {
108+
int cur = Math.min(x + k, Math.max(x - k, pre + 1));
109+
if (cur > pre) {
110+
++ans;
111+
pre = cur;
112+
}
113+
}
114+
return ans;
115+
}
116+
}
83117
```
84118

85119
#### C++
86120

87121
```cpp
88-
122+
class Solution {
123+
public:
124+
int maxDistinctElements(vector<int>& nums, int k) {
125+
ranges::sort(nums);
126+
int ans = 0, pre = INT_MIN;
127+
for (int x : nums) {
128+
int cur = min(x + k, max(x - k, pre + 1));
129+
if (cur > pre) {
130+
++ans;
131+
pre = cur;
132+
}
133+
}
134+
return ans;
135+
}
136+
};
89137
```
90138
91139
#### Go
92140
93141
```go
142+
func maxDistinctElements(nums []int, k int) (ans int) {
143+
sort.Ints(nums)
144+
pre := math.MinInt32
145+
for _, x := range nums {
146+
cur := min(x+k, max(x-k, pre+1))
147+
if cur > pre {
148+
ans++
149+
pre = cur
150+
}
151+
}
152+
return
153+
}
154+
```
94155

156+
#### TypeScript
157+
158+
```ts
159+
function maxDistinctElements(nums: number[], k: number): number {
160+
nums.sort((a, b) => a - b);
161+
let [ans, pre] = [0, -Infinity];
162+
for (const x of nums) {
163+
const cur = Math.min(x + k, Math.max(x - k, pre + 1));
164+
if (cur > pre) {
165+
++ans;
166+
pre = cur;
167+
}
168+
}
169+
return ans;
170+
}
95171
```
96172

97173
<!-- tabs:end -->

‎solution/3300-3399/3397.Maximum Number of Distinct Elements After Operations/README_EN.md‎

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,32 +64,108 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3397.Ma
6464

6565
<!-- solution:start -->
6666

67-
### Solution 1
67+
### Solution 1: Greedy + Sorting
68+
69+
We can sort the array $\textit{nums}$ and then consider each element $x$ from left to right.
70+
71+
For the first element, we can greedily change it to $x - k,ドル making $x$ as small as possible to leave more space for subsequent elements. We use the variable $\textit{pre}$ to track the maximum value of the elements used so far, initialized to negative infinity.
72+
73+
For subsequent elements $x,ドル we can greedily change it to $\min(x + k, \max(x - k, \textit{pre} + 1))$. Here, $\max(x - k, \textit{pre} + 1)$ means we try to make $x$ as small as possible but not smaller than $\textit{pre} + 1$. If this value exists and is less than $x + k,ドル we can change $x$ to this value, increment the count of distinct elements, and update $\textit{pre}$ to this value.
74+
75+
After traversing the array, we obtain the maximum number of distinct elements.
76+
77+
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}$.
6878

6979
<!-- tabs:start -->
7080

7181
#### Python3
7282

7383
```python
74-
84+
class Solution:
85+
def maxDistinctElements(self, nums: List[int], k: int) -> int:
86+
nums.sort()
87+
ans = 0
88+
pre = -inf
89+
for x in nums:
90+
cur = min(x + k, max(x - k, pre + 1))
91+
if cur > pre:
92+
ans += 1
93+
pre = cur
94+
return ans
7595
```
7696

7797
#### Java
7898

7999
```java
80-
100+
class Solution {
101+
public int maxDistinctElements(int[] nums, int k) {
102+
Arrays.sort(nums);
103+
int n = nums.length;
104+
int ans = 0, pre = Integer.MIN_VALUE;
105+
for (int x : nums) {
106+
int cur = Math.min(x + k, Math.max(x - k, pre + 1));
107+
if (cur > pre) {
108+
++ans;
109+
pre = cur;
110+
}
111+
}
112+
return ans;
113+
}
114+
}
81115
```
82116

83117
#### C++
84118

85119
```cpp
86-
120+
class Solution {
121+
public:
122+
int maxDistinctElements(vector<int>& nums, int k) {
123+
ranges::sort(nums);
124+
int ans = 0, pre = INT_MIN;
125+
for (int x : nums) {
126+
int cur = min(x + k, max(x - k, pre + 1));
127+
if (cur > pre) {
128+
++ans;
129+
pre = cur;
130+
}
131+
}
132+
return ans;
133+
}
134+
};
87135
```
88136
89137
#### Go
90138
91139
```go
140+
func maxDistinctElements(nums []int, k int) (ans int) {
141+
sort.Ints(nums)
142+
pre := math.MinInt32
143+
for _, x := range nums {
144+
cur := min(x+k, max(x-k, pre+1))
145+
if cur > pre {
146+
ans++
147+
pre = cur
148+
}
149+
}
150+
return
151+
}
152+
```
92153

154+
#### TypeScript
155+
156+
```ts
157+
function maxDistinctElements(nums: number[], k: number): number {
158+
nums.sort((a, b) => a - b);
159+
let [ans, pre] = [0, -Infinity];
160+
for (const x of nums) {
161+
const cur = Math.min(x + k, Math.max(x - k, pre + 1));
162+
if (cur > pre) {
163+
++ans;
164+
pre = cur;
165+
}
166+
}
167+
return ans;
168+
}
93169
```
94170

95171
<!-- tabs:end -->
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
int maxDistinctElements(vector<int>& nums, int k) {
4+
ranges::sort(nums);
5+
int ans = 0, pre = INT_MIN;
6+
for (int x : nums) {
7+
int cur = min(x + k, max(x - k, pre + 1));
8+
if (cur > pre) {
9+
++ans;
10+
pre = cur;
11+
}
12+
}
13+
return ans;
14+
}
15+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
func maxDistinctElements(nums []int, k int) (ans int) {
2+
sort.Ints(nums)
3+
pre := math.MinInt32
4+
for _, x := range nums {
5+
cur := min(x+k, max(x-k, pre+1))
6+
if cur > pre {
7+
ans++
8+
pre = cur
9+
}
10+
}
11+
return
12+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int maxDistinctElements(int[] nums, int k) {
3+
Arrays.sort(nums);
4+
int n = nums.length;
5+
int ans = 0, pre = Integer.MIN_VALUE;
6+
for (int x : nums) {
7+
int cur = Math.min(x + k, Math.max(x - k, pre + 1));
8+
if (cur > pre) {
9+
++ans;
10+
pre = cur;
11+
}
12+
}
13+
return ans;
14+
}
15+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def maxDistinctElements(self, nums: List[int], k: int) -> int:
3+
nums.sort()
4+
ans = 0
5+
pre = -inf
6+
for x in nums:
7+
cur = min(x + k, max(x - k, pre + 1))
8+
if cur > pre:
9+
ans += 1
10+
pre = cur
11+
return ans
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function maxDistinctElements(nums: number[], k: number): number {
2+
nums.sort((a, b) => a - b);
3+
let [ans, pre] = [0, -Infinity];
4+
for (const x of nums) {
5+
const cur = Math.min(x + k, Math.max(x - k, pre + 1));
6+
if (cur > pre) {
7+
++ans;
8+
pre = cur;
9+
}
10+
}
11+
return ans;
12+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /