|
| 1 | +# 🔥 Largest Perimeter Triangle 🔥 || 5 Solutions || Simple Fast and Easy || with Explanation |
| 2 | + |
| 3 | +## Solution - 1 |
| 4 | + |
| 5 | +Before diving into code.. Let us recall the basic rule for lengths of sides of a triangle |
| 6 | + i.e, sum of any two sides of a triangle should be greater than its third side. |
| 7 | + Simply, a + b > c |
| 8 | + where a, b, c are sides of triangle |
| 9 | + Approach: |
| 10 | + Since we need the largest perimeter from the given nums. We sort the array and traverse from the end |
| 11 | + |
| 12 | + if nums[i] < nums[i-1] + nums[i-2], then |
| 13 | + we Stop and return the perimeter==> nums[i] + nums[i-1] + nums[i-2] |
| 14 | + |
| 15 | + if there is no values that satisfies the given condition then |
| 16 | + we return 0 |
| 17 | + |
| 18 | +```dart |
| 19 | +class Solution { |
| 20 | +// Runtime: 426 ms, faster than 80.00% of Dart online submissions for Largest Perimeter Triangle. |
| 21 | +// Memory Usage: 149.6 MB, less than 100.00% of Dart online submissions for Largest Perimeter Triangle. |
| 22 | +
|
| 23 | + int largestPerimeter(List<int> nums) { |
| 24 | + nums.sort(); |
| 25 | + for (int i = nums.length - 1; i > 1; --i) |
| 26 | + if (nums[i] < nums[i - 1] + nums[i - 2]) |
| 27 | + return nums[i] + nums[i - 1] + nums[i - 2]; |
| 28 | + return 0; |
| 29 | + } |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +## Solution - 2 Recursive |
| 34 | + |
| 35 | +```dart |
| 36 | +class Solution { |
| 37 | +// Runtime: 514 ms, faster than 60.00% of Dart online submissions for Largest Perimeter Triangle. |
| 38 | +// Memory Usage: 150.2 MB, less than 20.00% of Dart online submissions for Largest Perimeter Triangle. |
| 39 | +
|
| 40 | + //utility method to get max element at given index |
| 41 | + void swapToGetMax(List<int> nums, int index) { |
| 42 | + int max = 0, maxIndex = -1; |
| 43 | + for (int i = 0; i <= index; i++) |
| 44 | + if (max < nums[i]) { |
| 45 | + max = nums[i]; |
| 46 | + maxIndex = i; |
| 47 | + } |
| 48 | + //actual swapping after finding max element in given range |
| 49 | + int temp = nums[index]; |
| 50 | + nums[index] = max; |
| 51 | + nums[maxIndex] = temp; |
| 52 | + } |
| 53 | +
|
| 54 | + int largestPerimeter(List<int> nums) { |
| 55 | + //if array has only 3 elements |
| 56 | + if (nums.length == 3) { |
| 57 | + if (nums[0] < nums[1] + nums[2] && |
| 58 | + nums[1] < nums[0] + nums[2] && |
| 59 | + nums[2] < nums[1] + nums[0]) |
| 60 | + return nums[0] + nums[1] + nums[2]; |
| 61 | + else |
| 62 | + return 0; |
| 63 | + } |
| 64 | + //for more than 3 elements, without doing explicit sorting |
| 65 | + int n = nums.length; |
| 66 | + //here we are putting max element at last index |
| 67 | + swapToGetMax(nums, n - 1); |
| 68 | + //here we are putting max element at second last index |
| 69 | + swapToGetMax(nums, n - 2); |
| 70 | + //here we are putting max element at third last index |
| 71 | + swapToGetMax(nums, n - 3); |
| 72 | + //in loop checking if nums[i]<nums[i-1]+nums[i-2] which this triplet will form the max perimeter |
| 73 | + for (int i = n - 1; i >= 2; i--) { |
| 74 | + if (nums[i] < nums[i - 1] + nums[i - 2]) |
| 75 | + return nums[i] + nums[i - 1] + nums[i - 2]; |
| 76 | + //if not then will find max element as (i-3)th largest element |
| 77 | + else if (i > 2) swapToGetMax(nums, i - 3); |
| 78 | + } |
| 79 | + return 0; |
| 80 | + } |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +## Solution - 3 |
| 85 | + |
| 86 | +```dart |
| 87 | +class Solution { |
| 88 | +// Runtime: 596 ms, faster than 40.00% of Dart online submissions for Largest Perimeter Triangle. |
| 89 | +// Memory Usage: 150.3 MB, less than 20.00% of Dart online submissions for Largest Perimeter Triangle. |
| 90 | + int largestPerimeter(List<int> nums) { |
| 91 | + if (nums.length == 3) { |
| 92 | + if (nums[0] < nums[1] + nums[2] && |
| 93 | + nums[1] < nums[0] + nums[2] && |
| 94 | + nums[2] < nums[1] + nums[0]) |
| 95 | + return nums[0] + nums[1] + nums[2]; |
| 96 | + else |
| 97 | + return 0; |
| 98 | + } |
| 99 | + int n = nums.length; |
| 100 | + int maxi = 0; |
| 101 | + for (int i = 0; i < n - 2; i++) { |
| 102 | + for (int j = i + 1; j < n - 1; j++) { |
| 103 | + for (int k = j + 1; k < n; k++) { |
| 104 | + int a = nums[i]; |
| 105 | + int b = nums[j]; |
| 106 | + int c = nums[k]; |
| 107 | + if (a < b + c && b < c + a && c < a + b) maxi = max(maxi, a + b + c); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + if (maxi > 0) return maxi; |
| 112 | + return 0; |
| 113 | + } |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +## Solution - 4 |
| 118 | + |
| 119 | +```dart |
| 120 | +class Solution { |
| 121 | + int largestPerimeter(List<int> nums) { |
| 122 | + //sort the vector |
| 123 | + nums.sort(); |
| 124 | + //any triangle sum of smaller two side greater than 3rd side...so we check that condition, a+b>c where a<b<c |
| 125 | + int maxPerimeter = 0; |
| 126 | +
|
| 127 | + for (int i = 0; i <= nums.length - 3; i++) { |
| 128 | + //check the triangle valid condition |
| 129 | + if (nums[i] + nums[i + 1] > nums[i + 2]) { |
| 130 | + maxPerimeter = |
| 131 | + max(maxPerimeter, nums[i] + nums[i + 1] + nums[i + 2]); //find max |
| 132 | + } |
| 133 | + } |
| 134 | +
|
| 135 | + return maxPerimeter; //return the result |
| 136 | + } |
| 137 | +} |
| 138 | +``` |
| 139 | + |
| 140 | +## Solution - 5 |
| 141 | + |
| 142 | +```dart |
| 143 | +class Solution { |
| 144 | + int largestPerimeter(List<int> nums) { |
| 145 | + nums.sort((a, b) => a - b); |
| 146 | + int i = nums.length - 1; |
| 147 | + while (i >= 0) { |
| 148 | + if (nums[i] < nums[i - 1] + nums[i - 2]) { |
| 149 | + return nums[i] + nums[i - 1] + nums[i - 2]; |
| 150 | + } else { |
| 151 | + i--; |
| 152 | + } |
| 153 | + } |
| 154 | + return 0; |
| 155 | + } |
| 156 | +} |
| 157 | +``` |
0 commit comments