← Home

812. Largest Triangle Area

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
812

Let's make this one less mysterious. For 812. Largest Triangle Area, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

Guide

When?

Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are combinationUtil, getCombinations, combination, largestTriangleArea, indices.

Guide

Why?

The solution works because it narrows the problem until every update has a clear reason to exist.

  • The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Initialize the memory or helper structure.
  2. Process candidates in the order the invariant expects.
  3. Update the answer only when the current state is valid.
  4. Return the value that represents the fully processed input.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 152 ms, faster than 5.31% of C++ online submissions for Largest Triangle Area.
02//Memory Usage: 56.1 MB, less than 50.00% of C++ online submissions for Largest Triangle Area.
03class Solution {
04public:
05    void combinationUtil(std::vector<int>& arr, std::vector<int>& combination, std::vector<std::vector<int>>& combinations, int start, int end, int index, int r) { 
06        if (index == r) { 
07            combinations.push_back(combination);
08            return;
09        }
10
11        //r - index : number of remaining cells in "combination" to be filled
12        //end - i + 1 : the size of remaining cells in "arr"
13        //stop when we don't have enough elements to fill the remaining part of "combination"
14        for (int i = start; i <= end && end - i + 1 >= r - index; i++) {
15            combination[index] = arr[i];
16            combinationUtil(arr, combination, combinations, i+1, end, index+1, r);
17        } 
18    } 
19
20    void getCombinations(std::vector<int>& arr, std::vector<std::vector<int>>& combinations, int n, int r) {
21        std::vector<int> combination(r);
22        combinationUtil(arr, combination, combinations, 0, n-1, 0, r); 
23    } 
24    
25    double largestTriangleArea(vector<vector<int>>& points) {
26        int n = points.size(), r = 3;
27        vector<int> indices(n);
28        vector<vector<int>> combs;
29        //min of double(negative)
30        double max_area = std::numeric_limits<double>::lowest();
31        
32        iota(indices.begin(), indices.end(), 0);
33        
34        getCombinations(indices, combs, n, r);
35        // cout << "n: " << n << ", " << combs.size() << endl;
36        
37        for(vector<int>& comb : combs){
38            // cout << comb[0] << ", " << comb[1] << ", " << comb[2] << endl;
39            int curx = points[comb[0]][0];
40            vector<int> p0 = points[comb[0]];
41            vector<int> p1 = points[comb[1]];
42            vector<int> p2 = points[comb[2]];
43            
44            //x coordinates are same
45            if((p0[0] == p1[0]) && (p1[0] == p2[0])){
46                continue;
47            }
48            //on same line
49            //(y1-y0)/(x1-x0) == (y2-y1)/(x2-x1)
50            //(y1-y0)*(x2-x1) == (y2-y1)*(x1-x0)
51            if((p1[1] - p0[1]) * (p2[0] - p1[0]) == 
52              (p2[1] - p1[1]) * (p1[0] - p0[0])){
53                continue;
54            }
55            
56            //calculate area using Heron's formula
57            double a = sqrt(pow(p1[0] - p0[0], 2) + pow(p1[1] - p0[1], 2));
58            double b = sqrt(pow(p1[0] - p2[0], 2) + pow(p1[1] - p2[1], 2));
59            double c = sqrt(pow(p2[0] - p0[0], 2) + pow(p2[1] - p0[1], 2));
60            double s = (a + b + c)/2;
61            double area = sqrt(s * (s - a) * (s - b) * (s - c));
62            max_area = max(max_area, area);
63        }
64        
65        return max_area;
66    }
67};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.