-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1886.py
More file actions
48 lines (34 loc) · 1.44 KB
/
1886.py
File metadata and controls
48 lines (34 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""1886. Determine Whether Matrix Can Be Obtained By Rotation
Given two n x n binary matrices mat and target, return true if it is possible to make mat equal to target by rotating mat in 90-degree increments, or false otherwise.
Example 1:
Input: mat = [[0,1],[1,0]], target = [[1,0],[0,1]]
Output: true
Explanation: We can rotate mat 90 degrees clockwise to make mat equal target.
Example 2:
Input: mat = [[0,1],[1,1]], target = [[1,0],[0,1]]
Output: false
Explanation: It is impossible to make mat equal to target by rotating mat.
Example 3:
Input: mat = [[0,0,0],[0,1,0],[1,1,1]], target = [[1,1,1],[0,1,0],[0,0,0]]
Output: true
Explanation: We can rotate mat 90 degrees clockwise two times to make mat equal target."""
from typing import List
class Solution:
def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:
for _ in range(4):
if mat == target:
return True
# rotate 90° clockwise
mat = [list(row) for row in zip(*mat[::-1])]
return False
# Test locally
if __name__ == "__main__":
sol = Solution()
tests = [
([[0,1],[1,0]], [[1,0],[0,1]], True),
([[0,1],[1,1]], [[1,0],[0,1]], False),
([[0,0,0],[0,1,0],[1,1,1]], [[1,1,1],[0,1,0],[0,0,0]], True),
]
for i, (mat, target, expected) in enumerate(tests, 1):
result = sol.findRotation(mat, target)
print(f"Test {i}: {result} (Expected: {expected})")