Skip to content

Commit 1e52f38

Browse files
committed
[Math] add a solution to Sparse Matrix Multiplication
1 parent c60bfb5 commit 1e52f38

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

Math/SparseMatrixMultiplication.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/sparse-matrix-multiplication/
3+
* Primary idea: Use a matrix to mark indices of an matrix where is not zero to optimize the multiplication
4+
*
5+
* Time Complexity: O(n^3), Space Complexity: O(n^2)
6+
*/
7+
8+
class SparseMatrixMultiplication {
9+
func multiply(_ A: [[Int]], _ B: [[Int]]) -> [[Int]] {
10+
let l = A.count, m = B.count, n = B[0].count
11+
12+
var res = Array(repeating: Array(repeating: 0, count: n), count: l)
13+
14+
var nonZeroB = Array(repeating: [Int](), count: m)
15+
for i in 0..<m {
16+
for j in 0..<n {
17+
if B[i][j] != 0 {
18+
nonZeroB[i].append(j)
19+
}
20+
}
21+
}
22+
23+
for i in 0..<l {
24+
for j in 0..<m {
25+
if A[i][j] == 0 {
26+
continue
27+
}
28+
29+
for k in nonZeroB[j] {
30+
res[i][k] += A[i][j] * B[j][k]
31+
}
32+
}
33+
}
34+
35+
return res
36+
}
37+
}

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
![Leetcode](./logo.png?style=centerme)
55

66
## Progress
7-
[Problem Status](#problem-status) shows the latest progress to all 1000+ questions. Currently we have 318 completed solutions. Note: questions with &hearts; mark means that you have to **Subscript to premium membership** of LeetCode to unlock them.
7+
[Problem Status](#problem-status) shows the latest progress to all 1000+ questions. Currently we have 319 completed solutions. Note: questions with &hearts; mark means that you have to **Subscript to premium membership** of LeetCode to unlock them.
88

99
## Contributors
1010

@@ -332,6 +332,7 @@
332332
[Integer to Roman](https://leetcode.com/problems/integer-to-roman/)| [Swift](./Math/IntegerToRoman.swift)| Medium| O(n)| O(1)|
333333
[Roman to Integer](https://leetcode.com/problems/roman-to-integer/)| [Swift](./Math/RomanToInteger.swift)| Easy| O(n)| O(n)|
334334
[Integer to English Words](https://leetcode.com/problems/integer-to-english-words/)| [Swift](./Math/IntegerEnglishWords.swift)| Hard| O(n)| O(1)|
335+
[Sparse Matrix Multiplication](https://leetcode.com/problems/sparse-matrix-multiplication/)| [Swift](./Math/SparseMatrixMultiplication.swift)| Medium| O(n^3)| O(n^2)|
335336
[Rectangle Area](https://leetcode.com/problems/rectangle-area/)| [Swift](./Math/RectangleArea.swift)| Easy| O(1)| O(1)|
336337
[Minimum Moves to Equal Array Elements](https://leetcode.com/problems/minimum-moves-to-equal-array-elements/)| [Swift](./Math/MinimumMovesEqualArrayElements.swift)| Easy| O(n)| O(1)|
337338
[Trapping Rain Water](https://leetcode.com/problems/trapping-rain-water/)| [Swift](./Math/TrappingRainWater.swift)| Hard| O(n)| O(n)|
@@ -602,7 +603,7 @@
602603
| [Swift](./Tree/BinaryTreeVerticalOrderTraversal.swift) | 314 | [Binary Tree Vertical Order Traversal](https://leetcode.com/problems/binary-tree-vertical-order-traversal/) &hearts; | Medium
603604
| [Swift](./Math/SuperUglyNumber.swift) | 313 | [Super Ugly Number](https://leetcode.com/problems/super-ugly-number/) | Medium
604605
| [Swift](./DP/GuessNumberHigherOrLowerII.swift) | 312 | [Burst Balloons](https://leetcode.com/problems/burst-balloons/) | Hard
605-
| | 311 | [Sparse Matrix Multiplication](https://leetcode.com/problems/sparse-matrix-multiplication/) &hearts; | Medium
606+
| [Swift](./Math/SparseMatrixMultiplication.swift) | 311 | [Sparse Matrix Multiplication](https://leetcode.com/problems/sparse-matrix-multiplication/) &hearts; | Medium
606607
| | 310 | [Minimum Height Trees](https://leetcode.com/problems/minimum-height-trees/) | Medium
607608
| [Swift](./DP/BestTimeBuySellStockCooldown.swift) | 309 | [Best Time to Buy and Sell Stock with Cooldown](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/) | Medium
608609
| | 308 | [Range Sum Query 2D - Mutable](https://leetcode.com/problems/range-sum-query-2d-mutable/) &hearts; | Hard

0 commit comments

Comments
 (0)