-
-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problem: No.3397
No.3397.Maximum Number of Distinct Elements After Operations
- Loading branch information
Showing
7 changed files
with
225 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
solution/3300-3399/3397.Maximum Number of Distinct Elements After Operations/Solution.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class Solution { | ||
public: | ||
int maxDistinctElements(vector<int>& nums, int k) { | ||
ranges::sort(nums); | ||
int ans = 0, pre = INT_MIN; | ||
for (int x : nums) { | ||
int cur = min(x + k, max(x - k, pre + 1)); | ||
if (cur > pre) { | ||
++ans; | ||
pre = cur; | ||
} | ||
} | ||
return ans; | ||
} | ||
}; |
12 changes: 12 additions & 0 deletions
12
solution/3300-3399/3397.Maximum Number of Distinct Elements After Operations/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
func maxDistinctElements(nums []int, k int) (ans int) { | ||
sort.Ints(nums) | ||
pre := math.MinInt32 | ||
for _, x := range nums { | ||
cur := min(x+k, max(x-k, pre+1)) | ||
if cur > pre { | ||
ans++ | ||
pre = cur | ||
} | ||
} | ||
return | ||
} |
15 changes: 15 additions & 0 deletions
15
solution/3300-3399/3397.Maximum Number of Distinct Elements After Operations/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class Solution { | ||
public int maxDistinctElements(int[] nums, int k) { | ||
Arrays.sort(nums); | ||
int n = nums.length; | ||
int ans = 0, pre = Integer.MIN_VALUE; | ||
for (int x : nums) { | ||
int cur = Math.min(x + k, Math.max(x - k, pre + 1)); | ||
if (cur > pre) { | ||
++ans; | ||
pre = cur; | ||
} | ||
} | ||
return ans; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
solution/3300-3399/3397.Maximum Number of Distinct Elements After Operations/Solution.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class Solution: | ||
def maxDistinctElements(self, nums: List[int], k: int) -> int: | ||
nums.sort() | ||
ans = 0 | ||
pre = -inf | ||
for x in nums: | ||
cur = min(x + k, max(x - k, pre + 1)) | ||
if cur > pre: | ||
ans += 1 | ||
pre = cur | ||
return ans |
12 changes: 12 additions & 0 deletions
12
solution/3300-3399/3397.Maximum Number of Distinct Elements After Operations/Solution.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
function maxDistinctElements(nums: number[], k: number): number { | ||
nums.sort((a, b) => a - b); | ||
let [ans, pre] = [0, -Infinity]; | ||
for (const x of nums) { | ||
const cur = Math.min(x + k, Math.max(x - k, pre + 1)); | ||
if (cur > pre) { | ||
++ans; | ||
pre = cur; | ||
} | ||
} | ||
return ans; | ||
} |