zoukankan      html  css  js  c++  java
  • 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 } }
  • 相关阅读:
    CloudFlare防护下的破绽:寻找真实IP的几条途径
    用Log Parser Studio分析IIS日志
    Log Parser 2.2 + Log Parser Lizard GUI 分析IIS日志示例
    wordPress Development
    MyISAM 与 InnoDB 的区别
    ubuntu安装wiz笔记
    chown命令
    (转载)我们工作到底为了什么
    DS_Store 是什么文件
    Linux命令 ,在当前目录下查找一个,或者多个文件
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/13038141.html
Copyright © 2011-2022 走看看