LeetCode 473. Matchsticks to Square in F#
URL
leetcode.com/problems/matchsticks-to-square
Code
github.com/syohex/dotnet-study/blob/master/..
let rec makeSquare' pos (edges: int []) len (matchSticks: int []) =
if pos = matchSticks.Length then
edges.[0] = edges.[1]
&& edges.[1] = edges.[2]
&& edges.[2] = edges.[3]
else
let ret =
seq { 0 .. 3 }
|> Seq.fold
(fun ret i ->
if ret then
true
else if edges.[i] + matchSticks.[pos] <= len then
edges.[i] <- edges.[i] + matchSticks.[pos]
let ret' =
makeSquare' (pos + 1) edges len matchSticks
edges.[i] <- edges.[i] - matchSticks.[pos]
ret'
else
false)
false
ret
let sum = matchSticks |> List.sum
if sum % 4 <> 0 then
false
else
let edges = Array.zeroCreate 4
let matchSticks' =
matchSticks
|> List.sort
|> List.rev
|> List.toArray
makeSquare' 0 edges (sum / 4) matchSticks'