The temple beams
- O(n log n) · Hard
- Full plan
- Python
- JavaScript
- lists
- sorting
- comparisons
Problem
In Mycenae, a temple's gable is a triangle made of three beams, and a triangle only closes if each side is shorter than the other two put together. The architect wants the biggest gable the workshop can give. Write a function that takes beams, a list of positive integers (the length of each beam; it may hold fewer than three, or none), and returns the largest perimeter of a triangle made from three of those beams, or 0 if none can be built. Each beam is used only once, but two different beams may have the same length.
Being equal is not enough: 2, 3 and 5 do not close, since 2 + 3 is 5, not more.
With [2, 3, 5, 4], the best is 3, 4 and 5, which closes because 3 + 4 = 7 is more than 5: return 12.
Examples
The example
[2, 3, 5, 4] → 12
The three longest do not close
[3, 6, 2, 3] → 8
Three equal beams
[5, 5, 5] → 15
The biggest, not the first that closes
[6, 4, 7, 3, 5] → 18
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def perimeter(beams):
passJavaScript
function perimeter(beams) {
}It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.