【leetcode】698. Partition to K Equal Sum Subsets
题目如下:
解题思路:本题是【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的更多相关文章
- 【LeetCode】698. Partition to K Equal Sum Subsets 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- [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 ...
- 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 ...
- 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 ...
- [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 ...
- LeetCode Partition to K Equal Sum Subsets
原题链接在这里:https://leetcode.com/problems/partition-to-k-equal-sum-subsets/description/ 题目: Given an arr ...
- 【LeetCode】548. Split Array with Equal Sum 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 日期 题目地址:https://leetcode ...
- 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 ...
- 【leetcode】1043. Partition Array for Maximum Sum
题目如下: Given an integer array A, you partition the array into (contiguous) subarrays of length at mos ...
随机推荐
- [转]delphi 防止刷新时闪烁的终极解决办法
{ 防止刷新时闪烁的终极解决办法(对付双缓冲无效时) }Perform($000B, 0, 0); //锁屏幕 防止闪烁 // 做一些会发生严重闪烁的事情.. //解锁屏幕并重画Perform($00 ...
- log() exp()函数
1 对数函数表示法 import numpy as np import math print('输出自然底数e:',math.e) # np表示法 # np.log()是以e为底的自然对数 print ...
- TCP 首部格式
<图解TCP/IP> 6.7 TCP的首部格式 TCP中没有表示包长度和数据长度的字段.可由IP层获知TCP的包长由TCP的包长可知数据的长度. 源端口号:表示发送端端口号,字段长16位 ...
- windows 下使用Linux子系统
在 Windows 上进行 web 开发,比较普遍的方案是使用 phpstudy 或者别的一些集成环境软件进行环境搭建,写好代码后将代码上传至版本管理工具 git/svn,再将代码同步到 Linux ...
- centos7安装java JDK
Java环境 1.下载jdk(用FileZilla或xshell工具连接服务器后上传到需要安装的目录) 在 /opt/deploy 下新建 java 文件夹: # mkdir / opt/deploy ...
- 应用安全 - Web框架 - Apache Solr - 漏洞汇总
CVE-2019-12409 Date: // 类型: 配置不当导致远程代码执行 前置条件: 影响范围: Solr and for Linux Solr下载:https://www.apache.or ...
- 深入理解java:1. JVM虚拟机的构成
1.JVM虚拟机的构成 什么是Java虚拟机? 作为一个Java程序员,我们每天都在写Java代码,我们写的代码都是在一个叫做Java虚拟机的东西上执行的. 但是如果要问什么是虚拟机,恐怕很多人就会模 ...
- mysql常用知识点之limit
limit函数的应用.limit后面跟整数,如limit 5,表示在结果集中取前5条:limit后跟整数区间,如limit 2,5,表示在结果集中 从第3条开始,取5条数据,第一个整数表示结果集的顺序 ...
- JPA-style positional param was not an integral ordinal 异常
JPA-style positional param was not an integral ordinal 多是sql之间的空格问题,或者sql拼接问题. 字符串与字符串直接相加要加空格
- [19/10/14-星期一] Python中的对象和类
一.面向对象 ## 什么是对象? - 对象是内存中专门用来存储数据的一块区域. - 对象中可以存放各种数据(比如:数字.布尔值.代码) - 对象由三部分组成: 1.对象的标识(id) 2.对象的类型( ...