-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0473.go
107 lines (89 loc) · 2.54 KB
/
0473.go
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Source: https://leetcode.com/problems/matchsticks-to-square
// Title: Matchsticks to Square
// Difficulty: Medium
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// You are given an integer array matchsticks where matchsticks[i] is the length of the ith matchstick. You want to use all the matchsticks to make one square. You should not break any stick, but you can link them up, and each matchstick must be used exactly one time.
//
// Return true if you can make this square and false otherwise.
//
// Example 1:
//
// https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg
//
// Input: matchsticks = [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: matchsticks = [3,3,3,3,4]
// Output: false
// Explanation: You cannot find a way to form a square with all the matchsticks.
//
// Constraints:
//
// 1 <= matchsticks.length <= 15
// 1 <= matchsticks[i] <= 10^8
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package main
import (
"fmt"
"sort"
)
type square struct {
edges [4]int
}
func (s square) Hash() int {
return s.edges[0] + s.edges[1]<<8 + s.edges[2]<<16 + s.edges[3]<<24
}
func (s square) IsSquare() bool {
return s.edges[0] == s.edges[1] && s.edges[0] == s.edges[2] && s.edges[0] == s.edges[3]
}
func makesquare(matchsticks []int) bool {
n := len(matchsticks)
if n < 4 {
return false
}
sum := 0
for _, v := range matchsticks {
sum += v
}
if sum%4 != 0 {
return false
}
edge := sum / 4
sort.Sort(sort.Reverse(sort.IntSlice(matchsticks)))
if matchsticks[0] > edge {
return false
}
seen := make(map[int]bool, 0)
sq := square{}
sq.edges[0] = matchsticks[0]
return _makesquare(matchsticks, 1, sq, edge, seen)
}
func _makesquare(matchsticks []int, idx int, sq square, edge int, seen map[int]bool) bool {
if idx == len(matchsticks) {
return sq.IsSquare()
}
sort.Ints(sq.edges[:])
hash := sq.Hash()
if seen[hash] {
return false
}
seen[hash] = true
stick := matchsticks[idx]
for i := 0; i < 4; i++ {
sq.edges[i] += stick
if sq.edges[i] <= edge && _makesquare(matchsticks, idx+1, sq, edge, seen) {
return true
}
sq.edges[i] -= stick
}
return false
}
func main() {
res := makesquare([]int{12, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60})
fmt.Println(res)
}