Maximum Good Subarray Sum⚓︎
Solution⚓︎
See reference (Chinese).
Prefix sum:
\[
s\left[ 0 \right] =0
\]
\[
s\left[ 1 \right] =nums\left[ 0 \right]
\]
\[
s\left[ 2 \right] =nums\left[ 0 \right] +nums\left[ 1 \right]
\]
\[
\vdots
\]
\[
s\left[ i \right] =nums\left[ 0 \right] +\cdots +nums\left[ i-1 \right]
\]
\[
s\left[ i+1 \right] =nums\left[ 0 \right] +\cdots +nums\left[ i \right] =\sum_{j=0}^i{nums\left[ j \right]}
\]
\[
\sum_{k=l}^r{nums\left[ k \right]}=\left( nums\left[ 0 \right] +\cdots +nums\left[ r \right] \right) -\left( nums\left[ 0 \right] +\cdots +nums\left[ l-1 \right] \right) =s\left[ r+1 \right] -s\left[ l \right]
\]
We want to find:
\[
\max \left( s\left[ j+1 \right] -s\left[ i \right] \right) , s.t.\left| nums\left[ i \right] -nums\left[ j \right] \right|=k
\]
By iterating over j
, we need to calculate the minimum value of s[i]
, given nums[i] == nums[j] - k
or nums[i] == nums[j] + k
.
Define a hash map, where the key is nums[i]
, and the value is the minimum value of s[i]
for the same nums[i]
.
Note: Directly calculating prefix sums will result in TLE. We have to dynamically maintain the prefix sum.
- Time complexity: \(O(n)\);
- Space complexity: \(O(n)\).
Note: Python solution for reference: