-
Notifications
You must be signed in to change notification settings - Fork 21
/
Submatrix Sum.java
executable file
·81 lines (66 loc) · 2.41 KB
/
Submatrix Sum.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
M
1529974964
tags: Array, PreSum, Hash Table
给一个int[][] matrix, 找一个sub matrix, where the sum == 0.
#### PreSum的思想
- 算出一个右下角点(i,j)到(0,0)的大小: 上一块 + 左一块 + curr node - overlap area
- preSum[i][j]: sum from (0,0) to (i-1,j-1)
- same approach as `subarray sum`: use hashmap to store diff->index; if diff re-appears, that means sum of 0 has occurred
- sequence of calculation: 1. iterate over start row. 2. iterate over end row. 3. iterate over col number (this is where hashmap is stored based on)
- the iteration over col is like a screening: find previous sum and determine result
- Note: 其实并没有真的去找 `== 0` 的解答,而是根据特性来判断 `剩下的/后来加上的一定是0`
```
/*
Given an integer matrix, find a submatrix where the sum of numbers is zero. Your code should return the coordinate of the left-up and right-down number.
Example:
[
[1 ,5 ,7],
[3 ,7 ,-8],
[4 ,-8 ,9],
]
return [(1,1), (2,2)]
Challenge
O(n^3) time.
*/
class Solution {
public int[][] submatrixSum(int[][] nums) {
int[][] rst = new int[2][2];
// check input
if (validateInput(nums)) {
return rst;
}
int m = nums.length, n = nums[0].length;
// calculate presum
int[][] preSum = new int[m + 1][n + 1];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
preSum[i+1][j+1] = preSum[i][j+1] + preSum[i+1][j] + nums[i][j] - preSum[i][j];
}
}
// iterations
for (int start = 0; start < m; start++) {
for (int end = start + 1; end <= m; end++) {
Map<Integer, Integer> map = new HashMap<>();
for (int col = 0; col <= n; col++) {
int diff = preSum[end][col] - preSum[start][col];
if (map.containsKey(diff)) {
rst[0][0] = start;
rst[0][1] = map.get(diff);
rst[1][0] = end - 1;
rst[1][1] = col - 1;
return rst;
}
map.put(diff, col);
}
}
}
return rst;
}
private boolean validateInput(int[][] nums) {
if (nums == null || nums.length == 0 || nums[0] == null || nums[0].length == 0) {
return true;
}
return false;
}
}
```