Skip to content

Commit

Permalink
[Array] Add a solution to Range Sum Query 2D - Immutable
Browse files Browse the repository at this point in the history
  • Loading branch information
soapyigu committed Jul 6, 2018
1 parent 82043fd commit caafd3c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
40 changes: 40 additions & 0 deletions Array/NumMatrix.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Question Link: https://leetcode.com/problems/range-sum-query-2d-immutable/
* Primary idea: Prebuild a sum matrix by original as top left point and current as bottom right point
*
* Time Complexity: O(mn) for init(), O(1) for sumRegion(), Space Complexity: O(mn)
*/

class NumMatrix {
fileprivate var sum: [[Int]]

init(_ matrix: [[Int]]) {
let m = matrix.count, n = matrix[0].count
sum = Array(repeating: Array(repeating: 0, count: n), count: m)

for i in 0..<m {
var lineSum = 0
for j in 0..<n {
lineSum += matrix[i][j]

if i == 0 {
sum[i][j] = lineSum
} else {
sum[i][j] = lineSum + sum[i - 1][j]
}
}
}
}

func sumRegion(_ row1: Int, _ col1: Int, _ row2: Int, _ col2: Int) -> Int {
if row1 == 0 && col1 == 0 {
return sum[row2][col2]
} else if row1 == 0 {
return sum[row2][col2] - sum[row2][col1 - 1]
} else if col1 == 0 {
return sum[row2][col2] - sum[row1 - 1][col2]
} else {
return sum[row2][col2] - sum[row2][col1 - 1] - sum[row1 - 1][col2] + sum[row1 - 1][col1 - 1]
}
}
}
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* [Microsoft](#microsoft)

## Progress
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 265 completed solutions. Note: questions with &hearts; mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 266 completed solutions. Note: questions with &hearts; mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).


## Array
Expand All @@ -54,6 +54,7 @@
[3Sum Closest](https://leetcode.com/problems/3sum-closest/)| [Swift](./Array/ThreeSum.swift)| Medium| O(n^2)| O(nC3)|
[4Sum](https://leetcode.com/problems/4sum/)| [Swift](./Array/FourSum.swift)| Medium| O(n^3)| O(nC4)|
[Summary Ranges](https://leetcode.com/problems/summary-ranges/)| [Swift](./Array/SummaryRanges.swift)| Medium| O(n)| O(n)|
[Range Sum Query 2D - Immutable](https://leetcode.com/problems/range-sum-query-2d-immutable/)| [Swift](./Array/NumMatrix.swift)| Medium| O(mn)| O(mn)|
[Asteroid Collision](https://leetcode.com/problems/asteroid-collision/)| [Swift](./Array/AsteroidCollision.swift)| Medium| O(n)| O(n)|
[Maximize Distance to Closest Person](https://leetcode.com/problems/maximize-distance-to-closest-person/)| [Swift](./Array/MaximizeDistanceToClosestPerson.swift)| Easy| O(n)| O(1)|
[Exam Room](https://leetcode.com/problems/exam-room/)| [Swift](./Array/ExamRoom.swift)| Medium| O(n)| O(n)|
Expand Down Expand Up @@ -537,7 +538,7 @@
| | 307 | [Range Sum Query - Mutable](https://leetcode.com/problems/range-sum-query-mutable/) | Medium
| | 306 | [Additive Number](https://leetcode.com/problems/additive-number/) | Medium
| [Swift](./UnionFind/NumberIslandsII.swift) | 305 | [Number of Islands II](https://leetcode.com/problems/number-of-islands-ii/) &hearts; | Hard
| | 304 | [Range Sum Query 2D - Immutable](https://leetcode.com/problems/range-sum-query-2d-immutable/) | Medium
| [Swift](./Array/NumMatrix.swift) | 304 | [Range Sum Query 2D - Immutable](https://leetcode.com/problems/range-sum-query-2d-immutable/) | Medium
| | 303 | [Range Sum Query - Immutable](https://leetcode.com/problems/range-sum-query-immutable/) | Easy
| | 302 | [Smallest Rectangle Enclosing Black Pixels](https://leetcode.com/problems/smallest-rectangle-enclosing-black-pixels/) &hearts; | Hard
| [Swift](./DFS/RemoveInvalidParentheses.swift) | 301 | [Remove Invalid Parentheses](https://leetcode.com/problems/remove-invalid-parentheses/) | Hard
Expand Down

0 comments on commit caafd3c

Please sign in to comment.