Git Product home page Git Product logo

water-walls's Introduction

Water-Walls

Interactive solution for a variation of the classic "water walls" toy problem

Installing / Getting started

Clone github to your local machine, navigate to the root folder of the directory, then:

npm install
npm start

...then go to http://localhost:3000 on your browser.

Water Walls Problem Summary

We are going to represent a series of walls of different integer heights.

For example, given heights of [5, 3, 7, 2, 6, 4, 5, 9, 1, 2], it would look like this:

alt text

Now imagine that it rains.

alt text

Our goal is to determine which two walls have the most water trapped between them, and exactly how much water that is.

Output for the above: [3, 8, 11]...we're using 1 as the first index. This means that between wall #3 and wall #8, there are 11 blocks of water:

alt text

Solution Setup

Strategy

Phase 1)

Iterate through walls, keeping track of largest wall seen so far. For each wall, do three things:

A) If current wall is larger than largest wall, update largest wall B) Track all of the potentially trapped water that could exist on top of this current wall C) Check if the current wall successfully traps water on top of previously visited walls; if so, convert those "potential" spots of trapped water into actual spots of trapped water.

Phase 2)

At this point, we have an array of all of the positions with trapped water; find the largest amount of water trapped between two walls and return the wall indices (indices should start at 1) and the largest water total.

Inputs/Outputs

Input: const walls = [5, 3, 7, 2, 6, 4, 5, 9, 1, 2]; Output: let result = [3, 8, 11] Constraints/Edge Cases: none

Function Signatures

const getLargestTrappedWater = (walls) => { let potentialWater = {} let trappedWater = []; let largestWall = 0;

// for each wall... // if current wall greater than largest wall // update largest wall // update potential water that could be trapped on top of current wall // updatePotentialWater() // convert ant potential trapped water due to current wall // convertPotentialToTrapped()

// go over all of the trapped water // find largest total water between two walls // return the two wall indices and the total water trapped between them };

const updatePotentialWater = (currentWallHeight, currentWallIndex, largestWallHeight, potentialWater) => {

};

const convertPotentialToTrapped = (currentWall, potentialWater, trappedWater) => {

};

const findBiggestWaterTrap = (trappedWater) => {

let waterRecord = {left: 0, right: 0, water: 0}; let currentWater = 0;

// go over each position, looking for 0's (walls) // if 0, we've found first wall // create pointer to find next wall, set it to first wall + 1 // increment by one, stopping when we find next wall or get to end of array // while we see positions with water, add it to current water total // if curren water total is larger than max water seen // update waterRecord with first wall, second wall, and max total // return the two wall indices and the total water trapped between them };

water-walls's People

Contributors

chadam189 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.