【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 ...
随机推荐
- 使用 netkeeper 创翼电脑开 WiFi 方法(12)
学校的宽带使用"netkeeper"联网,但是电脑依然可以开启WiFi,以下是方法: 1. Win10用户请看 2. Win7用户请看 Win7无法在"任务管理器&quo ...
- C++程序设计学习-第2章
第二章 变量与基本类型 1.基本内置类型 C++定义了一套包括算术类型和空类型在内的基本数据类型 算术类型:整型和浮点型,包括带符号类型(signed)和无符号类型(unsigned),带符号类型可以 ...
- Python 中文件操作
上代码: import os import os.path rootdir = "d:/code/su/data" # 指明被遍历的文件夹 for parent,dirnames, ...
- Luogu5327 ZJOI2019语言(树上差分+线段树合并)
暴力树剖做法显然,即使做到两个log也不那么优美. 考虑避免树剖做到一个log.那么容易想到树上差分,也即要对每个点统计所有经过他的路径产生的总贡献(显然就是所有这些路径端点所构成的斯坦纳树大小),并 ...
- .net core +gogs + jenkins +docker自动化发布、部署
1.首先,安装docker,不多bb 2.我们采用docker的方式安装jenkins,同时将宿主机的docker挂载到docker安装的jenkins里面,可能有点拗口.说白了就是 就是要让jenk ...
- java 框架-缓冲-Redis 2Jedis操作
https://www.cnblogs.com/wlandwl/p/redis.html Redis介绍及Jedis基础操作 1.Redis简介 Redis 是一个开源(BSD许可)的,内存中的数 ...
- 如何判断 Session是否存在
相信很多人都跟我一样,在写网页中有些位置通过其他网页设置了 Session然后跳转到目标页面就需要要用 Session,但是那个位置如果是直接打开的就用不到 Session,那么问题就来了,例如:系统 ...
- Mediawiki 子页链接无效的问题
添加下面的配置到 LocalSettings.php 中即可: # Enable subpages in the main namespace $wgNamespacesWithSubpages[NS ...
- Java 之 可变参数
可变参数 在JDK1.5之后,如果我们定义一个方法需要接受多个参数,并且多个参数类型一致,我们可以对其简化成如下格式: 修饰符 返回值类型 方法名(参数类型... 形参名){ } 其实这个书写完全等价 ...
- 卷积神经网络(CNN)的训练过程
卷积神经网络的训练过程 卷积神经网络的训练过程分为两个阶段.第一个阶段是数据由低层次向高层次传播的阶段,即前向传播阶段.另外一个阶段是,当前向传播得出的结果与预期不相符时,将误差从高层次向底层次进行传 ...


