All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0401_0500.s0473_matchsticks_to_square.Solution Maven / Gradle / Ivy

There is a newer version: 1.38
Show newest version
package g0401_0500.s0473_matchsticks_to_square;

// #Medium #Array #Dynamic_Programming #Bit_Manipulation #Backtracking #Bitmask
// #2022_07_20_Time_165_ms_(53.96%)_Space_41.8_MB_(64.07%)

import java.util.Arrays;

/**
 * 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:**
 *
 * ![](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] <= 108
**/
public class Solution {
    public boolean makesquare(int[] matchsticks) {
        if (matchsticks.length < 4) {
            return false;
        }
        int per = 0;
        for (int ele : matchsticks) {
            per = ele + per;
        }
        if (per % 4 != 0) {
            return false;
        }
        Arrays.sort(matchsticks);
        int side = per / 4;
        int[] sides = new int[] {side, side, side, side};
        return help(matchsticks, matchsticks.length - 1, sides);
    }

    private boolean help(int[] nums, int i, int[] side) {
        if (i == -1) {
            return side[0] == 0 && side[1] == 0 && side[2] == 0 && side[3] == 0;
        }
        for (int j = 0; j < 4; j++) {
            if (nums[i] > side[j]) {
                continue;
            }
            side[j] -= nums[i];
            if (help(nums, i - 1, side)) {
                return true;
            }
            side[j] += nums[i];
        }
        return false;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy