Skip to content

Commit 6f861ca

Browse files
add 1572
1 parent 95c308d commit 6f861ca

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1572|[Matrix Diagonal Sum](https://leetcode.com/problems/matrix-diagonal-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1572.java) ||Array|Easy|
1112
|1567|[Maximum Length of Subarray With Positive Product](https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1567.java) |[:tv:](https://youtu.be/bFer5PdsgpY)|Greedy|Medium|
1213
|1566|[Detect Pattern of Length M Repeated K or More Times](https://leetcode.com/problems/detect-pattern-of-length-m-repeated-k-or-more-times/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1566.java) |[:tv:](https://youtu.be/aJAV_VgmjdE)|Array|Easy|
1314
|1561|[Maximum Number of Coins You Can Get](https://leetcode.com/problems/maximum-number-of-coins-you-can-get/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1561.java) |[:tv:](https://youtu.be/hPe9Z3TiUrA)|Sort|Medium|
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
public class _1572 {
7+
public static class Solution1 {
8+
public int diagonalSum(int[][] mat) {
9+
int m = mat.length;
10+
Set<Integer> added = new HashSet<>();
11+
int sum = 0;
12+
for (int i = 0; i < m; i++) {
13+
for (int j = 0; j < m; j++) {
14+
if (i == j) {
15+
added.add(i * m + j);
16+
sum += mat[i][j];
17+
}
18+
}
19+
}
20+
for (int i = 0; i < m; i++) {
21+
for (int j = m - 1; j >= 0; j--) {
22+
if (i + j == m - 1 && added.add(i * m + j)) {
23+
sum += mat[i][j];
24+
}
25+
}
26+
}
27+
return sum;
28+
}
29+
}
30+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1572;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.TestCase.assertEquals;
8+
9+
public class _1572Test {
10+
private static _1572.Solution1 solution1;
11+
private static int[][] mat;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _1572.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
mat = new int[][]{
21+
{1, 2, 3},
22+
{4, 5, 6},
23+
{7, 8, 9}
24+
};
25+
assertEquals(25, solution1.diagonalSum(mat));
26+
}
27+
28+
@Test
29+
public void test2() {
30+
mat = new int[][]{
31+
{5}
32+
};
33+
assertEquals(5, solution1.diagonalSum(mat));
34+
}
35+
36+
@Test
37+
public void test3() {
38+
mat = new int[][]{
39+
{1, 1, 1, 1},
40+
{1, 1, 1, 1},
41+
{1, 1, 1, 1},
42+
{1, 1, 1, 1},
43+
};
44+
assertEquals(8, solution1.diagonalSum(mat));
45+
}
46+
47+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy