Problem solution · Java

Peaks in Array

Peaks in Array: a Java 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
79 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

  • 79 lines of Java from the credited upstream file 3187.java.
  • 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 · JavaJava
Use this to learn the idea, then write your own version.
class FenwickTree {  public FenwickTree(int n) {    sums = new int[n + 1];  }   public void add(int i, int delta) {    while (i < sums.length) {      sums[i] += delta;      i += lowbit(i);    }  }   public int get(int i) {    int sum = 0;    while (i > 0) {      sum += sums[i];      i -= lowbit(i);    }    return sum;  }   private int[] sums;   private static int lowbit(int i) {    return i & -i;  }} class Solution {  public List<Integer> countOfPeaks(int[] nums, int[][] queries) {    List<Integer> ans = new ArrayList<>();    int[] peak = getPeak(nums);    FenwickTree tree = new FenwickTree(peak.length);     for (int i = 0; i < peak.length; ++i)      tree.add(i + 1, peak[i]);     // Update the peak array and Fenwick tree if the peak status of nums[i]    // changes.    for (int[] query : queries) {      if (query[0] == 1) {        final int l = query[1];        final int r = query[2];        ans.add(r - l < 2 ? 0 : tree.get(r) - tree.get(l + 1));      } else if (query[0] == 2) {        final int index = query[1];        final int val = query[2];        nums[index] = val;        update(nums, peak, tree, index);        if (index > 0)          update(nums, peak, tree, index - 1);        if (index + 1 < nums.length)          update(nums, peak, tree, index + 1);      }    }     return ans;  }   private void update(int[] nums, int[] peak, FenwickTree tree, int i) {    final int newPeak = isPeak(nums, i) ? 1 : 0;    if (newPeak != peak[i]) {      tree.add(i + 1, newPeak - peak[i]);      peak[i] = newPeak;    }  }   private int[] getPeak(int[] nums) {    int[] peak = new int[nums.length];    for (int i = 1; i + 1 < nums.length; i++)      peak[i] = nums[i] > nums[i - 1] && nums[i] > nums[i + 1] ? 1 : 0;    return peak;  }   private boolean isPeak(int[] nums, int i) {    return i > 0 && i + 1 < nums.length && 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 ↗