#241 Counting Connected Regions in a Matrix
Write a method that accepts a matrix filled with positive numbers and returns the number of connected regions consisting of identical numbers:
public class Main
{
public static void main(String[] args) {
System.out.println("-----------START----------");
int[][] picture1 = new int[][]{
{2, 2, 2, 1},
{1, 2, 2, 1},
{1, 3, 1, 1},
{1, 1, 1, 3}
};
System.out.println(strokesRequired(picture1)); // expected 4
int[][] picture2 = new int[][]{
{1, 1, 1, 2, 1},
{1, 2, 1, 2, 1},
{1, 1, 1, 3, 1}
};
System.out.println(strokesRequired(picture2)); // expected 5
System.out.println("-----------FINISH----------");
}
private static int strokesRequired(int[][] arr) {
...
}
}Question review
Sign in to start the dialogue
The mentor keeps the context of the current question and your previous messages.
Sign in
Available with Premium
This is just one of the solutions available with Premium. And more:
- 250+ advanced lectures
- 1300+ practice exercises
- 500+ solutions to problems from real interviews
- Personalized Java Hub certificates
- Expanded access to the AI mentor