LeetCode in Kotlin

473. Matchsticks to Square

Medium

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:

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:

Solution

class Solution {
    fun makesquare(matchsticks: IntArray): Boolean {
        if (matchsticks.size < 4) {
            return false
        }
        var per = 0
        for (ele in matchsticks) {
            per = ele + per
        }
        if (per % 4 != 0) {
            return false
        }
        matchsticks.sort()
        val side = per / 4
        val sides = intArrayOf(side, side, side, side)
        return help(matchsticks, matchsticks.size - 1, sides)
    }

    private fun help(nums: IntArray, i: Int, side: IntArray): Boolean {
        if (i == -1) {
            return side[0] == 0 && side[1] == 0 && side[2] == 0 && side[3] == 0
        }
        for (j in 0..3) {
            if (nums[i] > side[j]) {
                continue
            }
            side[j] -= nums[i]
            if (help(nums, i - 1, side)) {
                return true
            }
            side[j] += nums[i]
        }
        return false
    }
}