-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
115 lines (99 loc) · 2.5 KB
/
main.cpp
File metadata and controls
115 lines (99 loc) · 2.5 KB
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
// Source: https://leetcode.com/problems/valid-triangle-number
// Title: Valid Triangle Number
// Difficulty: Medium
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Given an integer array `nums`, return 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: nums = [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
// ```
//
// **Example 2:**
//
// ```
// Input: nums = [4,2,3,4]
// Output: 4
// ```
//
// **Constraints:**
//
// - `1 <= nums.length <= 1000`
// - `0 <= nums[i] <= 1000`
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
// Sort, O(n^3)
class Solution {
public:
int triangleNumber(vector<int>& nums) {
int n = nums.size();
sort(nums.begin(), nums.end());
auto ans = 0;
for (auto i = 0; i < n; ++i) {
for (auto j = i + 1; j < n; ++j) {
for (auto k = j + 1; k < n; ++k) {
if (nums[i] + nums[j] > nums[k]) {
++ans;
}
}
}
}
return ans;
}
};
// Sort + Binary Search, O(n^2 logn)
//
// For each a & b, find maximum c such that a+b > c.
// Note that idx(c) > idx(b).
class Solution2 {
public:
int triangleNumber(vector<int>& nums) {
int n = nums.size();
sort(nums.begin(), nums.end());
auto ans = 0;
for (auto i = 0; i < n; ++i) {
for (auto j = i + 1; j < n; ++j) {
// find first invalid index
int k = lower_bound(nums.cbegin() + j, nums.cend(), nums[i] + nums[j]) - nums.cbegin();
ans += max(k - j - 1, 0);
}
}
return ans;
}
};
// Sort + Two Pointer, O(n^2)
//
// Use two pointer to find c
class Solution3 {
public:
int triangleNumber(vector<int>& nums) {
int n = nums.size();
sort(nums.begin(), nums.end());
auto ans = 0;
for (auto i = 0; i < n; ++i) {
auto k = i + 2;
for (auto j = i + 1; j < n; ++j) {
// find first invalid index
while (k < n && nums[i] + nums[j] > nums[k]) ++k;
ans += max(k - j - 1, 0);
}
}
return ans;
}
};
int main() {
auto nums = vector<int>({0, 0, 0});
cout << Solution3().triangleNumber(nums) << endl;
return 0;
}