698. Partition to K Equal Sum Subsets
package LeetCode_698 /** * 698. Partition to K Equal Sum Subsets * https://leetcode.com/problems/partition-to-k-equal-sum-subsets/description/ * * Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal. Example 1: Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4 Output: True Explanation: It's possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums. Note: 1 <= k <= len(nums) <= 16. 0 < nums[i] < 10000. * */ class Solution { fun canPartitionKSubsets(nums: IntArray, k: Int): Boolean {
//dfs solution, Time complexity:O(n!), Space complexity:O(n) //because we have to find k subset, so sum of each subset is nums.sum/k val sum = nums.sum() if (sum % k != 0) { //can not find k subset return false } val target = sum / k val visited = BooleanArray(nums.size) nums.sort() return dfs(nums, k, 0, target, visited, 0) } private fun dfs(nums: IntArray, k: Int, curSum: Int, target: Int, visited: BooleanArray, start: Int): Boolean { //println("target:$target") //println("curSum:$curSum") if (k == 0) { //found the result return true } if (curSum > target) { return false } if (curSum == target) { //find out 1 result, set k-1 and curSum=0 to find the next one return dfs(nums, k - 1, 0, target, visited, 0) } for (i in start until nums.size) { if (visited[i]) { continue } visited[i] = true if (dfs(nums, k, curSum + nums[i], target, visited, i + 1)) { return true } visited[i] = false } return false } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)