Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 93d5e9a

Browse files
authored
Create sort matrix by diagonals.py
Signed-off-by: Erjan <erjan@users.noreply.github.com>
1 parent 3a4bd73 commit 93d5e9a

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

‎sort matrix by diagonals.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
You are given an n x n square matrix of integers grid. Return the matrix such that:
2+
3+
The diagonals in the bottom-left triangle (including the middle diagonal) are sorted in non-increasing order.
4+
The diagonals in the top-right triangle are sorted in non-decreasing order.
5+
-------------------------------------------------------------------------------------------------------------------------------------
6+
7+
8+
class Solution:
9+
def sortMatrix(self, grid: List[List[int]]) -> List[List[int]]:
10+
n=len(grid)
11+
# upper right triangle j=i+d
12+
for d in range(n-2, -1, -1):
13+
diag=sorted(grid[i][i+d] for i in range(n-d))
14+
for i, x in enumerate(diag):
15+
grid[i][i+d]=x
16+
# lower left triangle i=j+d
17+
for d in range(n-1):
18+
diag=sorted((grid[j+d][j] for j in range(n-d)), reverse=True)
19+
for j, x in enumerate(diag):
20+
grid[j+d][j]=x
21+
return grid
22+
23+
24+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /