zoukankan      html  css  js  c++  java
  • LeetCode 473. Matchsticks to Square

    原题链接在这里:https://leetcode.com/problems/matchsticks-to-square/

    题目:

    Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match girl has, please find out a way you can make one square by using up all those matchsticks. You should not break any stick, but you can link them up, and each matchstick must be used exactly one time.

    Your input will be several matchsticks the girl has, represented with their stick length. Your output will either be true or false, to represent whether you could make one square using all the matchsticks the little match girl has.

    Example 1:

    Input: [1,1,2,2,2]
    Output: true
    
    Explanation: You can form a square with length 2, one side of the square came two sticks with length 1.

    Example 2:

    Input: [3,3,3,3,4]
    Output: false
    
    Explanation: You cannot find a way to form a square with all the matchsticks.

    Note:

    1. The length sum of the given matchsticks is in the range of 0 to 10^9.
    2. The length of the given matchstick array will not exceed 15.

    题解:

    It is to check if matches could be deivied into 4 equal groups.

    Calculate the target sum/4.

    When checking if there is combination that hit target, DFS needs states nums, boolean [] visited, current starting index, current sum, target and how many groups already found.

    If sum > target, return false.

    If sum == target, then check next group, having starting index set back to 0.

    The reason state needs both visited and current starting index is to save time. If nums[i] can't be used to sum to current target, then for j>i, there is no need to check nums[i] again. 

    Because if nums[i] could be used, it would already be marked as true before.

    The only case it could be used now is to sum up to next target.

    Time Complexity: exponential.

    Space: O(nums.length). stack space.

    AC Java:

     1 class Solution {
     2     public boolean makesquare(int[] nums) {
     3         if(nums == null || nums.length < 4){
     4             return false;
     5         }
     6         
     7         int sum = 0;
     8         for(int num : nums){
     9             sum += num;
    10         }
    11         
    12         if(sum%4 != 0){
    13             return false;
    14         }
    15         
    16         return dfs(nums, new boolean[nums.length], 0, 0, sum/4, 4);
    17     }
    18     
    19     private boolean dfs(int [] nums, boolean [] visited, int start, int sum, int target, int edges){
    20         if(edges == 1){
    21             return true;
    22         }
    23         
    24         if(sum == target){
    25             return dfs(nums, visited, 0, 0, target, edges-1);
    26         }
    27         
    28         if(sum > target){
    29             return false;
    30         }
    31         
    32         for(int i = start; i<nums.length; i++){
    33             if(!visited[i]){
    34                 visited[i] = true;
    35                 if(dfs(nums, visited, i+1, sum+nums[i], target, edges)){
    36                     return true;
    37                 }
    38                 
    39                 visited[i] = false;
    40             }
    41         }
    42         
    43         return false;
    44     }
    45 }

    类似Partition to K Equal Sum Subsets.

  • 相关阅读:
    字典序(摘)
    N
    Y
    B
    购物网站和秒杀系统实现技术
    行测技巧:十字交叉法解决比值混合问题
    数学符号读法大全
    搜狐畅游编程题
    sql删除重复记录
    'for' loop initial declarations are only allo
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/11949691.html
Copyright © 2011-2022 走看看