Skip to content

Commit 4b93668

Browse files
authored
Create largest-triangle-area.cpp
1 parent 74b870e commit 4b93668

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

C++/largest-triangle-area.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Time: O(n^3)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
double largestTriangleArea(vector<vector<int>>& points) {
7+
double result = 0.0;
8+
for (int i = 0; i < points.size() - 2; ++i) {
9+
for (int j = i + 1; j < points.size() - 1; ++j) {
10+
for (int k = j + 1; k < points.size(); ++k) {
11+
result = max(result,
12+
0.5 * abs(points[i][0] * points[j][1] +
13+
points[j][0] * points[k][1] +
14+
points[k][0] * points[i][1] -
15+
points[j][0] * points[i][1] -
16+
points[k][0] * points[j][1] -
17+
points[i][0] * points[k][1]));
18+
}
19+
}
20+
}
21+
return result;
22+
}
23+
};

0 commit comments

Comments
 (0)