forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidTriangleNumber.java
47 lines (39 loc) · 1.12 KB
/
ValidTriangleNumber.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.leetcode.arrays;
import java.util.Arrays;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* Level: Medium
* Problem Link: https://leetcode.com/problems/valid-triangle-number/
* Problem Description:
* 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.
* <p>
* 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
* <p>
* Note:
* The length of the given array won't exceed 1000.
* The integers in the given array are in the range of [0, 1000].
*
* @author rampatra
* @since 2019-08-07
*/
public class ValidTriangleNumber {
public static int triangleNumber(int[] nums) {
int l = 0;
int r = nums.length - 1;
int noOfTriangles = 0;
Arrays.sort(nums);
// todo
return noOfTriangles;
}
public static void main(String[] args) {
assertEquals(3, triangleNumber(new int[]{2, 2, 3, 4}));
}
}