LintCode "Submatrix Sum"
Naive solution is O(n^4). But on 1 certain dimension, naive O(n^2) can be O(n) by this well-known equation: sum[i..j] = sum[0..j] - sum[0..i]. And pls take care of several corner cases.
- class Solution {
- public:
- /**
- * @param matrix an integer matrix
- * @return the coordinate of the left-up and right-down number
- */
- vector<vector<int>> submatrixSum(vector<vector<int>>& m) {
- int h = m.size();
- if(!h) return {};
- int w = m[].size();
- if(!w) return {};
- // Get accumulate sum by columns
- vector<vector<int>> cols(h, vector<int>(w, ));
- for(int i = ; i < w; i ++)
- {
- unordered_map<int, int> rec; // sum-inx
- for(int j = ; j < h; j ++)
- {
- if(m[j][i] == )
- {
- return {{i, j},{i, j}};
- }
- cols[j][i] = (j ? cols[j - ][i] : ) + m[j][i];
- if (!cols[j][i])
- {
- return {{, i}, {j, i}};
- }
- else if(rec.find(cols[j][i]) != rec.end())
- {
- return {{rec[cols[j][i]] + , i}, {j, i}};
- }
- rec[cols[j][i]] = j;
- }
- }
- // horizontal case
- for(int i = ; i < h; i ++)
- for(int j = i; j < h; j ++)
- {
- vector<int> hsum(w, );
- for(int x = ; x < w; x ++)
- {
- int prev = ((i == ) ? : cols[i - ][x]);
- hsum[x] = cols[j][x] - prev;
- }
- //
- vector<int> asum(w, );
- unordered_map<int, int> rec; // sum-inx
- for(int x = ; x < w; x ++)
- {
- int nsum = (x ? asum[x - ] : ) + hsum[x];
- if (!nsum)
- {
- return {{i + , }, {j, x}};
- }
- else if(rec.find(nsum) != rec.end())
- {
- return {{i, rec[nsum] + }, {j, x}};
- }
- rec[nsum] = x;
- asum[x] = nsum;
- }
- }
- return {};
- }
- };
LintCode "Submatrix Sum"的更多相关文章
- [LintCode] Submatrix Sum 子矩阵之和
Given an integer matrix, find a submatrix where the sum of numbers is zero. Your code should return ...
- array / matrix subarray/submatrix sum
Maximal Subarray Sum : O(n) scan-and-update dynamic programming, https://en.wikipedia.org/wiki/Maxim ...
- lintcode 中等题:Submatrix sum is 0 和为零的子矩阵
和为零的子矩阵 给定一个整数矩阵,请找出一个子矩阵,使得其数字之和等于0.输出答案时,请返回左上数字和右下数字的坐标. 样例 给定矩阵 [ [1 ,5 ,7], [3 ,7 ,-8], [4 ,-8 ...
- lintcode: k Sum 解题报告
K SUM My Submissions http://www.lintcode.com/en/problem/k-sum/ 题目来自九章算法 13% Accepted Given n distinc ...
- LintCode "4 Sum"
4 Pointer solution. Key: when moving pointers, we skip duplicated ones. Ref: https://github.com/xbz/ ...
- Submatrix Sum
Given an integer matrix, find a submatrix where the sum of numbers is zero. Your code should return ...
- Lintcode: Subarray Sum 解题报告
Subarray Sum 原题链接:http://lintcode.com/zh-cn/problem/subarray-sum/# Given an integer array, find a su ...
- LintCode Subarray Sum
For this problem we need to learn a new trick that if your start sum up all elements in an array. Wh ...
- [LintCode] Two Sum 两数之和
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
随机推荐
- 【转】How-To-Ask-Questions-The-Smart-Way
提问的智慧 How To Ask Questions The Smart Way Copyright © 2001,2006,2014 Eric S. Raymond, Rick Moen 本指南英文 ...
- java项目上各种小问题
md,出现好几次这种问题,还得上百度? 以此为证,再出现这种问题我就不信想不起来怎么解决!!!----清除不存在jar包 ok,第二个问题: 每个类上有无数个红叉,然而代码并没有问题 解决方案:run ...
- DB2中的ROW_NUMBER() OVER()函数用法
ROW_NUMBER() OVER()大概有俩方面的作用 1,分页, 并返回分页结果集.2,是对数据进行处理 分组 db2的分页: select tmp.* from ( SELECT rownu ...
- Codeforces Round #377 (Div. 2) A B C D 水/贪心/贪心/二分
A. Buy a Shovel time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- jq中 load()方法 简介
load()方法会在元素的onload事件中绑定一个处理函数.如果处理函数绑定给window对象,则会在所有内容(包括窗口,框架,对象和图像等)加载完毕后触发,如果处理函数绑定在元素上,则会在元素的内 ...
- html5的程序接口与元素变化
除了原先的DOM接口,HTML5增加了更多API,如:1. 用于即时2D绘图的Canvas标签2. 定时媒体回放3. 离线数据库存储4.文档编辑5. 拖拽控制6. 浏览历史管理元素变化新的解析顺序新的 ...
- POJ 1488 Tex Quotes --- 水题
POJ 1488 题目大意:给定一篇文章,将它的左引号转成 ``(1的左边),右引号转成 ''(两个 ' ) 解题思路:水题,设置一个bool变量标记是左引号还是右引号即可 /* POJ 1488 T ...
- memory CPU cache books
http://www.amazon.com/Consistency-Coherence-Synthesis-Lectures-Architecture/dp/1608455645/ref=pd_sim ...
- 谓词的使用 -ios
#import <Foundation/Foundation.h> @interface Person : NSObject<NSCopying> @property(nona ...
- apk反编译生成程序的源代码和图片、XML配置、语言资源等文件
Android应用的UI越来越漂亮,遇到喜欢的我们可以通过反编译,得到应用的源代码借鉴下别人的思想. 具体步骤: 1.下载 apktool 下载地址:https://code.google.com/p ...