Skip to content

ermolenkoA/AN14-onl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 

Repository files navigation

AN14-onl 📱 / Homeworks 📚

Here I will publish the solved tasks for the Android App Development course

Java Kotlin Android IntelliJ


The last homework :

Homework

Task:

Given a two-dimensional array of ints, consisting of 0 and 1.

Find the maximum number of squares that can be made from 1.

Example:

In
1 0 1 1
1 0 1 1
1 0 1 1
1 0 1 1
1 0 1 1
Out
19

Solution Code

 public static Integer countSquare(int[][] matrix) {
    int result = 0;
    
    for(int i = 0; i < matrix.length; ++i) {
        for(int j = 0; j < matrix[i].length; ++j) {
    
            if(matrix[i][j] == 1) {
                ++result;
                int maxSize = Math.min(matrix[i].length - j, matrix.length - i);
    
                //Пока возможен квадрат большего размера
                for (int counter = 1; counter < maxSize; ++counter, ++result){
    
                    //Проверяем соседние элементы снизу и справа
                    for(int k = 0; k < counter; ++k){
                        if (matrix[i + counter][j+k] != 1 || matrix[i+k][j + counter] != 1) {
                            counter = 0;
                            break;
                        }
                    }
    
                    //Если в соседняя или диоганальная клетка равна 0 - останавливаем цикл
                    if (counter == 0 || matrix[i + counter][j + counter] != 1){
                        break;
                    }
                }
            }
        }
    }
    
    return result;
 }

About

Homeworks for the android development course

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published