Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Problem: You have been given a contract by a water company to scout out
- * new water sources for pumping. They need you to write a function that takes in
- * a 2D-bit array and returns an integer N representing the largest body of water found
- * in the 2D-bit array.
- *
- * 1 = Water
- * 0 = Land
- *
- * A body of water is represented by a cluster of connecting 1's.
- * Water can connect up, down, left, or right, but cannot connect diagonally.
- *
- * Ex:
- * [1, 1, 0, 0],
- * [1, 1, 0, 0],
- * [0, 0, 1, 1]
- * [0, 0, 0, 1]
- *
- * The number representing the largest body of water in this 2D-bit array is 4.
- *
- * Ex2:
- * [1, 1, 1, 1],
- * [1, 0, 0, 1],
- * [1, 0, 0, 1]
- * [1, 1, 1, 1]
- *
- * The number representing the largest body of water in this 2D-bit array is 12.
- */
- const map1 = [
- [1, 1, 0, 0],
- [1, 1, 0, 0],
- [0, 0, 1, 1],
- [0, 0, 0, 1]
- ];
- const map2 = [
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0],
- [0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0],
- [0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0],
- [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0],
- [0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0],
- [0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0]
- ];
- const map3 = [
- [1, 1, 1, 1, 1, 1, 1, 1],
- [0, 0, 0,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 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
- ];
- const map7 = [
- [1, 1, 1, 1],
- [1, 0, 0, 1],
- [1, 0, 0, 1],
- [1, 1, 1, 1]
- ];
- const expect = val => ({
- to: {
- be: val2 => {
- if (val !== val2) {
- throw new Error(`Expected: ${val}, Received: ${val2}`);
- } else {
- console.log(`${val} === ${val2}`);
- }
- }
- }
- });
- const driver = () => {
- expect(findLargestBodyOfWater(map1)).to.be(4);
- expect(findLargestBodyOfWater(map2)).to.be(16);
- expect(findLargestBodyOfWater(map3)).to.be(21);
- expect(findLargestBodyOfWater(map4)).to.be(42);
- expect(findLargestBodyOfWater(map5)).to.be(34);
- expect(findLargestBodyOfWater(map6)).to.be(33);
- expect(findLargestBodyOfWater(map7)).to.be(12);
- console.log("All Tests Passed!");
- };
- const findLargestBodyOfWater = grid => {
- /**
- * YOUR CODE HERE
- */
- };
- driver();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement