Skip to content

Commit a4647dc

Browse files
committed
080523
1 parent 078bdfc commit a4647dc

File tree

4 files changed

+163
-1
lines changed

4 files changed

+163
-1
lines changed

src/Main.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import leetcode.editor.en.Q188.BestTimeToBuyAndSellStockIv;
66
import leetcode.editor.en.Q1964.FindTheLongestValidObstacleCourseAtEachPosition;
77
import leetcode.editor.en.Q2466.CountWaysToBuildGoodStrings;
8+
import leetcode.editor.en.Q311.SparseMatrixMultiplication;
89
import leetcode.editor.en.Q354.RussianDollEnvelopes;
910
import leetcode.editor.en.Q377.CombinationSumIv;
1011
import leetcode.editor.en.Q474.OnesAndZeroes;
@@ -24,7 +25,7 @@
2425

2526
public class Main {
2627
public static void main(String[] args) throws IOException {
27-
System.out.println(new DecodeWays().numDecodings("230"));
28+
System.out.println(Arrays.deepToString(new SparseMatrixMultiplication().multiply(toIntMatrix("[[1,-5]]"), toIntMatrix("[[12],[-1]]"))));
2829

2930
}
3031

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package leetcode.editor.en.Q1572;
2+
3+
import java.util.*;
4+
5+
import javafx.util.Pair;
6+
7+
//Given a square matrix mat, return the sum of the matrix diagonals.
8+
//
9+
// Only include the sum of all the elements on the primary diagonal and all the
10+
//elements on the secondary diagonal that are not part of the primary diagonal.
11+
//
12+
//
13+
// Example 1:
14+
//
15+
//
16+
//Input: mat = [[1,2,3],
17+
//  [4,5,6],
18+
//  [7,8,9]]
19+
//Output: 25
20+
//Explanation: Diagonals sum: 1 + 5 + 9 + 3 + 7 = 25
21+
//Notice that element mat[1][1] = 5 is counted only once.
22+
//
23+
//
24+
// Example 2:
25+
//
26+
//
27+
//Input: mat = [[1,1,1,1],
28+
//  [1,1,1,1],
29+
//  [1,1,1,1],
30+
//  [1,1,1,1]]
31+
//Output: 8
32+
//
33+
//
34+
// Example 3:
35+
//
36+
//
37+
//Input: mat = [[5]]
38+
//Output: 5
39+
//
40+
//
41+
//
42+
// Constraints:
43+
//
44+
//
45+
// n == mat.length == mat[i].length
46+
// 1 <= n <= 100
47+
// 1 <= mat[i][j] <= 100
48+
//
49+
//
50+
// 👍 2392 👎 31
51+
52+
53+
//leetcode submit region begin(Prohibit modification and deletion)
54+
class Solution {
55+
public int diagonalSum(int[][] mat) {
56+
boolean[][] visited = new boolean[mat.length][mat[0].length];
57+
return sum(mat, visited, 0, 0, 1, 1) + sum(mat, visited, 0, mat[0].length - 1, 1, -1);
58+
}
59+
60+
private int sum(int[][] mat, boolean[][] visited, int row, int col, int rSum, int cSum) {
61+
if (!isValidIdx(row, col, mat)) return 0;
62+
63+
if (visited[row][col]) {
64+
return sum(mat, visited, row + rSum, col + cSum, rSum, cSum);
65+
}
66+
67+
visited[row][col] = true;
68+
return mat[row][col] + sum(mat, visited, row + rSum, col + cSum, rSum, cSum);
69+
}
70+
71+
private boolean isValidIdx(int row, int col, int[][] grid) {
72+
return row >= 0 && row < grid.length && col >= 0 && col < grid[row].length;
73+
}
74+
}
75+
//leetcode submit region end(Prohibit modification and deletion)
76+
77+
78+
public class MatrixDiagonalSum extends Solution {
79+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package leetcode.editor.en.Q256;
2+
3+
import java.util.*;
4+
5+
import javafx.util.Pair;
6+
7+
//leetcode submit region begin(Prohibit modification and deletion)
8+
class Solution {
9+
Integer[][] cache;
10+
11+
public int minCost(int[][] costs) {
12+
cache = new Integer[costs.length][4];
13+
return minCost(0, 3, costs);
14+
}
15+
16+
public int minCost(int i, int cantBe, int[][] costs) {
17+
if (i == costs.length) return 0;
18+
int red = costs[i][0];
19+
int blue = costs[i][1];
20+
int green = costs[i][2];
21+
22+
int ans = 0;
23+
if (cache[i][cantBe] != null) {
24+
return cache[i][cantBe];
25+
}
26+
27+
int useRed = red + minCost(i + 1, 0, costs);
28+
int useBlue = blue + minCost(i + 1, 1, costs);
29+
int useGreen = green + minCost(i + 1, 2, costs);
30+
31+
switch (cantBe) {
32+
case 0 -> {
33+
ans = Math.min(useGreen, useBlue);
34+
}
35+
case 1 -> {
36+
ans = Math.min(useRed, useGreen);
37+
}
38+
case 2 -> {
39+
ans = Math.min(useRed, useBlue);
40+
}
41+
default -> {
42+
ans = Math.min(useRed, useBlue);
43+
ans = Math.min(ans, useGreen);
44+
}
45+
}
46+
cache[i][cantBe] = ans;
47+
48+
return ans;
49+
50+
}
51+
}
52+
//leetcode submit region end(Prohibit modification and deletion)
53+
54+
55+
public class PaintHouse extends Solution {
56+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package leetcode.editor.en.Q311;
2+
3+
import java.util.*;
4+
5+
import javafx.util.Pair;
6+
7+
//leetcode submit region begin(Prohibit modification and deletion)
8+
class Solution {
9+
public int[][] multiply(int[][] mat1, int[][] mat2) {
10+
int[][] ans = new int[mat1.length][mat2[0].length];
11+
for (int i = 0; i < ans.length; i++) {
12+
for (int j = 0; j < ans[i].length; j++) {
13+
for (int k = 0; k < mat1[0].length; k++) {
14+
ans[i][j] += (mat1[i][k] * mat2[k][j]);
15+
}
16+
}
17+
}
18+
return ans;
19+
20+
}
21+
}
22+
//leetcode submit region end(Prohibit modification and deletion)
23+
24+
25+
public class SparseMatrixMultiplication extends Solution {
26+
}

0 commit comments

Comments
 (0)