-
Notifications
You must be signed in to change notification settings - Fork 23
/
Triangle Count.java
executable file
·122 lines (108 loc) · 3.35 KB
/
Triangle Count.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
M
1516683660
tags: Array
其实也就是3sum的变形, 或者而说2sum的变形. 主要用2 pointers来做.
注意, 在选index时候每次定好一个 [0 ~ i], 在这里面找点start, end, 然后i 来组成triangle.
Note巧妙点:
在此之中, 如果一种start/end/i 符合, 那么从这个[start~end]中, 大于start的都可以, 所以我们count+= end-start.
反而言之, <end的其他index, 就不一定能符合nums[start] + nums[end] > nums[i]
```
/*
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].
*/
/*
Thoughts:
Follow the triangle rules, we'll sort, and use two pointers to count all possible triangles.
When sorted, if at one point moving out of range, any point going beyound will be out of range.
*/
class Solution {
public int triangleNumber(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int count = 0;
Arrays.sort(nums);
for (int i = 2; i < nums.length; i++) {
int start = 0;
int end = i - 1;
while (start < end) {
if (nums[start] + nums[end] > nums[i]) {
count += end - start; // any num[index] where start < index < end, can replace start and fit.
end--;
} else {
start++;
}
}
}
return count;
}
}
/*
Given an array of integers, how many three numbers can be found in the array,
so that we can build an triangle whose three edges length is the three numbers that we find?
Example
Given array S = [3,4,6,7], return 3. They are:
[3,4,6]
[3,6,7]
[4,6,7]
Given array S = [4,4,4,4], return 4. They are:
[4(1),4(2),4(3)]
[4(1),4(2),4(4)]
[4(1),4(3),4(4)]
[4(2),4(3),4(4)]
Tags Expand
Two Pointers LintCode Copyright
*/
/*
Thoughts:
Pick 3 integers that fits the condition:
A + B > C
B + C > A
A + C > B
If we sort the input, then we know A <= B <= C, so we can remove 2 conditoins above and only have:
A + B > C
That is, Pick one C, and pick two integers A,B in front. Similar to TWO SUM II.
Have a fixed C as target, and find A + B > target in the remaining array on left of C.
How about just use 2 pointers left, right, and compare with a C (s[i] in for loop)
Time: O(n^2)
Note: don't forget to sort
*/
public class Solution {
/**
* @param S: A list of integers
* @return: An integer
*/
public int triangleCount(int S[]) {
if (S == null || S.length == 0) {
return 0;
}
Arrays.sort(S);
int count = 0;
for (int i = 0; i < S.length; i++) {
int left = 0;
int right = i - 1; //at least 1 step left from C
while (left < right){
if (S[left] + S[right] > S[i]) {
count += (right - left);
right--;
} else {//(S[left] + S[right] <= S[i])
left++;
}
}
}
return count;
}
}
```