Problem solution · C++

Minimum Operations to Achieve at Least K Peaks

Minimum Operations to Achieve at Least K Peaks: a C++ solution using sliding window or two pointers. Learn the idea, check the complexity, and read the full code, with credit to Kamyu LeetCode Solutions.

Technique
Sliding window or two pointers
Source
Kamyu LeetCode Solutions
Length
44 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Sliding window or two pointers

For Minimum Operations to Achieve at Least K Peaks, the implementation maintains a moving interval and updates only the information that enters or leaves the window.

  1. Choose the invariant that makes a window valid or useful.
  2. Advance the right boundary and add the new element.
  3. Move the left boundary only as needed while maintaining the invariant and updating the answer.

Code notes

  • 44 lines of C++ from the credited upstream file minimum-operations-to-achieve-at-least-k-peaks.cpp.
  • The implementation visibly relies on sequence storage, work queue.
  • 2 loop blocks detected.

Complexity

Confirm that neither pointer moves backwards; if so, the scan is usually linear apart from the window’s data-structure operations.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from Kamyu LeetCode Solutions by kamyu104 and is used under the MIT licence.

Full codeMinimum Operations to Achieve at Least K Peaks · C++C++
Use this to learn the idea, then write your own version.
// Time:  O(n + klogn)// Space: O(n) // greedy, heap, doubly linked listclass Solution {public:    int minOperations(vector<int>& nums, int k) {        if (2 * k > size(nums)) {            return -1;        }        if (!k) {            return 0;        }        vector<bool> lookup(size(nums));        vector<int> left(size(nums)), right(size(nums)), cost(size(nums));        vector<pair<int, int>> pairs(size(nums));        for (int i = 0; i < size(nums); ++i) {            left[i] = (size(nums) + (i - 1)) % size(nums);            right[i] = (size(nums) + (i + 1)) % size(nums);            cost[i] = max((max(nums[left[i]], nums[right[i]]) + 1) - nums[i], 0);            pairs[i] = pair(cost[i], i);        }        priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> min_heap(cbegin(pairs), cend(pairs));        int result = 0;        while (!empty(min_heap)) {            const auto [c, i] = min_heap.top(); min_heap.pop();            if (lookup[i]) {                continue;            }            result += c;            if (!--k) {                break;            }            cost[i] = cost[left[i]] + cost[right[i]] - cost[i];            min_heap.emplace(cost[i], i);            lookup[left[i]] = lookup[right[i]] = true;            left[i] = left[left[i]];            right[i] = right[right[i]];            right[left[i]] = left[right[i]] = i;        }        return result;    }}; 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗