Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: trekhleb/javascript-algorithms
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: e97ffd0bae7d4e5e903cd91ae934d6231d62a938
Choose a base ref
..
head repository: trekhleb/javascript-algorithms
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: a2743952d3f966f368923e675ae8fb0b91beff21
Choose a head ref
Showing with 2 additions and 2 deletions.
  1. +2 −2 src/algorithms/uncategorized/rain-terraces/dpRainTerraces.js
4 changes: 2 additions & 2 deletions src/algorithms/uncategorized/rain-terraces/dpRainTerraces.js
Original file line number Diff line number Diff line change
@@ -5,11 +5,11 @@
* @return {number}
*/
export default function dpRainTerraces(terraces) {
// Obtain the maximum elevations to the left & right of each terrace (inclusive).
// Obtain the maximum elevations to the left and right of each terrace (inclusive).
const maxL = terraces.reduce((acc, val, idx) => [...acc, Math.max(val, (acc[idx - 1] || 0))], []);
const maxR = terraces.reduceRight((acc, val) => [Math.max(val, (acc[0] || 0)), ...acc], []);

// Convert maximum values to amount of water at current index & sum water values.
// Convert maximum values to amount of water at current index and sum water values.
const trap = terraces.map((val, idx) => Math.max(0, Math.min(maxL[idx], maxR[idx]) - val));
return trap.reduce((acc, val) => acc + val);
}