Cover image for LeetCode Daily Problem P812 Largest Triangle Area

LeetCode Daily Problem P812 Largest Triangle Area


Timeline

timeline

2025-09-27

init

exhaustive enumeration

Problem:

This problem doesn’t require much thought; it mainly tests geometry knowledge. Just enumerate directly.
The formula for triangle area is:

S=12x1(y2y3)+x2(y3y1)+x3(y1y2)S = \frac{1}{2} \left| x_1(y_2 - y_3) + x_2(y_3 - y_1) + x_3(y_1 - y_2) \right|

Assume these three points: the first point i is on the left side of the points array, the second point’s array position is between the two points, and the third point is on the right.

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

#include <cmath>
#include <vector>

using std::vector;

class Solution {
public:
double largestTriangleArea(vector<vector<int>> &points) {

int n;
int x1, x2, x3;
int y1, y2, y3;
double max_area = 0;
double area;

n = points.size();
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
for (int k = j + 1; k < n; k++) {
x1 = points[i][0];
x2 = points[j][0];
x3 = points[k][0];
y1 = points[i][1];
y2 = points[j][1];
y3 = points[k][1];
area =
0.5 * std::fabs(x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2));
if (area > max_area) {
max_area = area;
}
}
}
}
return max_area;
}
};