题目如下

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

代码如下

  1. class Solution(object):
  2. def canPartitionKSubsets(self, nums, k):
  3. """
  4. :type nums: List[int]
  5. :type k: int
  6. :rtype: bool
  7. """
  8. border = k
  9. if sum(nums) % border != 0:
  10. return False
  11. avg = sum(nums) / border
  12. #过滤掉了nums中值等于avg的元素,不参与后面的DFS计算
  13. newnums = []
  14. for i in nums:
  15. if i == avg:
  16. border -= 1
  17. continue
  18. newnums.append(i)
  19. nums = newnums[:]
  20. nums.sort()
  21. queue = [[x] for x in xrange(len(nums))]
  22. res = []
  23. visit = [0 for x in xrange(len(nums))]
  24. while len(queue) > 0:
  25. nl = queue.pop(0)
  26. amount = 0
  27. for i in nl:
  28. amount += nums[i]
  29. if amount == avg:
  30. res.append(nl)
  31. for i in nl:
  32. visit[i] = 1
  33. continue
  34. tl = []
  35. for i in xrange(nl[-1] + 1, len(nums)):
  36. if amount + nums[i] <= avg:
  37. tl = nl[:]
  38. tl.append(i)
  39. queue.append(tl)
  40. if len(res) < border:
  41. return False
  42. if sum(visit) != len(visit):
  43. return False
  44. queue = []
  45. for i in res:
  46. queue.append((set(i), 1))
  47. # print queue
  48. while len(queue) > 0:
  49. ns, count = queue.pop(0)
  50. if count == border and len(ns) == len(nums):
  51. # print ns
  52. return True
  53. for i in res:
  54. # print ns | set(i)
  55. if len(ns | set(i)) == len(ns) + len(i):
  56. queue.insert(0, (ns | set(i), count + 1))
  57.  
  58. 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. [转]delphi 防止刷新时闪烁的终极解决办法

    { 防止刷新时闪烁的终极解决办法(对付双缓冲无效时) }Perform($000B, 0, 0); //锁屏幕 防止闪烁 // 做一些会发生严重闪烁的事情.. //解锁屏幕并重画Perform($00 ...

  2. log() exp()函数

    1 对数函数表示法 import numpy as np import math print('输出自然底数e:',math.e) # np表示法 # np.log()是以e为底的自然对数 print ...

  3. TCP 首部格式

    <图解TCP/IP> 6.7  TCP的首部格式 TCP中没有表示包长度和数据长度的字段.可由IP层获知TCP的包长由TCP的包长可知数据的长度. 源端口号:表示发送端端口号,字段长16位 ...

  4. windows 下使用Linux子系统

     在 Windows 上进行 web 开发,比较普遍的方案是使用 phpstudy 或者别的一些集成环境软件进行环境搭建,写好代码后将代码上传至版本管理工具 git/svn,再将代码同步到 Linux ...

  5. centos7安装java JDK

    Java环境 1.下载jdk(用FileZilla或xshell工具连接服务器后上传到需要安装的目录) 在 /opt/deploy 下新建 java 文件夹: # mkdir / opt/deploy ...

  6. 应用安全 - Web框架 - Apache Solr - 漏洞汇总

    CVE-2019-12409 Date: // 类型: 配置不当导致远程代码执行 前置条件: 影响范围: Solr and for Linux Solr下载:https://www.apache.or ...

  7. 深入理解java:1. JVM虚拟机的构成

    1.JVM虚拟机的构成 什么是Java虚拟机? 作为一个Java程序员,我们每天都在写Java代码,我们写的代码都是在一个叫做Java虚拟机的东西上执行的. 但是如果要问什么是虚拟机,恐怕很多人就会模 ...

  8. mysql常用知识点之limit

    limit函数的应用.limit后面跟整数,如limit 5,表示在结果集中取前5条:limit后跟整数区间,如limit 2,5,表示在结果集中 从第3条开始,取5条数据,第一个整数表示结果集的顺序 ...

  9. JPA-style positional param was not an integral ordinal 异常

    JPA-style positional param was not an integral ordinal 多是sql之间的空格问题,或者sql拼接问题. 字符串与字符串直接相加要加空格

  10. [19/10/14-星期一] Python中的对象和类

    一.面向对象 ## 什么是对象? - 对象是内存中专门用来存储数据的一块区域. - 对象中可以存放各种数据(比如:数字.布尔值.代码) - 对象由三部分组成: 1.对象的标识(id) 2.对象的类型( ...