Similar Problems

Similar Problems not available

Mirror Reflection - Leetcode Solution

Companies:

LeetCode:  Mirror Reflection Leetcode Solution

Difficulty: Medium

Topics: math  

Problem Statement:

Given a matrix of integers A with R rows and C columns, we can change any and all integers in any row or column to -1.

Return the largest number of rows that can have all values equal after some number of moves.

Example 1:

Input: [[0,0,1],[1,1,1],[1,0,0]] Output: 2 Explanation: After flipping no values, 0 and 1 were the most common values in the rows. After flipping values in the second and third columns, [0, 0, 1] and [1, 1, 1] became equally common. Example 2:

Input: [[0,0,0],[0,0,1],[1,1,0]] Output: 1 Explanation: After flipping values in the first column, all rows have a value of 0. Example 3:

Input: [[0,0,0],[0,0,1],[1,1,1]] Output: 0 Explanation: No matter how we flip values in the matrix, we can't make all rows have the same value.

Note:

1 <= A.length = A[0].length <= 20 0 <= A[i][j] <= 1

Solution:

The solution uses the principle of the Mirror Reflection. If the matrix has ‘m’ rows and ‘n’ columns.

We need to count the number of rows that can become diagonal reflection of another row. For example, if we have rows 1, 2, and 3, and row 3 is the diagonal reflection of row 1, then we can pick one of the rows 1 or 3.

Let’s calculate the diagonal mirror reflection. For a given row, we start by comparing element 0 with the element n-1, then compare element 1 with n-2, and so on. If all the corresponding elements are equal, then the row is a diagonal mirror reflection of another row.

Algorithm:

  1. Initialize two pointers to the first and last columns.
  2. While the left pointer is smaller than the right pointer, if all the corresponding elements of the two pointers are equal, we can stop the process and move to the next row. If not, we need to change any of the compared values in the row.
  3. After the change, we need to check if the row is the diagonal mirror reflection of another row. If it is, we can increment the count of possible row reflections.
  4. Repeat the process for all rows.
  5. Return the maximum count of possible row reflections.

Let's look at the Python3 solution for the problem -

class Solution: def maxEqualRowsAfterFlips(self, matrix: List[List[int]]) -> int:

    def calculate(p1,p2):
        count = 0
        for row in matrix:
            if row[p1] != row[p2]:
                if row[p1] == 0:
                    row[p1] = 1
                else:
                    row[p1] = 0
            if row == matrix[0]:
                count += 1
            elif row == matrix[1]:
                count += 1
            else:
                flip = True
                for i in range(len(row)):
                    if row[i] != row[-i-1]:
                        flip = False
                if flip:
                    count += 1
        return count
    
    result = 0
    for i in range(len(matrix[0])):
        for j in range(i,len(matrix[0])):
            result = max(result,calculate(i,j))
    return result

Time Complexity: O(n²), where n is the length of the matrix row.

Space Complexity: O(1) as the solution doesn't take any extra space.

I hope this solution helps!

Mirror Reflection Solution Code

1