Code Monkey home page Code Monkey logo

lonely-island's Introduction

Lonely Island

A graph problem that creates very sad lonely island.

Reason:


I was click baited into a programming problem on youtube and paused it to solve it before watching the solution.


Problem:


Return all island's from the given matrix

0 = water

1 = land

An island is a piece of land that does not have adjacent land either up, right, down, or left of it's current location


Example:


Input:

[
   [1, 1, 0, 0, 0]
   [0, 1, 0, 0, 1]
   [1, 0, 0, 1, 0]
   [0, 0, 0, 0, 0]
   [1, 0, 1, 0, 1]
]

Output:

[
   ['~', '~', '~', '~', '~']
   ['~', '~', '~', '~',  1 ]
   [ 1 , '~', '~',  1 , '~']
   ['~', '~', '~', '~', '~']
   [ 1 , '~',  1 , '~',  1 ]
]

Solution:



O(n + m * 4)

n = row

m = col

4 = sum iteration

This simplifies down to O(n)

4 is constant so we can remove this

m given the length is constant we now know that our solution scales at the time complexity of

O(n)

//Get it? Like the Maldives but in the meta verse ๐Ÿฅ๐Ÿ
const metaDieves = [
    [1, 0, 0, 1, 1],
    [0, 1, 0, 1, 0],
    [1, 0, 0, 1, 0],
    [0, 1, 0, 0, 1],
    [0, 0, 0, 1, 0],
];

const isIsland = (island, row, column) => {
    const current = island[row][column];
    const nextRow = island[row + 1] || [];
    const prevRow = island[row - 1] || [];
    const currentRow = island[row];

    const top = prevRow[column] || 0;
    const bottom = nextRow[column] || 0;
    const left = currentRow[column - 1] || 0;
    const right = currentRow[column + 1] || 0;

   if (current === 0) {
       return false;
   };

   //TODO: Refactor this so that you can base a radius argument and dynamically check the surrounding cells
   // hence why the reduce is here and not a simple top + bottom + left + right
   const sum = [top, bottom, left, right].reduce((acc, cur) => acc + cur, 0);

   return !sum;  
};

const main = () => {

    const lonelyIslands = metaDieves.map((row, rowIndex) => {
       return row.map((col, columnIndex) => { 
              return  isIsland(metaDieves, rowIndex, columnIndex) ? 1 : "~";
       }, []);
    }, []);
    console.log(lonelyIslands);
};

main();

console.log:

[
  [  1 , '~', '~', '~', '~' ],
  [ '~',  1 , '~', '~', '~' ],
  [  1 , '~', '~', '~', '~' ],
  [ '~',  1 , '~', '~',  1  ],
  [ '~', '~', '~',  1 , '~' ]
]

lonely-island's People

Contributors

dudenamedjune avatar

Stargazers

Jacob Eugene Lara avatar

Watchers

James Cloos avatar  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.