Skip to content

Commit ccd184f

Browse files
committed
LC 1572. Matrix Diagonal Sum (Rust)
1 parent f149b90 commit ccd184f

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
425425
| [1523. Count Odd Numbers in an Interval Range][lc1523] | 🟢 Easy | [![python](res/py.png)][lc1523py] [![rust](res/rs.png)][lc1523rs] |
426426
| [1539. Kth Missing Positive Number][lc1539] | 🟢 Easy | [![python](res/py.png)][lc1539py] [![rust](res/rs.png)][lc1539rs] |
427427
| [1544. Make The String Great][lc1544] | 🟢 Easy | [![python](res/py.png)][lc1544py] |
428+
| [1572. Matrix Diagonal Sum][lc1572] | 🟢 Easy | [![rust](res/rs.png)][lc1572rs] |
428429
| [1578. Minimum Time to Make Rope Colorful][lc1578] | 🟠 Medium | [![python](res/py.png)][lc1578py] |
429430
| [1579. Remove Max Number of Edges to Keep Graph Fully Traversable][lc1579] | 🔴 Hard | [![python](res/py.png)][lc1579py] |
430431
| [1584. Min Cost to Connect All Points][lc1584] | 🟠 Medium | [![python](res/py.png)][lc1584py] |
@@ -1407,6 +1408,8 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
14071408
[lc1539rs]: leetcode/kth-missing-positive-number.rs
14081409
[lc1544]: https://leetcode.com/problems/make-the-string-great/
14091410
[lc1544py]: leetcode/make-the-string-great.py
1411+
[lc1572]: https://leetcode.com/problems/matrix-diagonal-sum/
1412+
[lc1572rs]: leetcode/matrix-diagonal-sum.rs
14101413
[lc1578]: https://leetcode.com/problems/minimum-time-to-make-rope-colorful/
14111414
[lc1578py]: leetcode/minimum-time-to-make-rope-colorful.py
14121415
[lc1579]: https://leetcode.com/problems/remove-max-number-of-edges-to-keep-graph-fully-traversable/

leetcode/matrix-diagonal-sum.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// 1572. Matrix Diagonal Sum
2+
// 🟢 Easy
3+
//
4+
// https://leetcode.com/problems/matrix-diagonal-sum/
5+
//
6+
// Tags: Array - Matrix
7+
8+
struct Solution;
9+
impl Solution {
10+
/// A cell belongs to a diagonal if it fulfills either of two conditions,
11+
/// r == c => main positive diagonal or r+c == n-1 => main negative
12+
/// diagonal. We iterate over all cells, if it matches, we add its value to
13+
/// the result.
14+
///
15+
/// Time complexity: O(n^2) - We iterate over all cells, for each, we check
16+
/// if it matches the conditions and, if it does, add it to the result.
17+
/// Space complexity: O(1) - We only store integer values and pointers.
18+
///
19+
/// Runtime 0 ms Beats 100%
20+
/// Memory 2.2 MB Beats 85.71%
21+
pub fn diagonal_sum(mat: Vec<Vec<i32>>) -> i32 {
22+
let n = mat.len();
23+
let mut res = 0;
24+
// Iterate over all cells checking if the cell belongs to a diagonal.
25+
for r in 0..n {
26+
for c in 0..n {
27+
if r == c || r + c == n - 1 {
28+
res += mat[r][c];
29+
}
30+
}
31+
}
32+
res
33+
}
34+
}
35+
36+
// Tests.
37+
fn main() {
38+
let tests = [
39+
(vec![vec![1, 2, 3], vec![4, 5, 6], vec![7, 8, 9]], 25),
40+
(
41+
vec![
42+
vec![1, 1, 1, 1],
43+
vec![1, 1, 1, 1],
44+
vec![1, 1, 1, 1],
45+
vec![1, 1, 1, 1],
46+
],
47+
8,
48+
),
49+
];
50+
for t in tests {
51+
assert_eq!(Solution::diagonal_sum(t.0), t.1);
52+
}
53+
println!("\x1b[92m» All tests passed!\x1b[0m")
54+
}

0 commit comments

Comments
 (0)