The output completely matches conditions.
The only thing i can think about is, every time i find a rectangle i set it's 1s to 0s.
But it doesn't say in conditions you can't change arrays.
Thanks.
package com.codegym.task.task20.task2026;
import java.util.Arrays;
/*
Rectangle algorithms
*/
public class Solution {
public static void main(String[] args) {
byte[][] a1 = new byte[][]{
{1, 1, 0, 0},
{1, 1, 0, 0},
{1, 1, 0, 0},
{1, 1, 0, 1}
};
byte[][] a2 = new byte[][]{
{1, 0, 0, 1},
{0, 0, 0, 0},
{0, 0, 0, 0},
{1, 0, 0, 1}
};
int count1 = getRectangleCount(a1);
System.out.println("count = " + count1 + ". Must be 2");
int count2 = getRectangleCount(a2);
System.out.println("count = " + count2 + ". Must be 4");
}
public static int getRectangleCount(byte[][] a) {
int rect = 0;
for(int i=0; i<a.length; i++ ){
for(int j=0; j<a[0].length; j++){
if(a[i][j]==1) {
int m = findX(j, i, a);
int n = findY(j, i, a);
for(int c=i; c<=n; c++){
for(int r=j; r<=m; r++){
a[c][r] = 0;
}
}
rect++;
}
}
}
return rect;
}
public static int findX(int x, int y, byte[][] a) {
int count = -1;
for (int i = x; i < a[0].length; i++) {
if (a[y][i] == 1) count++;
else break;
}
return count;
}
public static int findY(int x, int y, byte[][] a) {
int count = -1;
for (int i = y; i < a.length; i++) {
if (a[i][x] == 1) count++;
else break;
}
return count;
}
}