-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatchsticks-to-square.js
More file actions
31 lines (23 loc) · 926 Bytes
/
Copy pathmatchsticks-to-square.js
File metadata and controls
31 lines (23 loc) · 926 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* @param {number[]} matchsticks
* @return {boolean}
*/
var makesquare = function(matchsticks) {
// Calculate the min required length for each side.
const minRequiredLength = matchsticks.reduce((x,y) => x+y)/4;
if(!Number.isInteger(minRequiredLength)) return false;
const sides = [0,0,0,0];
return recurse(sides, matchsticks.sort((a,b) => b-a) , 0, minRequiredLength);
//Bin packing first-fit recommends sorting in descending order
};
const recurse = function(sides, matchsticks, pos, minRequiredLength){
if(pos >= matchsticks.length) return true;
for(let i=0; i< sides.length; i++){
if(sides[i] + matchsticks[pos] <= minRequiredLength){
const side_copy = [...sides];
side_copy[i] += matchsticks[pos];
if(recurse(side_copy, matchsticks, pos+1, minRequiredLength)) return true;
}
}
return false;
}