|
| 1 | +--- |
| 2 | +id: Largest-Perimeter-Triangle |
| 3 | +title: Largest Perimeter Triangle |
| 4 | +sidebar_label: 0976 - Largest Perimeter Triangle |
| 5 | +tags: |
| 6 | + - Array |
| 7 | + - easy |
| 8 | + - Math |
| 9 | + - Greedy |
| 10 | + - Sorting |
| 11 | +--- |
| 12 | + |
| 13 | +## Problem Description |
| 14 | + |
| 15 | +Given an integer array `nums`, return the largest perimeter of a triangle with a non-zero area, formed from three of these lengths. If it is impossible to form any triangle of a non-zero area, return 0. |
| 16 | + |
| 17 | +### Examples |
| 18 | + |
| 19 | +**Example 1:** |
| 20 | + |
| 21 | +``` |
| 22 | +Input: nums = [2,1,2] |
| 23 | +Output: 5 |
| 24 | +Explanation: You can form a triangle with three side lengths: 1, 2, and 2. |
| 25 | +``` |
| 26 | + |
| 27 | +**Example 2:** |
| 28 | + |
| 29 | +``` |
| 30 | +Input: nums = [1,2,1,10] |
| 31 | +Output: 0 |
| 32 | +Explanation: |
| 33 | +You cannot use the side lengths 1, 1, and 2 to form a triangle. |
| 34 | +You cannot use the side lengths 1, 1, and 10 to form a triangle. |
| 35 | +You cannot use the side lengths 1, 2, and 10 to form a triangle. |
| 36 | +As we cannot use any three side lengths to form a triangle of non-zero area, we return 0. |
| 37 | +``` |
| 38 | + |
| 39 | +### Constraints |
| 40 | + |
| 41 | +``` |
| 42 | +3 <= nums.length <= 10^4 |
| 43 | +1 <= nums[i] <= 10^6 |
| 44 | +``` |
| 45 | + |
| 46 | +## Solution |
| 47 | + |
| 48 | +### Python |
| 49 | + |
| 50 | +```python |
| 51 | +def largestPerimeter(nums): |
| 52 | + nums.sort(reverse=True) |
| 53 | + for i in range(len(nums) - 2): |
| 54 | + if nums[i] < nums[i + 1] + nums[i + 2]: |
| 55 | + return nums[i] + nums[i + 1] + nums[i + 2] |
| 56 | + return 0 |
| 57 | + |
| 58 | +# Example usage: |
| 59 | +print(largestPerimeter([2, 1, 2])) # Output: 5 |
| 60 | +print(largestPerimeter([1, 2, 1, 10])) # Output: 0 |
| 61 | +``` |
| 62 | + |
| 63 | +### C++ |
| 64 | + |
| 65 | +```cpp |
| 66 | +#include <vector> |
| 67 | +#include <algorithm> |
| 68 | +using namespace std; |
| 69 | + |
| 70 | +class Solution { |
| 71 | +public: |
| 72 | + int largestPerimeter(vector<int>& nums) { |
| 73 | + sort(nums.rbegin(), nums.rend()); |
| 74 | + for (int i = 0; i < nums.size() - 2; ++i) { |
| 75 | + if (nums[i] < nums[i + 1] + nums[i + 2]) { |
| 76 | + return nums[i] + nums[i + 1] + nums[i + 2]; |
| 77 | + } |
| 78 | + } |
| 79 | + return 0; |
| 80 | + } |
| 81 | +}; |
| 82 | + |
| 83 | +// Example usage: |
| 84 | +// Solution sol; |
| 85 | +// vector<int> nums = {2, 1, 2}; |
| 86 | +// cout << sol.largestPerimeter(nums); // Output: 5 |
| 87 | +// nums = {1, 2, 1, 10}; |
| 88 | +// cout << sol.largestPerimeter(nums); // Output: 0 |
| 89 | +``` |
| 90 | +
|
| 91 | +### Java |
| 92 | +
|
| 93 | +```java |
| 94 | +import java.util.Arrays; |
| 95 | +
|
| 96 | +class Solution { |
| 97 | + public int largestPerimeter(int[] nums) { |
| 98 | + Arrays.sort(nums); |
| 99 | + for (int i = nums.length - 3; i >= 0; --i) { |
| 100 | + if (nums[i] + nums[i + 1] > nums[i + 2]) { |
| 101 | + return nums[i] + nums[i + 1] + nums[i + 2]; |
| 102 | + } |
| 103 | + } |
| 104 | + return 0; |
| 105 | + } |
| 106 | +
|
| 107 | + public static void main(String[] args) { |
| 108 | + Solution sol = new Solution(); |
| 109 | + int[] nums1 = {2, 1, 2}; |
| 110 | + System.out.println(sol.largestPerimeter(nums1)); // Output: 5 |
| 111 | + int[] nums2 = {1, 2, 1, 10}; |
| 112 | + System.out.println(sol.largestPerimeter(nums2)); // Output: 0 |
| 113 | + } |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +### JavaScript |
| 118 | + |
| 119 | +```javascript |
| 120 | +var largestPerimeter = function (nums) { |
| 121 | + nums.sort((a, b) => b - a); |
| 122 | + for (let i = 0; i < nums.length - 2; i++) { |
| 123 | + if (nums[i] < nums[i + 1] + nums[i + 2]) { |
| 124 | + return nums[i] + nums[i + 1] + nums[i + 2]; |
| 125 | + } |
| 126 | + } |
| 127 | + return 0; |
| 128 | +}; |
| 129 | + |
| 130 | +// Example usage: |
| 131 | +console.log(largestPerimeter([2, 1, 2])); // Output: 5 |
| 132 | +console.log(largestPerimeter([1, 2, 1, 10])); // Output: 0 |
| 133 | +``` |
0 commit comments