题目如下:

Given a rectangle of size n x m, find the minimum number of integer-sided squares that tile the rectangle.

Example 1:

Input: n = 2, m = 3
Output: 3
Explanation: 3 squares are necessary to cover the rectangle.
2 (squares of 1x1)
1 (square of 2x2)

Example 2:

Input: n = 5, m = 8
Output: 5

Example 3:

Input: n = 11, m = 13
Output: 6

Constraints:

  • 1 <= n <= 13
  • 1 <= m <= 13

解题思路:暴力破解法,没想到能AC。用矩阵表示矩形,元素值为0表示没有覆盖,1表示被覆盖,然后计算即可。

代码如下:

class Solution(object):
def tilingRectangle(self, n, m):
"""
:type n: int
:type m: int
:rtype: int
"""
import copy
def isCoverd(grid):
count = 0
for i in grid:
for j in i:count += int(j)
return count == (len(grid) * len(grid[0]))
def getNextOne(grid):
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == '':
return i,j
return None def encode(grid):
grid_str = ''
for i in grid:
for j in i:
grid_str += j
grid_str += '#'
return grid_str[:-1] def decode(grid_str):
grid_split = grid_str.split('#')
grid = []
for line in grid_split:
grid.append(list(line))
return grid self.res = 0
def greed(n,m):
self.res += 1
if n == m:
return
elif n < m:
greed(n,m-n)
else:greed(n-m,m) greed(n,m) dic = {} grid = [[''] * m for _ in range(n)]
MAX_SIDE_LENGTH = min(n,m)
queue = []
for i in range(1,MAX_SIDE_LENGTH+1):
new_grid = copy.deepcopy(grid)
for x in range(i):
for y in range(i):
new_grid[x][y] = ''
queue.append((encode(new_grid),1))
dic[encode(new_grid)] = 1 while len(queue) > 0:
mat_str,path = queue.pop(0)
mat = decode(mat_str)
if isCoverd(mat):
self.res = min(self.res,path)
continue
elif path >= self.res:
continue
i,j = getNextOne(mat)
for k in range(1,MAX_SIDE_LENGTH+1):
if i + k > len(mat) or j + k > len(mat[i]):
break
flag = True
#new_mat = copy.deepcopy(mat)
new_mat = decode(mat_str)
for x in range(k):
if flag == False:
break
for y in range(k):
if new_mat[i+x][j+y] == '':
flag = False
break
new_mat[i+x][j+y] = ''
new_mat_str = encode(new_mat)
if flag and (new_mat_str not in dic or dic[new_mat_str] > path + 1):
queue.append((encode(new_mat),path+1))
dic[new_mat_str] = path + 1
return self.res

【leetcode】1240. Tiling a Rectangle with the Fewest Squares的更多相关文章

  1. 【leetcode】963. Minimum Area Rectangle II

    题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...

  2. 【LeetCode】963. Minimum Area Rectangle II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 线段长+线段中心+字典 日期 题目地址:https: ...

  3. 【LeetCode】939. Minimum Area Rectangle 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 确定对角线,找另外两点(4sum) 字典保存出现的x ...

  4. 【leetcode】492. Construct the Rectangle

    problem 492. Construct the Rectangle 参考 1. Leetcode_492. Construct the Rectangle; 完

  5. 【leetcode】939. Minimum Area Rectangle

    题目如下: Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from t ...

  6. 【LeetCode】492. Construct the Rectangle 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 python解法 日期 题目地址:ht ...

  7. 【leetcode】1019. Next Greater Node In Linked List

    题目如下: We are given a linked list with head as the first node.  Let's number the nodes in the list: n ...

  8. 【leetcode】907. Sum of Subarray Minimums

    题目如下: 解题思路:我的想法对于数组中任意一个元素,找出其左右两边最近的小于自己的元素.例如[1,3,2,4,5,1],元素2左边比自己小的元素是1,那么大于自己的区间就是[3],右边的区间就是[4 ...

  9. 【leetcode】901. Online Stock Span

    题目如下: 解题思路:和[leetcode]84. Largest Rectangle in Histogram的核心是一样的,都是要找出当前元素之前第一个大于自己的元素. 代码如下: class S ...

随机推荐

  1. SpringBoot使用thymeleaf的方式引用static中的静态资源

    当我们在开发网站时为了快速完成,避免不了使用第三方的框架文件.这样我们就得引用框架中的各种资源文件.那么,在springboot中通过 thymeleaf如何在html中使用static文件夹下的静态 ...

  2. 一个无法解析的外部命令and无法解析的外部符号

    出现错误: 解决: 因为修改了函数定义的参数,但是没有修改函数声明的参数 把参数统一一下就行了

  3. MogileFS表说明

    MogileFS大致的表说明如下 checksum:用来存放文件的校验和class:文件分类定义device:主机上的可用设备定义,包括设备可用空间,使用的权重等信息domain:域定义信息file: ...

  4. java——数据类型和运算符

    强类型语言 Java语言是一门强类型语言.强类型包含两方面的含义:①所有的变量必须先声明.后使用:②指定类型的变量只能接受类型与之匹配的值.强类型语言可以在编译过程中发现源代码的错误,从而保证程序更加 ...

  5. (六)在线文档编辑器的使用和数据字典(ueditor编辑器/my97datepicker日期控件)

    使用ueditor编辑器注意: 1. 要把ueditor的jar包添加到WEB-INF/lib里. 2. 在做图片上传等功能时,必须重写struts的过滤器,否则图片流会被拦截程序无法得到图片. 3. ...

  6. 使用隔离级别read committed隐式解决并发冲突

    1.使用rc的弊端:出现不可重复读 Oracle不可重复读 Oracle丢失修改 Oracle幻读 任何数据库的update  insert  delete都加排它锁 sql server的selec ...

  7. java小工具:通过URL连接爬取资源(图片)

    java语言编写一个简单爬取网站图片工具,实现简单: 通过 java.net.HttpURLConnection 获取一个URL连接 HttpURLConnection 连接成功返回一个java.io ...

  8. Junit 学习1 junit的简单使用

    package junit; import java.sql.Connection; import java.sql.SQLException; import org.junit.Test; impo ...

  9. Unity使用Resources读取Resources路径下的二进制文件(Binary Data)必须使用 .bytes扩展名

    将某二进制文件放在Resources目录下,希望用Resources.Load<TextAsset>的方式读取,发现TextAsset是null 查阅Unity文档得知,使用Resourc ...

  10. 如何在 vue 2.0+ 中引入全局的stylus文件,且能正常

    由于stylus在引用时,不能像一般的css文件直接在main.js中引用,就算引用了也会只能使用转换后的css,不能使用里面的函数,方法等,原因可能是:在这里引入会被直接编译成css,不能在别的模板 ...