We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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))
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Maximum Perimeter Triangle
You are viewing a single comment's thread. Return to all comments →
Two pointer technique
def maximumPerimeterTriangle(sticks):