Skip to content

Instantly share code, notes, and snippets.

@inspector-ambitious
Last active November 16, 2021 07:36
Show Gist options
  • Select an option

  • Save inspector-ambitious/2261d33936fd9be99e08c89d40df03b7 to your computer and use it in GitHub Desktop.

Select an option

Save inspector-ambitious/2261d33936fd9be99e08c89d40df03b7 to your computer and use it in GitHub Desktop.

Largest Island

An island is a region of contiguous ones. A contiguous one is a 1 that is touched by at least one other 1, either horizontally, vertically or diagonally. Given a piece of the map, represented by a 2-D array, create a function that returns the area of the largest island.

To illustrate, if we were given the following piece of the map, we should return 4.

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

Our function should yield 2 for the map below:

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

Our function should yield 4 for the map below: :

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

Examples

largestIsland([
  [1, 0, 0],
  [0, 0, 0],
  [0, 0, 1]
])

➞ 1

largestIsland([
  [1, 1, 0],
  [0, 1, 1],
  [0, 0, 1]
])

➞ 5

largestIsland([
  [1, 0, 0],
  [1, 0, 0],
  [1, 0, 1]
])

➞ 3

Notes

  • Maps can be any m x n dimension.
  • Maps will always have at least 1 element. m >= 1 and n >= 1.

Code

using namespace std;
int largestIsland(vector<vector<int>> map) {
	
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment