Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Coding Interview
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Container registry
Model registry
Analyze
Contributor analytics
Repository analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Dmytro Bogatov
Coding Interview
Commits
b276dd6d
Verified
Commit
b276dd6d
authored
May 30, 2020
by
Dmytro Bogatov
Browse files
Options
Downloads
Patches
Plain Diff
Add count the islands problem.
parent
2894ec42
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/hacker-rank/CountTheIslands.cs
+66
-0
66 additions, 0 deletions
src/hacker-rank/CountTheIslands.cs
test/hacker-rank/CountTheIslands.cs
+25
-0
25 additions, 0 deletions
test/hacker-rank/CountTheIslands.cs
with
91 additions
and
0 deletions
src/hacker-rank/CountTheIslands.cs
0 → 100644
+
66
−
0
View file @
b276dd6d
namespace
CodingInterview.HackerRank
{
/// <summary>
/// Given a boolean matrix, find the number of islands.
///
/// What is an island?
///
/// A group of connected 1s forms an island.
/// For example, the below matrix contains 5 islands:
///
/// {1, 1, 0, 0, 0},
/// {0, 1, 0, 0, 1},
/// {1, 0, 0, 1, 1},
/// {0, 0, 0, 0, 0},
/// {1, 0, 1, 0, 1}
/// </summary>
public
class
CountTheIslands
{
static
int
[,]
map
;
/// <summary>
/// Returns the number of islands
/// </summary>
/// <param name="map">the map matrix</param>
/// <returns>the number of islands</returns>
public
int
Solve
(
int
[,]
map
)
{
CountTheIslands
.
map
=
map
;
var
islands
=
0
;
for
(
var
i
=
0
;
i
<
map
.
GetLength
(
0
);
i
++)
{
for
(
var
j
=
0
;
j
<
map
.
GetLength
(
1
);
j
++)
{
if
(
DFS
(
i
,
j
))
{
islands
++;
}
}
}
return
islands
;
}
static
bool
DFS
(
int
i
,
int
j
)
{
if
(
i
<
0
||
i
==
map
.
GetLength
(
0
)
||
j
<
0
||
j
==
map
.
GetLength
(
1
)
||
map
[
i
,
j
]
==
0
||
map
[
i
,
j
]
==
2
)
{
return
false
;
}
map
[
i
,
j
]
=
2
;
DFS
(
i
,
j
+
1
);
// right
DFS
(
i
+
1
,
j
+
1
);
// right down
DFS
(
i
+
1
,
j
);
// down
DFS
(
i
+
1
,
j
-
1
);
// left down
DFS
(
i
,
j
-
1
);
// left
DFS
(
i
-
1
,
j
-
1
);
// left up
DFS
(
i
-
1
,
j
);
// up
DFS
(
i
-
1
,
j
+
1
);
// right up
return
true
;
}
}
}
This diff is collapsed.
Click to expand it.
test/hacker-rank/CountTheIslands.cs
0 → 100644
+
25
−
0
View file @
b276dd6d
using
Xunit
;
namespace
CodingInterview.Tests.HackerRank
{
public
class
CountTheIslands
{
[
Fact
]
public
void
TestCases
()
{
// From Hacker Rank
Assert
.
Equal
(
5
,
new
CodingInterview
.
HackerRank
.
CountTheIslands
().
Solve
(
new
int
[,]
{
{
1
,
1
,
0
,
0
,
0
},
{
0
,
1
,
0
,
0
,
1
},
{
1
,
0
,
0
,
1
,
1
},
{
0
,
0
,
0
,
0
,
0
},
{
1
,
0
,
1
,
0
,
1
}
}
)
);
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
sign in
to comment