forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalid-triangle-number.cpp
45 lines (43 loc) · 1.11 KB
/
valid-triangle-number.cpp
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
// Time: O(n^2)
// Space: O(1)
class Solution {
public:
int triangleNumber(vector<int>& nums) {
int result = 0;
sort(begin(nums), end(nums));
for (int i = size(nums) - 1; i >= 2; --i) {
int left = 0, right = i - 1;
while (left < right) {
if (nums[left] + nums[right] > nums[i]) {
result += right - left;
--right;
} else {
++left;
}
}
}
return result;
}
};
// Time: O(n^2)
// Space: O(1)
class Solution2 {
public:
int triangleNumber(vector<int>& nums) {
int result = 0;
sort(nums.begin(), nums.end());
for (int i = 0; i + 2 < nums.size(); ++i) {
if (nums[i] == 0) {
continue;
}
auto k = i + 2;
for (int j = i + 1; j + 1 < nums.size(); ++j) {
while (k < nums.size() && nums[i] + nums[j] > nums[k]) {
++k;
}
result += k - j - 1;
}
}
return result;
}
};