Skip to content

Commit 53816ae

Browse files
committed
D. J.:
- Added the leetcode problem and solution for1329
1 parent 8953bee commit 53816ae

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@
281281
- [1143 Longest Common Subsequence](https://leetcode.com/problems/longest-common-subsequence/description/)
282282
- [1218 Longest Arithmetic Subsequence of Given Difference](https://leetcode.com/problems/longest-arithmetic-subsequence-of-given-difference/description/)
283283
- [1312 Minimum Insertion Steps to Make a String Palindrome](https://leetcode.com/problems/minimum-insertion-steps-to-make-a-string-palindrome/description/)
284+
- [1329 Sort the Matrix Diagonally](https://leetcode.com/problems/sort-the-matrix-diagonally/description/)
284285
- [1456 Maximum Number of Vowels in a Substring of Given Length](https://leetcode.com/problems/maximum-number-of-vowels-in-a-substring-of-given-length/description/)
285286
- [1534 Count Good Triplets](https://leetcode.com/problems/count-good-triplets/description/)
286287
- [1679 Max Number of K-Sum Pairs](https://leetcode.com/problems/max-number-of-k-sum-pairs/description/)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
"""Base class for all LeetCode Problems."""
6+
7+
def diagonalSort(self, mat: List[List[int]]) -> List[List[int]]:
8+
"""
9+
A matrix diagonal is a diagonal line of cells starting from some cell in either
10+
the topmost row or leftmost column and going in the bottom-right direction until
11+
reaching the matrix's end. For example, the matrix diagonal starting from
12+
mat[2][0], where mat is a 6 x 3 matrix, includes cells mat[2][0], mat[3][1],
13+
and mat[4][2].
14+
15+
Given an m x n matrix mat of integers, sort each matrix diagonal in ascending
16+
order and return the resulting matrix.
17+
"""
18+
# Collect diagonals
19+
diag = {}
20+
for i in range(len(mat)):
21+
for j in range(len(mat[0])):
22+
key = i - j
23+
if key not in diag:
24+
diag[key] = []
25+
diag[key].append(mat[i][j])
26+
27+
# Sort in decreasing / increasing order for each diagonal
28+
diag = dict(
29+
zip(
30+
diag.keys(),
31+
map(lambda key: sorted(diag[key]), diag.keys()),
32+
strict=False,
33+
)
34+
)
35+
36+
# Apply the results to the result matrix
37+
result = [[0 for _ in range(len(mat[0]))] for _ in range(len(mat))]
38+
for i in range(len(mat)):
39+
for j in range(len(mat[0])):
40+
key = i - j
41+
result[i][j] = diag[key].pop(0)
42+
43+
return result
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from typing import List
2+
3+
import pytest
4+
5+
from awesome_python_leetcode._1329_sort_the_matrix_diagonally import Solution
6+
7+
8+
@pytest.mark.parametrize(
9+
argnames=["mat", "expected"],
10+
argvalues=[
11+
(
12+
[[3, 3, 1, 1], [2, 2, 1, 2], [1, 1, 1, 2]],
13+
[[1, 1, 1, 1], [1, 2, 2, 2], [1, 2, 3, 3]],
14+
),
15+
(
16+
[
17+
[11, 25, 66, 1, 69, 7],
18+
[23, 55, 17, 45, 15, 52],
19+
[75, 31, 36, 44, 58, 8],
20+
[22, 27, 33, 25, 68, 4],
21+
[84, 28, 14, 11, 5, 50],
22+
],
23+
[
24+
[5, 17, 4, 1, 52, 7],
25+
[11, 11, 25, 45, 8, 69],
26+
[14, 23, 25, 44, 58, 15],
27+
[22, 27, 31, 36, 50, 66],
28+
[84, 28, 75, 33, 55, 68],
29+
],
30+
),
31+
],
32+
)
33+
def test_func(mat: List[List[int]], expected: List[List[int]]):
34+
"""Tests the solution of a LeetCode problem."""
35+
sorted_mat = Solution().diagonalSort(mat)
36+
assert sorted_mat == expected

0 commit comments

Comments
 (0)