Leetcode Problem 976. Largest Perimeter Triangle

976. Largest Perimeter Triangle

Leetcode Solutions

Key approach of the solution

  1. Sort the array of side lengths in non-increasing order.
  2. Iterate through the sorted array starting from the third element to the end.
  3. For each element at index i, check if the elements at i-1 and i-2 satisfy the triangle inequality with the current element.
  4. If the inequality nums[i-2] + nums[i-1] > nums[i] holds, return the perimeter nums[i-2] + nums[i-1] + nums[i].
  5. If no valid triangle is found after the iteration, return 0.
UML Thumbnail

Alternative approach using brute force

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...