Maximum Perimeter Triangle

  • + 0 comments

    Two pointer technique

    def maximumPerimeterTriangle(sticks):

    n = len(sticks)
    sticks.sort()
    possible_triangles = []
    for i in range(n - 1, 1, -1):
        left = 0
        right = i - 1
        while left < right:
            if sticks[left] + sticks[right] > sticks[i]:
                for k in range(left, right):
                    possible_triangles.append((sticks[k], sticks[right], sticks[i]))
                right -= 1
            else:
                left += 1
    
    if not possible_triangles:
        return [-1]
    return max(possible_triangles, key=lambda x: sum(x))