【leetcode】1074. Number of Submatrices That Sum to Target
题目如下:
Given a
matrix, and atarget, return the number of non-empty submatrices that sum to target.A submatrix
x1, y1, x2, y2is the set of all cellsmatrix[x][y]withx1 <= x <= x2andy1 <= y <= y2.Two submatrices
(x1, y1, x2, y2)and(x1', y1', x2', y2')are different if they have some coordinate that is different: for example, ifx1 != x1'.Example 1:
Input: matrix = [[0,1,0],[1,1,1],[0,1,0]], target = 0
Output: 4
Explanation: The four 1x1 submatrices that only contain 0.Example 2:
Input: matrix = [[1,-1],[-1,1]], target = 0
Output: 5
Explanation: The two 1x2 submatrices, plus the two 2x1 submatrices, plus the 2x2 submatrix.Note:
1 <= matrix.length <= 3001 <= matrix[0].length <= 300-1000 <= matrix[i] <= 1000-10^8 <= target <= 10^8
解题思路:暴力计算的方法时间复杂度是O(n^4),而matrix.length最大值是300,应该无法被AC。那O(n^3)可以吗?试试吧。首先引入一个二维数组val_grid , 记var_grid[m][n] 为从0开始到第m行这个区间内第n列的值的和,例如用例1中的输入[[0,1,0],[1,1,1],[0,1,0]],其对应的val_grid为[[0, 1, 0], [1, 2, 1], [1, 3, 1]] 。如果要计算matrix中第j行~第i行区间内有多少满足和等于target的子矩阵,很轻松就可以求出这个子矩阵每一列的和,例如第k的列的和为 val_grid[i][k] - val_grid[j-1][k] (j>0),接下来令k in range(0,len(matrix[0]),依次判断[j~i]每一列的和是否等于target,同时累加并记录从0开始的列和,假设[0~k]累计的列和是sum,只要判断历史的列和中有多少个满足 sum - target,即可求出[j~i]子矩阵里面以k列为右边列的满足条件的子矩阵个数。这样即可将复杂度优化成O(n^3)。
代码如下:
class Solution(object):
def numSubmatrixSumTarget(self, matrix, target):
"""
:type matrix: List[List[int]]
:type target: int
:rtype: int
"""
val_grid = [[0 for i in range(len(matrix[0]))] for j in range(len(matrix))]
for j in range(len(matrix[0])):
amount = 0
for i in range(len(matrix)):
amount += matrix[i][j]
val_grid[i][j] = amount
#print val_grid res = 0
for i in range(len(matrix)):
for j in range(i+1):
dic = {}
amount = 0
for k in range(len(matrix[i])):
v = val_grid[i][k]
if j > 0:
v -= val_grid[j-1][k]
amount += v
if amount == target:res += 1
if amount - target in dic:
res += dic[amount - target]
dic[amount] = dic.setdefault(amount,0) + 1
return res
【leetcode】1074. Number of Submatrices That Sum to Target的更多相关文章
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)
[LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)
[LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...
- 【leetcode】698. Partition to K Equal Sum Subsets
题目如下: 解题思路:本题是[leetcode]473. Matchsticks to Square的姊妹篇,唯一的区别是[leetcode]473. Matchsticks to Square指定了 ...
- 【LeetCode】Single Number I & II & III
Single Number I : Given an array of integers, every element appears twice except for one. Find that ...
- 【LeetCode】698. Partition to K Equal Sum Subsets 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- 【LeetCode】476. Number Complement (java实现)
原题链接 https://leetcode.com/problems/number-complement/ 原题 Given a positive integer, output its comple ...
- 【LeetCode】191. Number of 1 Bits 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 右移32次 计算末尾的1的个数 转成二进制统计1的个 ...
- 【LeetCode】1128. Number of Equivalent Domino Pairs 等价多米诺骨牌对的数量(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典统计 代码 复杂度分析 日期 题目地址:http ...
随机推荐
- Python Module_openpyxl_处理Excel表格
目录 目录 前言 软件系统 Install openpyxl module Sample code load_workbook加载Excel文件 wbObjectget_sheet_names 获取E ...
- 将html转化为canvas图片(清晰度高)的方法
var copyDom = document.querySelector('.fenxiang1'); var width = copyDom.offsetWidth;//dom宽 var heigh ...
- CSS3——提示工具 图片廓 图像透明 图像拼接技术 媒体类型 属性选择器
提示工具 提示框在鼠标移动到特定的元素上显示 设置提示框的位置 给提示框添加箭头 提示框的淡入效果 提示框美化 图片廓 响应式图片廓 图像透明 创建透明图像——悬停效果 ———鼠标放置后———> ...
- CSS3——背景 文本 字体 链接 列表样式 表格
背景 background-color rgb(255,0,0,1) 最后一个值表示透明度,范围是 0--1 background-image 默认平铺重复显示 background-rep ...
- 25. Reverse Nodes in k-Group[H]k个一组翻转链表
题目 Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. ...
- 【VS开发】C++调用外部程序
关于三个SDK函数:WinExec, ShellExecute,CreateProcess的其他注意事项:[1]定义头文件必须定义以下两个头文件: [cpp] view plain copy #inc ...
- maven指定本地jar包
来自 https://blog.csdn.net/zhengxiangwen/article/details/50734565 一.怎么添加jar到本地仓库呢?步骤:1.cmd命令进入该jar包所在路 ...
- vue 还原Data里面的数据
this.$data包含现有的data数据, this.$options.data()中是原有的data数据 还原代码 Object.assign(this.$data.searchForm, thi ...
- Linux如何设置在当前目录下打开终端
转:https://blog.csdn.net/iot_flower/article/details/71189816 1. sudo apt-get install nautilus-open-te ...
- 各类最新Asp .Net Core 项目和示例源码
1.网站地址:http://www.freeboygirl.com2.网站Asp .Net Core 资料http://www.freeboygirl.com/blog/tag/asp%20net%2 ...