Timeline
timeline
2025-10-22
init
enumeration
Problem:
For a value nums[i], we need to find values in nums[i] that lie within [nums[i]-k, nums[i]+k]. Such values can become the target mode through one operation. However, it is incorrect to only consider nums[i], because nums[i] can also become any value within [nums[i]-k, nums[i]+k] as the target mode through one operation.
Then do we need to enumerate all values that the candidate target mode num[i] can become through one operation as target modes? The answer is no.
Core Concept
- Sorted Array
nums - Operable interval of each element:
[nums[i]-k, nums[i]+k] - Enumerate target mode val → count elements that can become val
Define window[val-k, val+k]:
- Left boundary l: the leftmost element contained in the window
- Right boundary r: the rightmost element contained in the window
range_size = r - l + 1
Maximum frequency:
Key observation
Only when the left or right boundary l or r of the window changes,
range_sizewill change → f_i may increase
- Assume val ∈
[nums[r]-k, nums[r+1]-k):- r unchanged
- l unchanged
- range_size constant → f_i unchanged
- All val inside the intervalwill not increase the maximum frequency→ no need to enumerate
Critical point analysis
Critical points of window boundary changes:
- Left boundary l change: val - k = nums[i] (left element just enters/leaves the window)
- Right boundary r change: val + k = nums[i] (right element just enters/leaves the window)
That is,Only when val equals some nums[i] ± k or nums[i] itself, the window boundary will change.
- val = nums[i] → corresponding window covers nums[i] itself
- val = nums[i] - k → window left boundary just covers nums[i]
- val = nums[i] + k → window right boundary just covers nums[i]
These three values areall possible critical points that can change the window range
In summary: we only need to enumerate the boundary values, i.e., enumerate {nums[i]-k, nums[i], nums[i]+k}.
1 |
|
