题目如下

解题思路:本题是【leetcode】473. Matchsticks to Square的姊妹篇,唯一的区别是【leetcode】473. Matchsticks to Square指定了分成四个子数组,而本题分成的份数不定,作为参数输入。另外,本题的测试用例好像复杂一点,因此我过滤掉了nums中值等于avg的元素,不参与后面的DFS计算,提高效率。

代码如下

class Solution(object):
def canPartitionKSubsets(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: bool
"""
border = k
if sum(nums) % border != 0:
return False
avg = sum(nums) / border
#过滤掉了nums中值等于avg的元素,不参与后面的DFS计算
newnums = []
for i in nums:
if i == avg:
border -= 1
continue
newnums.append(i)
nums = newnums[:]
nums.sort()
queue = [[x] for x in xrange(len(nums))]
res = []
visit = [0 for x in xrange(len(nums))]
while len(queue) > 0:
nl = queue.pop(0)
amount = 0
for i in nl:
amount += nums[i]
if amount == avg:
res.append(nl)
for i in nl:
visit[i] = 1
continue
tl = []
for i in xrange(nl[-1] + 1, len(nums)):
if amount + nums[i] <= avg:
tl = nl[:]
tl.append(i)
queue.append(tl)
if len(res) < border:
return False
if sum(visit) != len(visit):
return False
queue = []
for i in res:
queue.append((set(i), 1))
# print queue
while len(queue) > 0:
ns, count = queue.pop(0)
if count == border and len(ns) == len(nums):
# print ns
return True
for i in res:
# print ns | set(i)
if len(ns | set(i)) == len(ns) + len(i):
queue.insert(0, (ns | set(i), count + 1)) return False

【leetcode】698. Partition to K Equal Sum Subsets的更多相关文章

  1. 【LeetCode】698. Partition to K Equal Sum Subsets 解题报告(Python & C++)

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

  2. [LeetCode] 698. Partition to K Equal Sum Subsets

    Problem Given an array of integers nums and a positive integer k, find whether it's possible to divi ...

  3. 698. Partition to K Equal Sum Subsets

    Given an array of integers nums and a positive integer k, find whether it's possible to divide this ...

  4. 698. Partition to K Equal Sum Subsets 数组分成和相同的k组

    [抄题]: Given an array of integers nums and a positive integer k, find whether it's possible to divide ...

  5. [LeetCode] Partition to K Equal Sum Subsets 分割K个等和的子集

    Given an array of integers nums and a positive integer k, find whether it's possible to divide this ...

  6. LeetCode Partition to K Equal Sum Subsets

    原题链接在这里:https://leetcode.com/problems/partition-to-k-equal-sum-subsets/description/ 题目: Given an arr ...

  7. 【LeetCode】548. Split Array with Equal Sum 解题报告(C++)

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

  8. Partition to K Equal Sum Subsets

    Given an array of integers nums and a positive integer k, find whether it's possible to divide this ...

  9. 【leetcode】1043. Partition Array for Maximum Sum

    题目如下: Given an integer array A, you partition the array into (contiguous) subarrays of length at mos ...

随机推荐

  1. Linux_Rsync远程同步备份服务器

    目录 目录 Remote Sync 同步的类型 本地模式 远程模式 RSync列表模式 RSync 服务模式 Setup RSync service How to use the rsync comm ...

  2. Python基本语法_基本数据类型_序列类型详解

    目录 目录 序列 序列的标准操作符 切片操作符 一个例子 字符串的连接 序列的功能函数 enumerate 枚举出序列对象的元素 len 获取序列对象的长度 min 取出sequence中的最小值 m ...

  3. Jmeter之仅一次控制器

    在进行压测时,往往登录只需要一次,其他的接口需要循环测试.这里就会用到控制器中的仅一次控制器.只要放置在控制器的请求,不管在哪种情况下,都只执行一次. 1.名称:可以标识在该控制器下主要完成的 工作内 ...

  4. HTML学习之==>CSS

    一.CSS选择器 id选择器 class选择器 标签选择器 标签层级选择器 class层级选择器 id层级选择器 id组合选择器 class组合选择器 属性选择器 <!DOCTYPE html& ...

  5. springboot+dubbo基于zookeeper快速搭建一个demo

    由于小编是在windows环境下搭建的,故该示例均为在windows下操作,这里只是提供一个快速搭建思路,linux操作也基本上差不多. 首先本示例的dubbo是基于zookeeper发布订阅消息的, ...

  6. 关于连接池和DBUtils

    1.关于数据库连接池 连接池就是创建和管理一个连接缓冲池的技术,这些连接好被任意线程所使用,数据库连接池负责分配和管理.释放数据库连接,总而言之数据库连接池就是提高对数据库的操作性能. 数据库连接池: ...

  7. Qt 遍历不规则树的节点

    在使用Qt的GraphicsScene作图时,遇到类似这样的需求:在scene中创建节点类似下图, 现在我要把每个节点的txt保存到xml文件中,结构为 <?xml version='1.0' ...

  8. 【IO流】FileInputStream FileOutputStream BufferInputStream BufferOutputStream

    FileInputStream IO流用来处理设备之间的数据传输. Java对数据的操作是通过流的方式. Java用于操作流的类都在IO包中. 流按流向分为两种:输入流,输出流. 流按操作类型分为两种 ...

  9. 剑指offer-树的子结构-python

    题目描述 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构)     思路 空树不是任意一个树的子结构,如果 root1 与root2 中有一个为空树的话,返回 ...

  10. 简单的物流项目实战,WPF的MVVM设计模式(四)

    接下来写ViewModels 创建运单的ViewModel类 public class CreateExpressWindowViewModel: NotificationObject { priva ...