|
| 1 | +/** |
| 2 | + * Title: Valid Triangle Number |
| 3 | + * Link: https://leetcode.com/problems/valid-triangle-number/ |
| 4 | + * |
| 5 | + * Problem: |
| 6 | + * Given an integer array nums, return the number of triplets chosen from the array |
| 7 | + * that can make triangles if we take them as side lengths of a triangle. |
| 8 | + * |
| 9 | + * Conditions for a valid triangle: |
| 10 | + * - The sum of any two sides must be greater than the third side. |
| 11 | + * - So, for three numbers a, b, c: |
| 12 | + * a + b > c |
| 13 | + * a + c > b |
| 14 | + * b + c > a |
| 15 | + * |
| 16 | + * --------------------------------------------------------------------------- |
| 17 | + * Approach 1: Brute Force (3 nested loops) |
| 18 | + * --------------------------------------------------------------------------- |
| 19 | + * Idea: |
| 20 | + * - Check all possible triplets (i, j, k). |
| 21 | + * - For each triplet, test the triangle inequality conditions. |
| 22 | + * |
| 23 | + * Dry Run: |
| 24 | + * nums = [2, 2, 3, 4] |
| 25 | + * Triplets: |
| 26 | + * (2,2,3) -> valid |
| 27 | + * (2,3,4) -> valid |
| 28 | + * (2,2,4) -> not valid |
| 29 | + * (2,3,4) -> valid |
| 30 | + * Total = 3 |
| 31 | + * |
| 32 | + * Time Complexity: O(n^3) |
| 33 | + * Space Complexity: O(1) |
| 34 | + */ |
| 35 | +class SolutionBruteForce { |
| 36 | + public int triangleNumber(int[] nums) { |
| 37 | + int count = 0; |
| 38 | + for (int i = 0; i < nums.length - 2; i++) { |
| 39 | + for (int j = i + 1; j < nums.length - 1; j++) { |
| 40 | + for (int k = j + 1; k < nums.length; k++) { |
| 41 | + int a = nums[i]; |
| 42 | + int b = nums[j]; |
| 43 | + int c = nums[k]; |
| 44 | + if ((a + b > c) && (a + c > b) && (b + c > a)) { |
| 45 | + count++; |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + return count; |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * --------------------------------------------------------------------------- |
| 56 | + * Approach 2: Sort + Triple Nested Loop |
| 57 | + * --------------------------------------------------------------------------- |
| 58 | + * Idea: |
| 59 | + * - Sort the array first. |
| 60 | + * - Because sorted array ensures nums[i] <= nums[j] <= nums[k], |
| 61 | + * only need to check: nums[i] + nums[j] > nums[k]. |
| 62 | + * |
| 63 | + * Dry Run: |
| 64 | + * nums = [2, 2, 3, 4] -> sorted |
| 65 | + * i=0, j=1, k=2 -> 2+2 > 3 ✅ |
| 66 | + * i=0, j=1, k=3 -> 2+2 > 4 ❌ |
| 67 | + * i=0, j=2, k=3 -> 2+3 > 4 ✅ |
| 68 | + * i=1, j=2, k=3 -> 2+3 > 4 ✅ |
| 69 | + * Total = 3 |
| 70 | + * |
| 71 | + * Time Complexity: O(n^3) (sorting O(n log n) negligible) |
| 72 | + * Space Complexity: O(1) |
| 73 | + */ |
| 74 | +class SolutionSortedTriple { |
| 75 | + public int triangleNumber(int[] nums) { |
| 76 | + Arrays.sort(nums); |
| 77 | + int count = 0; |
| 78 | + for (int i = 0; i < nums.length - 2; i++) { |
| 79 | + for (int j = i + 1; j < nums.length - 1; j++) { |
| 80 | + for (int k = j + 1; k < nums.length; k++) { |
| 81 | + if (nums[i] + nums[j] > nums[k]) { |
| 82 | + count++; |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + return count; |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * --------------------------------------------------------------------------- |
| 93 | + * Approach 3: Sort + Two Pointers (Optimized) |
| 94 | + * --------------------------------------------------------------------------- |
| 95 | + * Idea: |
| 96 | + * - Sort the array. |
| 97 | + * - Fix the largest side (nums[i]). |
| 98 | + * - Use two pointers (j, k) to count valid pairs. |
| 99 | + * If nums[j] + nums[k] > nums[i], then all elements between j..k-1 with nums[k] |
| 100 | + * also form valid triangles. So add (k-j) to count. |
| 101 | + * |
| 102 | + * Dry Run: |
| 103 | + * nums = [2, 2, 3, 4] -> sorted |
| 104 | + * i=3 (nums[i]=4), j=0, k=2 |
| 105 | + * nums[0]+nums[2]=5 > 4 -> count += 2 (k-j=2), k-- |
| 106 | + * nums[0]+nums[1]=4 !> 4 -> j++ |
| 107 | + * Done. Count=3 |
| 108 | + * |
| 109 | + * Time Complexity: O(n^2) |
| 110 | + * Space Complexity: O(1) |
| 111 | + */ |
| 112 | +class SolutionOptimized { |
| 113 | + public int triangleNumber(int[] nums) { |
| 114 | + Arrays.sort(nums); |
| 115 | + int count = 0; |
| 116 | + for (int i = nums.length - 1; i >= 2; i--) { |
| 117 | + int j = 0, k = i - 1; |
| 118 | + while (j < k) { |
| 119 | + if (nums[j] + nums[k] > nums[i]) { |
| 120 | + count += k - j; |
| 121 | + k--; |
| 122 | + } else { |
| 123 | + j++; |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + return count; |
| 128 | + } |
| 129 | +} |
0 commit comments