359. 正三角形拼接

359. 正三角形拼接

中文English

给出 nn 根木棍,每次切割可以将 11 根木棍切成 22 段。
请计算出最少切割几次,可以从所有木棍中选出 33 根,组成一个 正三角形 。

样例

输入:
[2,3,7,5]
输出:
2

说明

可以从长为 77 的木棍中,切出 22 根长为 33 的木棍,那么木棍的长度应该为 [2,3,1,3,3,5][2,3,1,3,3,5],可以拼出边长为 33 的正三角形。

注意事项

一开始的木棍根数为 nn,3 le n le 10003n1000。
所有木棍的长度为一个整型数组 lengthslengths,1 le length_i le 10^91lengthi​​109​​。
切割必须要将木棍分成 22 根整数长度的木棍,而且总长度要和原木棍相等

class Solution:
    """
    @param lengths: the lengths of sticks at the beginning.
    @return: return the minimum number of cuts.
    """
    def makeEquilateralTriangle(self, lengths):
        # write your code here.
        if not lengths: return None 
        
        length_dict = {}
        for length in lengths:
            length_dict[length] = length_dict.get(length, 0) + 1 
        
        count = 2
        for key, val in length_dict.items():
            if val >= 3:
                return 0
            elif val == 2 or key*2 in length_dict.keys():
                count = 1 
        
        return count
原文地址:https://www.cnblogs.com/yunxintryyoubest/p/14171032.html