【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 ...
随机推荐
- TypeScript 高级类型 接口(interface)
在代码的实现或者调用上能设定一定的限制和规范,就像契约一样.通常,我们把这种契约称为接口. TypeScript的核心原则之一是对值所具有的结构进行类型检查. 有时称为“鸭式辨型法”或“结构性子类型化 ...
- Oracle的varchar2如何比较大小
首先要说的是Oracle中字符类型的比较都是基于ASCII码表来实现的,我就简单做个总结. Oracle中varchar2类型的字符串使用的是非填充空格的标准来进行比较的(表格中右边的那列,注意空格的 ...
- FishingMaster(HDU-6709)【贪心】
题目链接:https://vjudge.net/problem/HDU-6709 题意:一个人要抓n条鱼,每抓一条鱼用时K,每烹饪一条鱼用时a[i],抓鱼的过程不能被打断,烹饪鱼的时候可以抓鱼,也可以 ...
- Python【Network/XHR/json】
##################################################################### 制定一个目标(爬取周杰伦的歌曲清单): 根据目标,确认一个方 ...
- 模块 json 和 pickle
目录 序列化 json 和 pickle 模块 序列化 序列:字符串 序列化:将其它数据类型转换成字符串的过程. 反序列化:字符串转成其它数据类型. 序列化的目的 1:以某种存储形式使用自定义对象持久 ...
- Oracle查询部门工资最高员工的两种方法 1、MAX()函数 2、RANK()函数
本文以SCOTT用户下初始的EMP表为参考.代码可直接使用. 查询EMP表结构的语句如下,[代码1]: DESC EMP; EMP表结构如下:[结果1]: SQL> DESC EMP ...
- Linux驱动函数解读
一.kmalloc().kzalloc()和vmalloc() 这三个函数都可以分配连续的虚拟内存 除此之外,这三个函数的区别有: 1. kmalloc()和kzalloc()函数分配的物理内存也是连 ...
- s5p6818裸机程序的设计:以GPIO为例
为了调试方便,首先确保对于硬件的控制没有问题. Makefile # Makefile edited by Schips # 2019-06-21 schips@dingtalk.com # 文件类型 ...
- 在JAVA中如何获取当前源文件名以及代码的行号
在最近经历中,遇见了这样一个问题,如何获取当前源文件名以及代码的行号,只是了解到C语言有预定义宏__FILE__.__LINE__,它们在预处理的时候都已经确定好了,但是在JAVA中应该怎么获取输出呢 ...
- atomikos 优化JDBC性能
JDBC performance comes for free with our pooling DataSource classes: AtomikosDataSourceBean for XA-e ...


