当前位置: 首页 > news >正文

[LeetCode] 3397. Maximum Number of Distinct Elements After Operations

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.

Example 1:
Input: nums = [1,2,2,3,3,4], k = 2
Output: 6

Explanation:
nums changes to [-1, 0, 1, 2, 3, 4] after performing operations on the first four elements.

Example 2:
Input: nums = [4,4,4,4], k = 1
Output: 3

Explanation:
By adding -1 to nums[0] and 1 to nums[1], nums changes to [3, 5, 4, 4].

Constraints:
1 <= nums.length <= 105
1 <= nums[i] <= 109
0 <= k <= 109

执行操作后不同元素的最大数量。

给你一个整数数组 nums 和一个整数 k。

你可以对数组中的每个元素 最多 执行 一次 以下操作:

将一个在范围 [-k, k] 内的整数加到该元素上。
返回执行这些操作后,nums 中可能拥有的不同元素的 最大 数量。

思路

思路是贪心。题目给的是一个 nums 数组和一个整数 k。首先这道题必须要排序,或者需要有一个类似 treemap 的数据结构使得最后的结果有序,否则是不太好判断某个数字是否存在的。这里我选择排序。

对 input 数组排序过后,我们从第一个数字开始判断。这里的思路是需要让每个数字尽量小,这样才能留出更多余地给后面的数字让他们尽量不重复。对于第一个数字 nums[0],我们可以把它变成 nums[0] - k,这样是最小的。然后我们记录下当前的 pre = nums[0] - k。为什么变量叫 pre 呢?因为我们需要记录前一个数字的值,方便和后面的数字比较。那么对于下一个数字 nums[1],他需要满足

  • 介于 [nums[1] - k, nums[1] + k] 之间
  • 大于 pre
  • 所以其实是介于 [max(pre + 1, nums[1] - k), nums[1] + k] 之间

如果数字满足这个区间,则说明找到了一个新的不同数字,我们就把 pre 更新为 max(pre + 1, nums[1] - k),并且结果 count++。如果不满足这个区间,则说明无法找到一个新的不同数字,我们就跳过这个数字,继续下一个数字的判断。

复杂度

时间O(nlogn),排序的时间复杂度
空间O(1)

代码

Java实现

class Solution {public int maxDistinctElements(int[] nums, int k) {Arrays.sort(nums);int count = 0;int pre = Integer.MIN_VALUE;for (int num : nums) {int left = num - k;int right = num + k;int cur = Math.max(pre + 1, left);if (cur <= right) {count++;pre = cur;}}return count;}
}
http://www.jsqmd.com/news/16424/

相关文章:

  • Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
  • Cisco IOS XRv 9000 Router IOS XR Release 7.11.2 MD - 思科 IOS XR 网络操作系统
  • 设备端语音处理技术解析
  • 毕设项目基于SpringBoot的河南非遗助推平台\251004(白嫖源码+演示录像)可做计算机毕业设计JAVA、PHP、爬虫、APP、小程序、C#、C++、python、资料可视化、文案
  • 语音技术跨学科研究新趋势
  • 罗马a线 宜家 大致地图 Roma Anagnina
  • 中国晶圆厂排行榜:谁是下一个台积电
  • 一首很棒的歌
  • 机器学习优化云虚拟机部署技术解析
  • tryhackme-预安全-网络基础知识-什么是网络-04
  • C++ std::function简单笔记
  • 【C++】基于asio的异步https server
  • tryhackme-预安全-网络安全简介-防御性安全简介-02
  • 明天发点东西
  • 嵌入式第六周作业任务二--PWM呼吸灯
  • 2022 ICPC Shenyang
  • tryhackme-预安全-网络安全简介-进攻性安全简介-01
  • 20231326第五周预习报告
  • 复矩阵的奇异值分解(SVD)
  • idea与cursor的整合方案
  • Codeforces Round 496 (Div. 3) F. Berland and the Shortest Paths
  • Dotnet通过Http2解决CVE-2025-55315高危漏洞
  • 【开源】目前最方便的retroarch模拟器游戏封面获取方式
  • PWN手的成长之路-18_铁人三项(第五赛区)_2018_rop
  • 日志|JAVAWEB|YApi|vue-cli|VUE-Element
  • 2025.10.18——1黄
  • 幂等的双倍快乐,你值得拥有
  • 10.18总结
  • 10.17总结
  • 软考中级学习总结(2)