【leetcode】1240. Tiling a Rectangle with the Fewest Squares
题目如下:
Given a rectangle of size
nxm, find the minimum number of integer-sided squares that tile the rectangle.Example 1:
Input: n = 2, m = 3
Output: 3
Explanation:3squares are necessary to cover the rectangle.
2(squares of1x1)
1(square of2x2)Example 2:
Input: n = 5, m = 8
Output: 5Example 3:
Input: n = 11, m = 13
Output: 6Constraints:
1 <= n <= 131 <= 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的更多相关文章
- 【leetcode】963. Minimum Area Rectangle II
题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...
- 【LeetCode】963. Minimum Area Rectangle II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 线段长+线段中心+字典 日期 题目地址:https: ...
- 【LeetCode】939. Minimum Area Rectangle 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 确定对角线,找另外两点(4sum) 字典保存出现的x ...
- 【leetcode】492. Construct the Rectangle
problem 492. Construct the Rectangle 参考 1. Leetcode_492. Construct the Rectangle; 完
- 【leetcode】939. Minimum Area Rectangle
题目如下: Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from t ...
- 【LeetCode】492. Construct the Rectangle 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 python解法 日期 题目地址:ht ...
- 【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 ...
- 【leetcode】907. Sum of Subarray Minimums
题目如下: 解题思路:我的想法对于数组中任意一个元素,找出其左右两边最近的小于自己的元素.例如[1,3,2,4,5,1],元素2左边比自己小的元素是1,那么大于自己的区间就是[3],右边的区间就是[4 ...
- 【leetcode】901. Online Stock Span
题目如下: 解题思路:和[leetcode]84. Largest Rectangle in Histogram的核心是一样的,都是要找出当前元素之前第一个大于自己的元素. 代码如下: class S ...
随机推荐
- Spring Boot系列教程十二:Spring boot集成Redis
一.创建项目 项目名称为 "springboot_redis",创建过程中勾选 "Web","Redis",第一次创建Maven需要下载依赖 ...
- C++视频读取与视频保存
VideoCapture cap("E:\\122.avi"); //计算视频帧数 int VedioFPS = cap.get(CV_CAP_PROP_FPS); //cout ...
- Django自定义分页并保存搜索条件
Django自定义分页并保存搜索条件 1.自定义分页组件pagination.py import copy class Pagination: def __init__(self, current_p ...
- asp.net core-4.命令行配置
先用vs2017创建一个控制台应用程序,这里不做多的介绍. 现在命名空间中添加using Microsoft.Extensions.Configuration; 如果没有,就在依赖项—>管理Nu ...
- hdu 5212 反向容斥或者莫比
http://acm.hdu.edu.cn/showproblem.php?pid=5212 题意:忽略.. 题解:把题目转化为求每个gcd的贡献.(http://www.cnblogs.com/z1 ...
- sql 批量插入
create PROCEDURE insertinto as begindeclare @id int;set @id=1;while @id<10begininsert into perso ...
- optparser模块 与 ZIP爆破(Python)
optparser模块: 为脚本传递命令参数. 初始化: 带 Usage 选项(-h 的显示内容 Usage:): >>> from optparse import OptionPa ...
- 解决spring-boot-maven-plugin插件打包,springboot启动时报找不到主main问题
一:遇到的问题及解决方法 最近在搭建一个新项目时,使用spring-boot-maven-plugin插件打包,springboot项目在发布后启动时遇到找不到主main问题. 遇到这个问题当时感觉本 ...
- 【leetcode】566. Reshape the Matrix
原题 In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a ne ...
- MySQL详细知识点总结
1 Windows服务 -- 启动MySQL net start mysql -- 创建Windows服务 sc create mysql binPath= mysqld_bin_path(注意:等号 ...


