-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sync LeetCode submission Runtime - 14 ms (44.74%), Memory - 19.7 MB (…
…47.58%)
- Loading branch information
Showing
2 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<p>Given an <code>m x n</code> matrix <code>mat</code>, return <em>an array of all the elements of the array in a diagonal order</em>.</p> | ||
|
||
<p> </p> | ||
<p><strong class="example">Example 1:</strong></p> | ||
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/10/diag1-grid.jpg" style="width: 334px; height: 334px;" /> | ||
<pre> | ||
<strong>Input:</strong> mat = [[1,2,3],[4,5,6],[7,8,9]] | ||
<strong>Output:</strong> [1,2,4,7,5,3,6,8,9] | ||
</pre> | ||
|
||
<p><strong class="example">Example 2:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> mat = [[1,2],[3,4]] | ||
<strong>Output:</strong> [1,2,3,4] | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>m == mat.length</code></li> | ||
<li><code>n == mat[i].length</code></li> | ||
<li><code>1 <= m, n <= 10<sup>4</sup></code></li> | ||
<li><code>1 <= m * n <= 10<sup>4</sup></code></li> | ||
<li><code>-10<sup>5</sup> <= mat[i][j] <= 10<sup>5</sup></code></li> | ||
</ul> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Approach 1: Diagonal Iteration and Reversal | ||
|
||
# Time: O(n * m) | ||
# Space: O(min(n, m)) | ||
|
||
class Solution: | ||
def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]: | ||
if not mat or not mat[0]: | ||
return [] | ||
|
||
n, m = len(mat), len(mat[0]) | ||
|
||
result, intermediate = [], [] | ||
|
||
for d in range(n + m - 1): | ||
intermediate.clear() | ||
|
||
# We need to figure out the "head" of this diagonal | ||
# The elements in the first row and the last column | ||
# are the respective heads. | ||
r, c = 0 if d < m else d - m + 1, d if d < m else m - 1 | ||
|
||
# Iterate until one of the indices goes out of scope | ||
# Take note of the index math to go down the diagonal | ||
while r < n and c > -1: | ||
intermediate.append(mat[r][c]) | ||
r += 1 | ||
c -= 1 | ||
|
||
# Reverse even numbered diagonals. The | ||
# article says we have to reverse odd | ||
# numbered articles but here, the numbering | ||
# is starting from 0 :P | ||
if d % 2 == 0: | ||
result.extend(intermediate[::-1]) | ||
else: | ||
result.extend(intermediate) | ||
|
||
return result | ||
|