Re: [闲聊] 每日leetcode

楼主: JIWP (JIWP)   2024-12-11 20:31:43
2779. Maximum Beauty of an Array After Applying Operation

3347. Maximum Frequency of an Element After Performing Operations II
3346. Maximum Frequency of an Element After Performing Operations I
这两题很像
基本上改一下就可以了
思路:
(1)sliding windows
3个指标L1、L2、R
L2要满足nums[i]-nums[L2]<=2*k
L1要满足nums[i]-nums[L1]<=k
R要满足nums[R]-nums[i]>k
就这样维护idx-L2+1、R-L1的最大值就是答案
不过这样很慢就是了
(2)
找到nums里的最大值maxnum
建立一个长度为maxnum+2的矩阵arr
接着遍历nums
把arr[nums[i]-k]++
arr[nums[i]+k+1]

Links booklink

Contact Us: admin [ a t ] ucptt.com