-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathsolution.cpp
More file actions
27 lines (26 loc) · 960 Bytes
/
Copy pathsolution.cpp
File metadata and controls
27 lines (26 loc) · 960 Bytes
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
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
int triangleNumber(vector<int>& nums) {
int n = nums.size();
if (n < 3) return 0;
sort(nums.begin(), nums.end()); // sort ascending
int count = 0;
// k is index for the largest side (c)
for (int k = n - 1; k >= 2; --k) {
int i = 0, j = k - 1; // two pointers for a and b
while (i < j) {
// if smallest + current largest > c, then all values from i..j-1 with j work
if (nums[i] + nums[j] > nums[k]) {
count += (j - i); // add all pairs (i..j-1, j)
--j; // try smaller b
} else {
++i; // need larger a
}
}
}
return count;
}
};