|
| 1 | +/* |
| 2 | + * Problem Link: |
| 3 | + * https://leetcode.com/problems/largest-perimeter-triangle/?envType=daily-question&envId=2025-09-28 |
| 4 | + * |
| 5 | + * Q: Largest Perimeter Triangle |
| 6 | + * |
| 7 | + * You are given an array nums of non-negative integers. |
| 8 | + * Return the largest perimeter of a triangle with non-zero area, formed from three of these lengths. |
| 9 | + * If it is impossible to form any triangle of non-zero area, return 0. |
| 10 | + * |
| 11 | + * ------------------------------------------------------ |
| 12 | + * Approach 1: Brute Force (O(n^3)) |
| 13 | + * ------------------------------------------------------ |
| 14 | + * Idea: |
| 15 | + * - Sort the array. |
| 16 | + * - Try every possible triplet (i, j, k). |
| 17 | + * - Check the triangle inequality: |
| 18 | + * a + b > c, a + c > b, b + c > a |
| 19 | + * - Keep track of the maximum perimeter. |
| 20 | + * |
| 21 | + * Dry Run Example: |
| 22 | + * nums = [2, 1, 2] |
| 23 | + * After sort = [1, 2, 2] |
| 24 | + * Pick (1,2,2): |
| 25 | + * 1 + 2 > 2 (yes) |
| 26 | + * 1 + 2 > 2 (yes) |
| 27 | + * 2 + 2 > 1 (yes) |
| 28 | + * Valid triangle, perimeter = 5 |
| 29 | + * Answer = 5 |
| 30 | + * |
| 31 | + * Time Complexity: O(n^3) |
| 32 | + * Space Complexity: O(1) |
| 33 | + */ |
| 34 | + |
| 35 | +import java.util.Arrays; |
| 36 | + |
| 37 | +class SolutionBruteForce { |
| 38 | + public int largestPerimeter(int[] nums) { |
| 39 | + Arrays.sort(nums); |
| 40 | + int max = 0; |
| 41 | + for (int i = 0; i < nums.length - 2; i++) { |
| 42 | + int x = nums[i]; |
| 43 | + for (int j = i + 1; j < nums.length - 1; j++) { |
| 44 | + int y = nums[j]; |
| 45 | + for (int k = j + 1; k < nums.length; k++) { |
| 46 | + int z = nums[k]; |
| 47 | + if (x + y > z && x + z > y && y + z > x) { |
| 48 | + max = Math.max(max, x + y + z); |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + return max; |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | + |
| 58 | +/* |
| 59 | + * ------------------------------------------------------ |
| 60 | + * Approach 2: Optimized Greedy (O(n log n)) |
| 61 | + * ------------------------------------------------------ |
| 62 | + * Idea: |
| 63 | + * - Sort the array. |
| 64 | + * - The largest perimeter will always come from the three largest numbers |
| 65 | + * that can form a valid triangle. |
| 66 | + * - Starting from the end of the sorted array, check triplets (i, i-1, i-2). |
| 67 | + * - As soon as we find a valid triangle, return its perimeter. |
| 68 | + * |
| 69 | + * Why consecutive triplets are enough: |
| 70 | + * - After sorting, if nums[i-2] + nums[i-1] > nums[i], then |
| 71 | + * (nums[i-2], nums[i-1], nums[i]) is valid and maximizes perimeter. |
| 72 | + * - If not, moving left reduces the largest side, so we must keep checking. |
| 73 | + * |
| 74 | + * Dry Run Example: |
| 75 | + * nums = [2, 1, 2] |
| 76 | + * After sort = [1, 2, 2] |
| 77 | + * Start from i = 2: |
| 78 | + * nums[0] + nums[1] = 1 + 2 = 3 > 2 → valid |
| 79 | + * perimeter = 1 + 2 + 2 = 5 |
| 80 | + * Answer = 5 |
| 81 | + * |
| 82 | + * Time Complexity: O(n log n) (due to sorting) |
| 83 | + * Space Complexity: O(1) |
| 84 | + */ |
| 85 | + |
| 86 | +class SolutionOptimized { |
| 87 | + public int largestPerimeter(int[] nums) { |
| 88 | + Arrays.sort(nums); |
| 89 | + int n = nums.length; |
| 90 | + for (int i = n - 1; i >= 2; i--) { |
| 91 | + if (nums[i - 2] + nums[i - 1] > nums[i]) { |
| 92 | + return nums[i] + nums[i - 1] + nums[i - 2]; |
| 93 | + } |
| 94 | + } |
| 95 | + return 0; |
| 96 | + } |
| 97 | +} |
0 commit comments