Skip to content

Commit 736c262

Browse files
jeremymanninggithub-actions[bot]
authored andcommitted
Auto-solve daily LeetCode problem using GPT-5-mini
1 parent 97863f1 commit 736c262

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

problems/2946/gpt5-mini.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# [Problem 2946: Matrix Similarity After Cyclic Shifts](https://leetcode.com/problems/matrix-similarity-after-cyclic-shifts/description/?envType=daily-question)
2+
3+
## Initial thoughts (stream-of-consciousness)
4+
We are applying the same cyclic shift operation to each row k times: even-indexed rows shift left, odd-indexed rows shift right. Doing the same shift repeatedly is equivalent to shifting by (k mod n) positions where n is the row length. So for each row we only need to check whether that row is invariant under the appropriate net shift. If any row after the net shift differs from the original row, the final matrix differs from the original. Edge-case: when k % n == 0 no change; when all elements equal any shift gives same row. Another thought: we could reason with cycles/GCD, but a simple slice-based rotation comparison is enough given constraints (m,n <= 25).
5+
6+
## Refining the problem, round 2 thoughts
7+
- Let n = number of columns, s = k % n. For even i, net operation is left shift by s; for odd i, net operation is right shift by s (equivalently left shift by n - s).
8+
- If s == 0, all rows unchanged => return True quickly.
9+
- For each row, compute the rotated version using slicing and compare to the original row. If any mismatch => False.
10+
- Time complexity O(m * n) and space O(n) additional per-row for the rotated list (but can also be done in O(1) extra by comparing indices). Given constraints this is fine.
11+
- Edge cases: single-column (n = 1) => always unchanged since s % 1 == 0; all-equal rows; small/large k covered by modulo.
12+
13+
## Attempted solution(s)
14+
```python
15+
from typing import List
16+
17+
class Solution:
18+
def areSimilarAfterShifts(self, mat: List[List[int]], k: int) -> bool:
19+
if not mat:
20+
return True
21+
m = len(mat)
22+
n = len(mat[0])
23+
# effective shift amount
24+
s = k % n
25+
if s == 0:
26+
return True
27+
for i in range(m):
28+
row = mat[i]
29+
if i % 2 == 0:
30+
# even-indexed rows: left shift by s
31+
rotated = row[s:] + row[:s]
32+
else:
33+
# odd-indexed rows: right shift by s
34+
rotated = row[-s:] + row[:-s]
35+
if rotated != row:
36+
return False
37+
return True
38+
```
39+
- Notes:
40+
- Approach: compute effective shift s = k % n. For each row, build its shifted version and compare to original. Even rows use left rotation, odd rows use right rotation.
41+
- Time complexity: O(m * n) where m = number of rows and n = number of columns (building and comparing each row).
42+
- Space complexity: O(n) extra per row for the rotated list (can be lowered by index-wise comparison if desired).

0 commit comments

Comments
 (0)