Interview How to Count Squares
火柴拼出多少个正方形 http://matchstickpuzzles.blogspot.com/2011/06/55-4x4-square-how-many-squares.html
输入是两个二维数组ver 和 hor, 若是有火柴就是1, 没有就是0.
dpHor 表示横方向上有多少连续火柴,dpVer表示纵方向上有多少连续火柴。
最后以左上角第一根横着的火柴为根基检查是否能组成正方形。
Time Complexity: O(n^3). Space: O(n^2).
import java.util.*;
public class countSquare{
public static void main(String [] args){
int [][] hor = {{1,1},{1,0},{1,1}};
int [][] ver = {{1,1,1},{1,1,1}};
System.out.println("Number of square: " + countSquare(hor,ver));
} private static int countSquare(int [][] hor, int [][] ver){
if(hor == null || ver == null || hor.length == 0 || ver.length == 0 || hor[0].length == 0 || ver[0].length == 0){
return 0;
} int [][] dpHor = new int[hor.length][hor[0].length];
int [][] dpVer = new int[ver.length][ver[0].length]; for(int i = 0; i<hor.length; i++){
for(int j = 0; j<hor[0].length; j++){
if(hor[i][j] == 1){
dpHor[i][j] = j == 0 ? 1 : dpHor[i][j-1] + 1;
}else{
dpHor[i][j] = 0;
}
}
} for(int j = 0; j<ver[0].length; j++){
for(int i = 0; i<ver.length; i++){
if(ver[i][j] == 1){
dpVer[i][j] = i == 0 ? 1 : dpVer[i-1][j] + 1;
}else{
dpVer[i][j] = 0;
}
}
} System.out.println("dpHor is " + Arrays.deepToString(dpHor));
System.out.println("dpVer is " + Arrays.deepToString(dpVer)); int res = 0;
for(int i = 0; i<hor.length; i++){
for(int j = 0; j<hor[0].length; j++){
for(int len = 1; len<= Math.min(ver.length-i, hor[0].length-j); len++){
if(dpHor[i][j+len-1] >= len && dpHor[i+len][j+len-1] >= len && dpVer[i+len-1][j] >= len && dpVer[i+len-1][j+len] >= len){
res++;
System.out.println("i = " + i + ", j = " + j + ", len = " + len + ", res = " + res);
}
}
}
}
return res;
}
}
Interview How to Count Squares的更多相关文章
- 【CS Round #44 (Div. 2 only) D】Count Squares
[链接]点击打开链接 [题意] 给你一个0..n和0..m的区域. 你可以选定其中的4个点,然后组成一个正方形. 问你可以圈出多少个正方形. (正方形的边不一定和坐标轴平行) [题解] 首先,考虑只和 ...
- C Primer Plus(第五版)5
第5章 运算符,表达式和语句 5.1 循环简单 程序清单 5.1 显示了一个示例程序,该程序做了一点算术运算来计算穿 9 码鞋的脚用英寸表示的长度.为了增加你对循环的理解,程序的第一版演示了不使用循环 ...
- nodejs api 中文文档
文档首页 英文版文档 本作品采用知识共享署名-非商业性使用 3.0 未本地化版本许可协议进行许可. Node.js v0.10.18 手册 & 文档 索引 | 在单一页面中浏览 | JSON格 ...
- LeetCode Top Interview Questions
LeetCode Top Interview Questions https://leetcode.com/problemset/top-interview-questions/ # No. Titl ...
- [LeetCode] Word Squares 单词平方
Given a set of words (without duplicates), find all word squares you can build from them. A sequence ...
- [LeetCode] Count Primes 质数的个数
Description: Count the number of prime numbers less than a non-negative number, n click to show more ...
- [LintCode] Count and Say 计数和读法
The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221 ...
- HDU 1264 Counting Squares(线段树求面积的并)
Counting Squares Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- [leetcode] Count Primes
Count Primes Description: Count the number of prime numbers less than a non-negative number, n click ...
随机推荐
- Mysql 学习
一.ubuntu下mysql的安装: 通过sudo apt-get install mysql-server 完成: 然后可以通过/etc/init.d/mysql 进行start/stop/rest ...
- MyEclipse10整合Axis2插件
1.下载axis2的eclipse插件 2.把下载好的两个插件包解压后放置myeclipse10安装目录下的dropins文件夹中 3.重启MyEclipse10后 File->New-> ...
- [转]OBOUT ASP.NET HTML Editor - Insert HTML
本文转自:http://www.obout.com/editor_new/sample_InsertHTML.aspx Example demonstrates how to access HTML ...
- elFinder的使用并整合ckeditor
具体实现访问: 白乔大神资源:https://github.com/bluejoe2008/elfinder-2.x-servlet 本人oschina的库:https://git.oschina.n ...
- ST_SRID
定义 ST_SRID 以 ST_Geometry 对象作为输入参数,返回其空间参考 ID. 语法 sde.st_srid (g1 sde.st_geometry) 返回类型 整型 示例 创建下列表格: ...
- FZU 2124 bfs+vis记录
第一次团队训练赛的题 自己看完题没看到不能用舌头吃道具..以为是什么贪心混合bfs..果断放弃..悄悄的背锅了 然后其实比较简单 只是利用vis记录的时候要分两种状态记录 有没有道具 每到一个地方 就 ...
- html5 data
对于html5 的data使用 <div id='testDiv' data-value='123' data-name='china'> 通过var v = document.getLE ...
- DS实验题 Missile
题目: 提示:并没有精度问题. 原题 NOIP2010 导弹拦截 思路 设源点为A(x1, y1)和B(x2, y2). 第一步,用结构体存节点,包括以下元素: 1.横坐标x 2.纵坐标y 3.节点和 ...
- PHP 错误与异常 笔记与总结(17 )像处理异常一样处理 PHP 错误
有两种方式可以在 PHP 中以异常的方式处理错误: ① PHP 内置的 ErrorException类(也是 Exception 类的子类) <?php function exception_e ...
- jQuery Ajax 确定 form 表单 submit 提交成功
使用 jQuery 提交表单,可以使用 同步方式(async: false). a.html 是 html 文件,a.php 是服务端文件,把 a.html 中表单的数据提交到 a.php 中,在提交 ...