Maximum Perimeter Triangle

  • + 0 comments
    def maximumPerimeterTriangle(sticks):
        def nonDegenerateTriangle(t):
            return t[0] + t[1] > t[2]
    
        import itertools
        triangles = list(set(itertools.combinations(sticks, 3)))
    
        trueTriangles = [sorted(t) for t in triangles if nonDegenerateTriangle(sorted(t))]
    
        if not trueTriangles:
            return [-1]
        
        max_t2 = max(t[2] for t in trueTriangles)
        trueTriangles_t2 = [t for t in trueTriangles if t[2] == max_t2]
    
        max_t0 = max(t[0] for t in trueTriangles_t2)
        trueTriangles_t0 = [t for t in trueTriangles_t2 if t[0] == max_t0]
    
        return trueTriangles_t0[0]