Given an integer array nums, return 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: nums = [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

Example 2:

Input: nums = [4,2,3,4]
Output: 4

Constraints:

Solution

impl Solution {
    pub fn triangle_number(nums: Vec<i32>) -> i32 {
				// nums 각 원소에 대해 양수인지 확인하고, 양수인 원소만 필터링하여
				// 새로운 가변 벡터 nums 에 저장한다.( <= 정렬을 하기 위한 사전 준비)
        let mut nums = nums.iter().filter(|&n| *n > 0).collect::<Vec<_>>();
        if nums.len() < 3 {
            return 0;
        }

				// 정렬
        nums.sort_unstable();

        let mut answer = 0;
        **for** i in 0..nums.len() - 2 {
            // let mut k = i + 2;
            **for** j in i + 1..nums.len() - 1 {
								let mut k = j + 1;
								// 삼각형이 성립하지 않을 때까지만 반복함으로써
								// 약간의 효율성 고려
                **while** k < nums.len() && nums[i] + nums[j] > *nums[k] {
                    k += 1;
                }
								// 현재 선택한 i 와 j 에 대해 형성 가능한 삼각형의 갯수
                answer += k - j - 1;
            }
        }
        answer as i32
    }
}

Untitled

Untitled