21
21
* 2,2,3
22
22
* <p>
23
23
* Note:
24
- * The length of the given array won't exceed 1000.
25
- * The integers in the given array are in the range of [0, 1000].
24
+ * - The length of the given array won't exceed 1000.
25
+ * - The integers in the given array are in the range of [0, 1000].
26
+ * - Triangle Property: Sum of any 2 sides must be greater than the 3rd side.
26
27
*
27
28
* @author rampatra
28
29
* @since 2019-08-07
29
30
*/
30
31
public class ValidTriangleNumber {
31
32
32
- public static int triangleNumber (int [] nums ) {
33
- int l = 0 ;
34
- int r = nums .length - 1 ;
33
+ /**
34
+ * Time complexity : O(n^2 log n). In worst case, the inner loop will take n log n (binary search applied n times).
35
+ * Space complexity : O(log n). Sorting takes O(log n) space.
36
+ * Runtime: <a href="https://leetcode.com/submissions/detail/250225175/">13 ms</a>.
37
+ *
38
+ * @param nums
39
+ * @return
40
+ */
41
+ public static int triangleNumberUsingBinarySearch (int [] nums ) {
35
42
int noOfTriangles = 0 ;
36
43
37
44
Arrays .sort (nums );
38
45
39
- // todo
46
+ for (int i = 0 ; i < nums .length - 2 ; i ++) {
47
+ int k = i + 2 ;
48
+ for (int j = i + 1 ; j < nums .length - 1 ; j ++) {
49
+ k = binarySearch (nums , k , nums .length - 1 , nums [i ] + nums [j ]);
50
+ if (k - j - 1 > 0 ) {
51
+ noOfTriangles += k - j - 1 ;
52
+ }
53
+ }
54
+ }
55
+
56
+ return noOfTriangles ;
57
+ }
58
+
59
+ private static int binarySearch (int [] nums , int low , int high , int num ) {
60
+ while (low <= high ) {
61
+ int mid = (low + high ) / 2 ;
62
+ if (nums [mid ] < num ) {
63
+ low = mid + 1 ;
64
+ } else {
65
+ high = mid - 1 ;
66
+ }
67
+ }
68
+
69
+ return low ;
70
+ }
71
+
72
+ /**
73
+ * The concept is simple. For each pair (i,j), find the value of k such that nums[i] + nums[j] > nums[k] (as per
74
+ * triangle property). Once we find k then we can form k- j - 1 triangles.
75
+ *
76
+ * Time Complexity: O(n^2) Loop of k and j will be executed O(n^2) times in total, because, we do
77
+ * not reinitialize the value of k for a new value of j chosen(for the same i). Thus, the complexity
78
+ * will be O(n^2 + n^2) = O(n^2).
79
+ * Space Complexity: O(log n). Sorting takes O(log n) space.
80
+ * Runtime: <a href="https://leetcode.com/submissions/detail/250239099/">5 ms</a>.
81
+ *
82
+ * @param nums
83
+ * @return
84
+ */
85
+ public static int triangleNumber (int [] nums ) {
86
+ int noOfTriangles = 0 ;
87
+ Arrays .sort (nums );
88
+
89
+ for (int i = 0 ; i < nums .length - 2 ; i ++) {
90
+ int k = i + 2 ;
91
+ for (int j = i + 1 ; j < nums .length - 1 ; j ++) {
92
+ while (k < nums .length && nums [i ] + nums [j ] > nums [k ]) {
93
+ k ++;
94
+ }
95
+ if (k - j - 1 > 0 ) {
96
+ noOfTriangles += k - j - 1 ;
97
+ }
98
+ }
99
+ }
40
100
41
101
return noOfTriangles ;
42
102
}
43
103
44
104
public static void main (String [] args ) {
105
+ assertEquals (0 , triangleNumberUsingBinarySearch (new int []{}));
106
+ assertEquals (0 , triangleNumberUsingBinarySearch (new int []{1 }));
107
+ assertEquals (3 , triangleNumberUsingBinarySearch (new int []{2 , 2 , 3 , 4 }));
108
+ assertEquals (0 , triangleNumberUsingBinarySearch (new int []{0 , 1 , 0 , 1 }));
109
+ assertEquals (7 , triangleNumberUsingBinarySearch (new int []{1 , 2 , 3 , 4 , 5 , 6 }));
110
+
111
+ assertEquals (0 , triangleNumber (new int []{}));
112
+ assertEquals (0 , triangleNumber (new int []{1 }));
45
113
assertEquals (3 , triangleNumber (new int []{2 , 2 , 3 , 4 }));
114
+ assertEquals (0 , triangleNumber (new int []{0 , 1 , 0 , 1 }));
115
+ assertEquals (7 , triangleNumber (new int []{1 , 2 , 3 , 4 , 5 , 6 }));
46
116
}
47
- }
117
+ }
0 commit comments