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:
The length sum of the given matchsticks is in the range of 0 to 10^9.
The length of the given matchstick array will not exceed 15.

DFS, my solution is to fill each edge of the square one by one. DFS to construct the 1st, then 2nd, then 3rd, then 4th. For each edge I scan all the matches but only pick some, the others are discarded. These brings about two disadvantages: 1. I need to keep track of visited elem. 2. after the 1st edge is constructed, I have to re-scan to make the 2nd, 3rd, 4th, thus waste time! So my code get TLE in big case

 public class Solution {
public boolean makesquare(int[] nums) {
if (nums==null || nums.length==0) return false;
int sideLen = 0;
for (int num : nums) {
sideLen += num;
}
if (sideLen % 4 != 0) return false;
sideLen /= 4;
return find(nums, sideLen, 0, 0, new HashSet<Integer>());
} public boolean find(int[] nums, int sideLen, int completed, int curLen, HashSet<Integer> visited) {
if (curLen > sideLen) return false;
if (curLen == sideLen) {
completed++;
curLen = 0;
if (completed==4 && visited.size()==nums.length) return true;
if (completed==4 && visited.size()<nums.length) return false;
}
for (int i=0; i<nums.length; i++) {
if (!visited.contains(i)) {
visited.add(i);
if (find(nums, sideLen, completed, curLen+nums[i], visited))
return true;
visited.remove(i);
}
}
return false;
}
}

Better Solution: DFS, but only scan all the matches once! If a match can not make the 1st edge, then we do not discard it, we try 2nd, 3rd, 4th edge

referred to: https://discuss.leetcode.com/topic/72107/java-dfs-solution-with-explanation

 public class Solution {
public boolean makesquare(int[] nums) {
if (nums == null || nums.length < 4) return false;
int sum = 0;
for (int num : nums) sum += num;
if (sum % 4 != 0) return false; return dfs(nums, new int[4], 0, sum / 4);
} private boolean dfs(int[] nums, int[] sums, int index, int target) {
if (index == nums.length) {
if (sums[0] == target && sums[1] == target && sums[2] == target) {
return true;
}
return false;
} for (int i = 0; i < 4; i++) {
if (sums[i] + nums[index] > target) continue;
sums[i] += nums[index];
if (dfs(nums, sums, index + 1, target)) return true;
sums[i] -= nums[index];
} return false;
}
}

Updates on 12/19/2016 Thanks @benjamin19890721 for pointing out a very good optimization: Sorting the input array DESC will make the DFS process run much faster. Reason behind this is we always try to put the next matchstick in the first subset. If there is no solution, trying a longer matchstick first will get to negative conclusion earlier. Following is the updated code. Runtime is improved from more than 1000ms to around 40ms. A big improvement.

 public class Solution {
public boolean makesquare(int[] nums) {
if (nums==null || nums.length<4) return false;
int sideLen = 0;
for (int num : nums) {
sideLen += num;
}
if (sideLen % 4 != 0) return false;
sideLen /= 4;
Arrays.sort(nums);
reverse(nums);
return find(nums, new int[4], 0, sideLen);
} public boolean find(int[] nums, int[] edges, int index, int sideLen) {
if (index == nums.length) {
if (edges[0]==sideLen && edges[1]==sideLen && edges[2]==sideLen)
return true;
else return false;
} for (int i=0; i<4; i++) {
if (edges[i]+nums[index] > sideLen) continue;
edges[i] += nums[index];
if (find(nums, edges, index+1, sideLen)) return true;
edges[i] -= nums[index];
}
return false;
} public void reverse(int[] nums) {
int l=0, r=nums.length-1;
while (l < r) {
int temp = nums[l];
nums[l] = nums[r];
nums[r] = temp;
l++;
r--;
}
}
}

Grammar: How to sort array in descending order: http://www.java67.com/2016/07/how-to-sort-array-in-descending-order-in-java.html

How to sort object array in descending order

First, let's see the example of sorting an object array into ascending order. Then we'll see how to sort a primitive array in descending order. In order to sort a reference type array e.g. String array, Integer array or Employee array, you need to pass the Array.sort() method a reverse Comparator.

Fortunately, you don't need to code it yourself, you can use Collections.reverseOrder(Comparator comp) to get a reverse order Comparator. Just pass your Comparator to this method and it will return the opposite order Comparator.

If you are using Comparator method to sort in natural order, you can also use the overloaded Collection.reverseOrder() method. It returns a Comparator which sorts in the opposite of natural order. In fact, this is the one you will be using most of the time.

Here is an example of sorting Integer array in descending order:

Integer[] cubes = new Integer[] { 8, 27, 64, 125, 256 };
Arrays.sort(cubes, Collections.reverseOrder());

How to sort primitive array in descending order

Now, let's see how to sort a primitive array e.g. int[], long[],  float[] or char[] in descending order. As I told, there are no Arrays.sort() method which can sort the array in the reverse order. Many programmers make mistake of calling the above Array.sort() method as follows:

int[] squares = { 4, 25, 9, 36, 49 };
Arrays.sort(squares, Collections.reverseOrder());

This is compile time error "The method sort(int[]) in the type Arrays is not applicable for the arguments (int[], Comparator<Object>)" because there is no such method in the java.util.Arrays class.

The only way to sort a primitive array in descending order is first to sort it in ascending order and then reverse the array in place as shown here. Since in place reversal is an efficient algorithm and doesn't require extra memory, you can use it sort and reverse large array as well. You can also see a good book on data structure and algorithm e.g. Introduction to Algorithms to learn more about efficient sorting algorithm e.g. O(n) sorting algorithm like Bucket sort.

Leetcode: Matchsticks to Square && Grammar: reverse an primative array的更多相关文章

  1. [LeetCode] Matchsticks to Square 火柴棍组成正方形

    Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...

  2. Leetcode之深度优先搜索(DFS)专题-473. 火柴拼正方形(Matchsticks to Square)

    Leetcode之深度优先搜索(DFS)专题-473. 火柴拼正方形(Matchsticks to Square) 深度优先搜索的解题详细介绍,点击 还记得童话<卖火柴的小女孩>吗?现在, ...

  3. leetcode面试准备:Kth Largest Element in an Array

    leetcode面试准备:Kth Largest Element in an Array 1 题目 Find the kth largest element in an unsorted array. ...

  4. LeetCode 新题: Find Minimum in Rotated Sorted Array 解题报告-二分法模板解法

    Find Minimum in Rotated Sorted Array Question Solution Suppose a sorted array is rotated at some piv ...

  5. 【LeetCode】961. N-Repeated Element in Size 2N Array 解题报告(Python & C+++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...

  6. 【LeetCode】153. Find Minimum in Rotated Sorted Array 解题报告(Python)

    [LeetCode]153. Find Minimum in Rotated Sorted Array 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode. ...

  7. 【LeetCode】154. Find Minimum in Rotated Sorted Array II 解题报告(Python)

    [LeetCode]154. Find Minimum in Rotated Sorted Array II 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...

  8. 【LeetCode】473. Matchsticks to Square 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...

  9. LeetCode "473. Matchsticks to Square"

    A trickier DFS, with a little bit complex recursion param tweak, and what's more important is prunin ...

随机推荐

  1. html学习第二天—— 第七章——CSS样式基本知识

    外部式css样式,写在单独的一个文件中外部式css样式(也可称为外联式)就是把css代码写一个单独的外部文件中,这个css样式文件以“.css”为扩展名,在<head>内(不是在<s ...

  2. jface databinding:部分实现POJO对象的监测

    在前一篇博文<jface databinding/PojoBindable实现对POJO对象的支持  >中,已经知道直接对POJO对象进行修改,是不能被绑定的UI组件知道的,在上一篇文章中 ...

  3. 诡异的C语言实参求值顺序

    学了这么久的C语言,竟然第一次碰到这么诡异的实参求值顺序问题,大跌眼镜.果然阅读面太少了! #include<iostream> void foo(int a, int b, int c) ...

  4. ACM: NBUT 1107 盒子游戏 - 简单博弈

     NBUT 1107  盒子游戏 Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format:  Practice  Appoint ...

  5. Linux中总线设备驱动模型及平台设备驱动实例

    本文将简要地介绍Linux总线设备驱动模型及其实现方式,并不会过多地涉及其在内核中的具体实现,最后,本文将会以平台总线为例介绍设备和驱动程序的实现过程. 目录: 一.总线设备驱动模型总体介绍及其实现方 ...

  6. 随鼠标轮动翻动层————jquery小练习

    闲来无事在网站上看见一个网页制作的不错,就仿照做来看看.特此记录下来. 亮点:随鼠标上下滚动,展示页面随之不同,翻动效果. 功能点:鼠标向上,向下判断事件. css 代码 html { overflo ...

  7. mac优秀软件介绍

    1.首先是office软件: Microsoft_Office_2016_Installer.pkg 然后是一个破解软件 FWMSO2016VLU2.0.dmg_.zip 两个都不可少 2.然后是如果 ...

  8. centos7 最小化安装没有ifconfig及修改网卡名enoxxx为ethX

    问题: 1.最小化安装centos7后发现无ifconfig命令 想通过ifconfig查看ip地址发现ifconfig命令不存在,可通过命令 #ip addr       //查看ip 或者 解决: ...

  9. git revert和git reset的区别

    git revert 是撤销某次操作,此次操作之前的commit都会被保留 git reset 是撤销某次提交,但是此次之后的修改都会被退回到暂存区 具体一个例子,假设有三个commit, git s ...

  10. linux软件包管理(上)

    1.二进制包管理(RPM,yum) 2.源代码包的安装 3.脚本安装(shell或java) 4.Debian系列的linux软件包管理简介 在下载rmp包的时候注意检查硬件平台是否正确,如果硬件平台 ...