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 } }
  • 相关阅读:
    使用ZooKeeper实现Java跨JVM的分布式锁
    基于ZooKeeper的分布式锁和队列
    activiti数据库表结构剖析
    visualvm监控jvm及远程jvm监控方法
    使用visualvm 远程监控 JVM
    java jprofile
    Linux服务器上监控网络带宽的18个常用命令
    Redis-sentinel哨兵模式集群方案配置
    电容的去耦半径
    DC-DC BUCK电源芯片的基本原理和组成
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/13038141.html
Copyright © 2011-2022 走看看