1074:元素和为目标值的子矩阵数量

This commit is contained in:
huangge1199@hotmail.com 2021-05-29 22:29:41 +08:00
parent e755910965
commit 510947c8ec
5 changed files with 173 additions and 2 deletions

View File

@ -0,0 +1,93 @@
//给出矩阵 matrix 和目标值 target返回元素总和等于目标值的非空子矩阵的数量
//
// 子矩阵 x1, y1, x2, y2 是满足 x1 <= x <= x2 y1 <= y <= y2 的所有单元 matrix[x][y] 的集合
//
//
// 如果 (x1, y1, x2, y2) (x1', y1', x2', y2') 两个子矩阵中部分坐标不同x1 != x1'那么这两个子矩阵
//也不同
//
//
//
// 示例 1
//
//
//
//
//输入matrix = [[0,1,0],[1,1,1],[0,1,0]], target = 0
//输出4
//解释四个只含 0 1x1 子矩阵
//
//
// 示例 2
//
//
//输入matrix = [[1,-1],[-1,1]], target = 0
//输出5
//解释两个 1x2 子矩阵加上两个 2x1 子矩阵再加上一个 2x2 子矩阵
//
//
// 示例 3
//
//
//输入matrix = [[904]], target = 0
//输出0
//
//
//
//
// 提示
//
//
// 1 <= matrix.length <= 100
// 1 <= matrix[0].length <= 100
// -1000 <= matrix[i] <= 1000
// -10^8 <= target <= 10^8
//
// Related Topics 数组 动态规划 Sliding Window
// 👍 117 👎 0
package leetcode.editor.cn;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//1074:元素和为目标值的子矩阵数量
class NumberOfSubmatricesThatSumToTarget {
public static void main(String[] args) {
//测试代码
Solution solution = new NumberOfSubmatricesThatSumToTarget().new Solution();
System.out.println(solution.numSubmatrixSumTarget(new int[][]{{0,1,0}, {1,1,1},{0,1,0}}, 0));
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int numSubmatrixSumTarget(int[][] matrix, int target) {
int xLength = matrix.length;
int yLength = matrix[0].length;
int[][] sum = new int[xLength + 1][yLength + 1];
for (int i = 1; i <= xLength; ++i) {
for (int j = 1; j <= yLength; ++j) {
sum[i][j] = matrix[i - 1][j - 1] + sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1];
}
}
int count = 0;
for (int i = 1; i <= xLength; ++i) {
for (int j = 1; j <= yLength; ++j) {
for (int k = 1; k <= i; ++k) {
for (int l = 1; l <= j; ++l) {
if (sum[i][j] - sum[k - 1][j] - sum[i][l - 1] + sum[k - 1][l - 1] == target){
++count;
}
}
}
}
}
return count;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,44 @@
<p>给出矩阵 <code>matrix</code> 和目标值 <code>target</code>,返回元素总和等于目标值的非空子矩阵的数量。</p>
<p>子矩阵 <code>x1, y1, x2, y2</code> 是满足 <code>x1 <= x <= x2</code> 且 <code>y1 <= y <= y2</code> 的所有单元 <code>matrix[x][y]</code> 的集合。</p>
<p>如果 <code>(x1, y1, x2, y2)</code> 和 <code>(x1', y1', x2', y2')</code> 两个子矩阵中部分坐标不同(如:<code>x1 != x1'</code>),那么这两个子矩阵也不同。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2020/09/02/mate1.jpg" style="width: 242px; height: 242px;" /></p>
<pre>
<strong>输入:</strong>matrix = [[0,1,0],[1,1,1],[0,1,0]], target = 0
<strong>输出:</strong>4
<strong>解释:</strong>四个只含 0 的 1x1 子矩阵。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>matrix = [[1,-1],[-1,1]], target = 0
<strong>输出:</strong>5
<strong>解释:</strong>两个 1x2 子矩阵,加上两个 2x1 子矩阵,再加上一个 2x2 子矩阵。
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>matrix = [[904]], target = 0
<strong>输出:</strong>0
</pre>
<p> </p>
<p><strong><strong>提示:</strong></strong></p>
<ul>
<li><code>1 <= matrix.length <= 100</code></li>
<li><code>1 <= matrix[0].length <= 100</code></li>
<li><code>-1000 <= matrix[i] <= 1000</code></li>
<li><code>-10^8 <= target <= 10^8</code></li>
</ul>
<div><div>Related Topics</div><div><li>数组</li><li>动态规划</li><li>Sliding Window</li></div></div>\n<div><li>👍 116</li><li>👎 0</li></div>

View File

@ -0,0 +1,34 @@
class Solution {
public int numSubmatrixSumTarget(int[][] matrix, int target) {
int xLength = matrix.length;
int yLength = matrix[0].length;
int[][] sum = new int[xLength + 1][yLength + 1];
Map<Integer, Integer> map = new HashMap<>();
int count = 0;
for (int i = 1; i < xLength + 1; i++) {
for (int j = 1; j < yLength + 1; j++) {
sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1];
if (matrix[i - 1][j - 1] == target) {
count++;
}
map.put(sum[i][j], map.getOrDefault(sum[i][j], 0));
}
}
for (int key : map.keySet()) {
int value = map.get(key);
if (key == 2 * target && value > 1) {
count += value * (value - 1) / 2;
} else if (key > target && map.containsKey(key - target)) {
count += value * map.get(key - target);
}
}
return count;
}
}
//total_testcases:40
//total_correct:3
//input_formatted:[[1,-1],[-1,1]]
0
//expected_output:5
//code_output:0

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long