题目如下:

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. Tcp问题汇总

    一 TCP三次握手 PS:TCP协议中,主动发起请求的一端称为『客户端』,被动连接的一端称为『服务端』.不管是客户端还是服务端,TCP连接建立完后都能发送和接收数据. 起初,服务器和客户端都为CLOS ...

  2. linux下安装lnmp集成环境

    linux下安装lnmp集成环境 教程地址:https://www.cnblogs.com/peteremperor/p/6750204.html 必须要用root用户,否则权限不够无法安装 安装最新 ...

  3. Oracle数据库——查询所有用户

    查询数据库所有用户(ALL_USERS)的用户名,用户编号,创建日期 默认应该有36个用户 SELECT * FROM ALL_USERS; 查看ALL_USERS的结构 DESC ALL_USERS ...

  4. Eclipse一些技巧

    1:测试某个测试溢出,修改堆内存大小 // 模拟内存溢出 -Xms10m -Xmx10m private static void mockOOM() { List list = new ArrayLi ...

  5. 从零开始学ios开发(二):Hello World!

    今天看了书的第二章,主要介绍了一下Xcode的使用方法和一些必要的说明,最后做了一个“Hello World!”的小程序,其实就是在屏幕上用一个Label显示“Hello World!”,一行代码都没 ...

  6. 调用顺丰API实现电商专递下单和获取面单功能

    参考文章:https://www.cnblogs.com/zhangxiaoyong/p/8317229.html 顺丰需求文档: 链接:https://pan.baidu.com/s/16EEaph ...

  7. .NET Core 常用第三方包

    .NET Core 常用第三方包 作者:高堂 原文地址:https://www.cnblogs.com/gaotang/p/10845370.html 写在前面 最近在学习.NET Core 中经常用 ...

  8. 二、运行时JVM结构组成及作用

    二.运行时JVM结构组成及作用 程序计数器 是否共享:否,线程私有,每个线程有1个独立的程序计数器! 所处位置:线程私有的内部区域 生命周期:与线程绑定 主要作用: 当前线程执行字节码的行号指示器!  ...

  9. Seaborn(一)之风格管理

    Seaborn简介 seaborn同matplotlib一样,也是Python进行数据可视化分析的重要第三方包.但seaborn是在 matplotlib的基础上进行了更高级的API封装,使得作图更加 ...

  10. 一款结合nmap及mascan还有shodan的扫描脚本

    github在这里 https://github.com/s0md3v/Silver 很是舒服 Usage Note: Silver scans all TCP ports by default i. ...