Medium

Given a rectangular matrix containing only digits, calculate the number of different 2 × 2 squares in it.

Example

For

matrix = [[1, 2, 1],

                   [2, 2, 2],

                   [2, 2, 2],

                   [1, 2, 3],

                   [2, 2, 1]]

the output should be
differentSquares(matrix) = 6.

Here are all 6 different 2 × 2 squares:

  • 1 2
    2 2
  • 2 1
    2 2
  • 2 2
    2 2
  • 2 2
    1 2
  • 2 2
    2 3
  • 2 3
    2 1

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.array.integer matrix

    Guaranteed constraints:
    1 ≤ matrix.length ≤ 100,
    1 ≤ matrix[i].length ≤ 100,
    0 ≤ matrix[i][j] ≤ 9.

  • [output] integer

    • The number of different 2 × 2 squares in matrix.

[C] Syntax Tips

// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
    char * answer = malloc(strlen(name) + 8);
    printf("This prints to the console when you Run Tests");
    strcpy(answer, "Hello, ");
    strcat(answer, name);
    return answer;
}

더보기

Solution

// Arrays are already defined with this interface:
// typedef struct arr_##name {
//   int size;
//   type *arr;
// } arr_##name;
//
// arr_##name alloc_arr_##name(int len) {
//   arr_##name a = {len, len > 0 ? malloc(sizeof(type) * len) : NULL};
//   return a;
// }
//
//
int differentSquares(arr_arr_integer matrix)
{
	int square[10000]={0, }, count=0;

	for(int i=0;i<matrix.size-1;i++)
		for(int j=0;j<matrix.arr[0].size-1;j++)
			square[1000*matrix.arr[i].arr[j]+100*matrix.arr[i].arr[j+1]+10*matrix.arr[i+1].arr[j]+matrix.arr[i+1].arr[j+1]]=1;

	for(int i=0;i<10000;i++)
		count+=square[i];

	return count;
}

 

728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> File Naming  (0) 2020.04.06
<Codesignal> digitsProduct  (0) 2020.04.06
<Codesignal> sumUpNumbers  (0) 2020.04.06
<Codesignal> Valid Time  (0) 2020.04.06
<Codesignal> longestWord  (0) 2020.04.06

+ Recent posts