Count the number of possible triangles
https://leetcode.com/problems/valid-triangle-number
https://discuss.leetcode.com/topic/92110/java-solution-3-pointers
Approach #3 Linear Scan
X. Approach #2 Using Binary Search
X. https://leetcode.com/articles/valid-triangle-number/
X. Videoshttps://leetcode.com/problems/valid-triangle-number
Given an array consists of non-negative integers, your task is to count the number of triplets chosen from the array that can make triangles if we take them as side lengths of a triangle.
Example 1:
Input: [2,2,3,4] Output: 3 Explanation: Valid combinations are: 2,3,4 (using the first 2) 2,3,4 (using the second 2) 2,2,3
Note:
- The length of the given array won't exceed 1000.
- The integers in the given array are in the range of [0, 1000].
https://discuss.leetcode.com/topic/92110/java-solution-3-pointers
Assume
a
is the longest edge, b
and c
are shorter ones, to form a triangle, they need to satisfy len(b) + len(c) > len(a)
. public int triangleNumber(int[] nums) {
int result = 0;
if (nums.length < 3) return result;
Arrays.sort(nums);
for (int i = 2; i < nums.length; i++) {
int left = 0, right = i - 1;
while (left < right) {
if (nums[left] + nums[right] > nums[i]) {
result += (right - left);
right--;
}
else {
left++;
}
}
}
return result;
}
https://discuss.leetcode.com/topic/92099/java-o-n-2-time-o-1-spacepublic static int triangleNumber(int[] A) {
Arrays.sort(A);
int count = 0, n = A.length;
for (int i=n-1;i>=2;i--) {
int l = 0, r = i-1;
while (l < r) {
if (A[l] + A[r] > A[i]) {
count += r-l;
r--;
}
else l++;
}
}
return count;
}
https://leetcode.com/articles/valid-triangle-number/Approach #3 Linear Scan
once we sort the given array, we need to find the right limit of the index for a pair of indices chosen to find the of elements satisfying for the triplet to form a valid triangle.
We can find this right limit by simply traversing the index 's values starting from the index for a pair chosen and stopping at the first value of not satisfying the above inequality. Again, the of elements satisfying for the pair of indices chosen is given by as discussed in the last approach.
Further, as discussed in the last approach, when we choose a higher value of index for a particular chosen, we need not start from the index . Instead, we can start off directly from the value of where we left for the last index . This helps to save redundant computations.
- Time complexity : . Loop of and will be executed times in total, because, we do not reinitialize the value of for a new value of chosen(for the same ). Thus the complexity will be O(n^2+n^2)=O(n^2).
- Space complexity : . Sorting takes O(logn) space.
public int triangleNumber(int[] nums) { int count = 0; Arrays.sort(nums); for (int i = 0; i < nums.length - 2; i++) { int k = i + 2; for (int j = i + 1; j < nums.length - 1 && nums[i] != 0; j++) { while (k < nums.length && nums[i] + nums[j] > nums[k]) k++; count += k - j - 1; } } return count; }
X. Approach #2 Using Binary Search
- Time complexity : . In worst case inner loop will take (binary search applied times).
- Space complexity : . Sorting takes space.
int binarySearch(int nums[], int l, int r, int x) { while (r >= l && r < nums.length) { int mid = (l + r) / 2; if (nums[mid] >= x) r = mid - 1; else l = mid + 1; } return l; } public int triangleNumber(int[] nums) { int count = 0; Arrays.sort(nums); for (int i = 0; i < nums.length - 2; i++) { int k = i + 2; for (int j = i + 1; j < nums.length - 1 && nums[i] != 0; j++) { k = binarySearch(nums, k, nums.length - 1, nums[i] + nums[j]); count += k - j - 1; } } return count; }
X. https://leetcode.com/articles/valid-triangle-number/
public int triangleNumber(int[] nums) { int count = 0; for (int i = 0; i < nums.length - 2; i++) { for (int j = i + 1; j < nums.length - 1; j++) { for (int k = j + 1; k < nums.length; k++) { if (nums[i] + nums[j] > nums[k] && nums[i] + nums[k] > nums[j] && nums[j] + nums[k] > nums[i]) count++; } } } I return count; }
LeetCode 611. Valid Triangle Number - 花花酱 刷题找工作 EP43