Leetcode 611: Valid Triangle Number
Given an array of integers, return the number of triplets that can form a valid triangle. A valid triangle is formed when the sum of any two sides is greater than the third side.
Problem
Approach
Steps
Complexity
Input: An array of integers representing the side lengths of the triangle.
Example: nums = [1, 2, 3, 4]
Constraints:
• 1 <= nums.length <= 1000
• 0 <= nums[i] <= 1000
Output: Return the number of triplets from the array that can form a valid triangle.
Example: 3
Constraints:
• The result is an integer representing the number of valid triplets.
Goal: Determine how many triplets from the array satisfy the triangle inequality property.
Steps:
• Sort the array in non-decreasing order.
• For each element in the array, consider it as the largest side of the triangle.
• Use two pointers to find pairs of smaller sides that form a valid triangle with the largest side.
Goal: The array contains up to 1000 integers, and each integer is between 0 and 1000.
Steps:
• The array length is between 1 and 1000.
• Each integer is between 0 and 1000.
Assumptions:
• The numbers in the array represent side lengths of a potential triangle.
• Input: nums = [1, 2, 3, 4]
• Explanation: In this case, there are three valid triangles that can be formed by picking different combinations of side lengths.
Approach: The approach is to first sort the array and then use a two-pointer technique to find valid triplets that satisfy the triangle inequality.
Observations:
• Sorting the array helps us efficiently check for valid triplets by leveraging the triangle inequality.
• By sorting the array and iterating through it, we can reduce the time complexity and find the triplets in linear time.
Steps:
• 1. Sort the array in non-decreasing order.
• 2. For each element (starting from the third one), treat it as the largest side of the triangle.
• 3. Use two pointers to find valid pairs of sides that can form a triangle with the current largest side.
Empty Inputs:
• An empty input array.
Large Inputs:
• Handling arrays of size 1000 with large values.
Special Values:
• Arrays with duplicate values or elements that cannot form a triangle.
Constraints:
• Ensure that the algorithm works within the time limits for large arrays.
int triangleNumber(vector<int>& nums) {
sort(nums.begin(), nums.end());
int cnt = 0, n = nums.size();
for(int i = n - 1; i >= 0; i--)
for(int j = 0, k = i - 1; j < k;) {
if(nums[i] < nums[j] + nums[k]) {
cnt+= k - j;
k--;
} else j++;
}
return cnt;
}
1 : Function Declaration
int triangleNumber(vector<int>& nums) {
Defines the function `triangleNumber` that takes a vector of integers (`nums`) representing side lengths and returns an integer count of valid triangles that can be formed.
2 : Sort Input
sort(nums.begin(), nums.end());
Sorts the input array `nums` in ascending order to facilitate the triangle validation process. Sorting helps reduce the number of checks needed when evaluating potential triangles.
3 : Variable Initialization (cnt, n)
int cnt = 0, n = nums.size();
Initializes a counter `cnt` to 0, which will track the number of valid triangles. It also stores the size of the array `nums` in the variable `n`.
4 : Outer Loop (i)
for(int i = n - 1; i >= 0; i--)
The outer loop starts from the largest element in the sorted array (`i = n - 1`) and iterates downwards. This loop helps in checking the largest side of the triangle first.
5 : Inner Loop (j, k)
for(int j = 0, k = i - 1; j < k;) {
The inner loop uses two pointers, `j` and `k`, to check potential pairs of sides (`nums[j]`, `nums[k]`) with the current largest side (`nums[i]`). The pointers move towards each other to check all possible pairs.
6 : Triangle Validation Check
if(nums[i] < nums[j] + nums[k]) {
Checks if the current set of sides (`nums[i]`, `nums[j]`, `nums[k]`) forms a valid triangle based on the triangle inequality theorem (sum of two sides must be greater than the third side).
7 : Update Count
cnt+= k - j;
If the sides form a valid triangle, the count `cnt` is updated. The number of valid triangles is incremented by `k - j` because all pairs of sides between `j` and `k` form valid triangles with the current `i`.
8 : Move Pointer k
k--;
Decrements the pointer `k` to check the next possible smaller side for forming valid triangles.
9 : Move Pointer j
} else j++;
If the sides do not form a valid triangle, increment the pointer `j` to check the next possible larger side.
10 : Return Result
return cnt;
Returns the total count `cnt` of valid triangles that can be formed from the input side lengths.
Best Case: O(N^2)
Average Case: O(N^2)
Worst Case: O(N^2)
Description: The time complexity is O(N^2) due to the two-pointer approach after sorting the array.
Best Case: O(1)
Worst Case: O(1)
Description: The space complexity is O(1) since we only need a constant amount of space.
LeetCode Solutions Library / DSA Sheets / Course Catalog |
---|
comments powered by Disqus