-
Notifications
You must be signed in to change notification settings - Fork 277
/
Copy pathMatchSticksToSquare.java
88 lines (67 loc) · 2.36 KB
/
MatchSticksToSquare.java
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//https://leetcode.com/problems/matchsticks-to-square/
// Solution 1: Time Limit Exceeding
class Solution {
// TC : O(4^N)
public boolean makesquare(int[] nums) {
if (nums.length < 4) return false;
int perimeter = 0;
for (int el : nums) {
perimeter += el;
}
if (perimeter % 4 != 0) return false;
Arrays.sort(nums);
int side = perimeter / 4;
int[] sides = new int[] {side, side, side, side};
return makesquareHelper(nums, 0, sides);
}
private boolean makesquareHelper(int[] nums, int i, int[] sides) {
if(i == nums.length) {
if(sides[0] == 0 && sides[1] == 0 && sides[2] == 0 && sides[3] == 0){
return true;
} else{
return false;
}
}
for (int j = 0; j < 4; j++) {
if (nums[i] > sides[j]) continue;
sides[j] -= nums[i];
if (makesquareHelper(nums, i + 1, sides)) return true;
sides[j] += nums[i];
}
return false;
}
}
// Solution 2: Accepted
class Solution {
public boolean makesquare(int[] matchsticks) {
int sum = 0;
for (int matchstick : matchsticks)
sum += matchstick;
if (sum % 4 != 0) // The four sides cannot be equal
return false;
int sideLen = sum / 4;
int[] sides = new int[] {sideLen, sideLen, sideLen, sideLen};
Arrays.sort(matchsticks); // Always try longer matchstick first
return helper(matchsticks, sides, matchsticks.length - 1); // idx starts from right to left
}
private boolean helper(int[] matchsticks, int[] sides, int idx) {
if (idx == -1) // use up matchstick
return allZero(sides);
for (int i = 0; i < sides.length; i++) { // for each matchstick, try four sides
if (matchsticks[idx] <= sides[i]) {
sides[i] -= matchsticks[idx];
if(helper(matchsticks, sides, idx - 1))
return true;
sides[i] += matchsticks[idx]; // backtrack
}
}
return false;
}
private boolean allZero(int[] sides) {
for (int i = 0; i < sides.length; i++) {
if (sides[i] > 0)
return false;
}
return true;
}
}