15. Continents

Difficulty: 

A newly-discovered planet has a few continents with one or more countries in each continent. Countries within a continent have land borders with at least another country in the same continent, and no two countries from different continents have a land border. Given a matrix of land borders between countries, we would like to calculate how many continents there are.

The borders data for n countries is an n X n matrix, where a value of 1 at i-th row and j-th column means a direct land border between i-th and j-th countries, and 0 means no border. The matrix is symmetric, and no country has a border with itself.

Given the borders matrix, return the number of continents.

Input:

continents([
  [
    0,
    1,
    0
  ],
  [
    1,
    0,
    0
  ],
  [
    0,
    0,
    0
  ]
]);

Output:

2

Time Limit:

500 (ms)

Hint (hover to see):

  • Hint #1



Results

ProblemsAcceptedWrongTimed OutError
160000

Input:

Expected Output:

Output: