Maximum Perimeter Triangle

  • + 0 comments

    def valid_triangle(tri):

    if tri[0] + tri[1] > tri[2] and tri[1] + tri[2] > tri[0] and tri[2] + tri[0] > tri[1]:
        return True
    return False
    

    def maximumPerimeterTriangle(sticks):

    n_tri = []
    sticks.sort()
    for i in range(len(sticks)):
        tri = sticks[i:i + 3]
        if len(tri) == 3 and valid_triangle(tri):
            n_tri.append((tri, sum(tri)))
    if len(n_tri) > 0:
        m = max([i[1] for i in n_tri if i[1]])
        n = max([i[0] for i in n_tri if i[1] >= m])
    else:
        n = [-1]
    return n