Problem solution · C++

Peaks in Array

Peaks in Array: a C++ solution using segment tree or range structure. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Segment tree or range structure
Source
walkccc LeetCode Solutions
Length
81 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

Segment tree or range structure

For Peaks in Array, the implementation stores interval information in a range-query data structure so updates and queries avoid rescanning the full input.

  1. Choose the aggregate stored for each interval or prefix.
  2. Build or initialize the structure from the input.
  3. Apply updates and combine the affected nodes to answer each query.

Code notes

  • 81 lines of C++ from the credited upstream file 3187.cpp.
  • The implementation visibly relies on sequence storage.
  • 5 loop blocks detected.

Complexity

Count the build once, then multiply the logarithmic update or query path by the number of operations.

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

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codePeaks in Array · C++C++
Use this to learn the idea, then write your own version.
class FenwickTree { public:  FenwickTree(int n) : sums(n + 1) {}   void add(int i, int delta) {    while (i < sums.size()) {      sums[i] += delta;      i += lowbit(i);    }  }   int get(int i) const {    int sum = 0;    while (i > 0) {      sum += sums[i];      i -= lowbit(i);    }    return sum;  }  private:  vector<int> sums;   static inline int lowbit(int i) {    return i & -i;  }}; class Solution { public:  vector<int> countOfPeaks(vector<int>& nums, vector<vector<int>>& queries) {    vector<int> ans;    vector<int> peak = getPeak(nums);    FenwickTree tree(peak.size());     for (int i = 0; i < peak.size(); ++i)      tree.add(i + 1, peak[i]);     // Update the peak array and Fenwick tree if the peak status of nums[i]    // changes.    auto update = [&](int i) {      const int newPeak = isPeak(nums, i);      if (newPeak != peak[i]) {        tree.add(i + 1, newPeak - peak[i]);        peak[i] = newPeak;      }    };     for (const vector<int>& query : queries)      if (query[0] == 1) {        const int l = query[1];        const int r = query[2];        ans.push_back(r - l < 2 ? 0 : tree.get(r) - tree.get(l + 1));      } else if (query[0] == 2) {        const int index = query[1];        const int val = query[2];        nums[index] = val;        update(index);        if (index > 0)          update(index - 1);        if (index + 1 < nums.size())          update(index + 1);      }     return ans;  }  private:  vector<int> getPeak(const vector<int>& nums) {    vector<int> peak(nums.size());    for (int i = 1; i + 1 < nums.size(); ++i)      peak[i] = nums[i] > nums[i - 1] && nums[i] > nums[i + 1];    return peak;  }   bool isPeak(const vector<int>& nums, int i) {    return i > 0 && i + 1 < nums.size() && nums[i] > nums[i - 1] &&           nums[i] > nums[i + 1];  }}; 

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 ↗