Link: https://leetcode.com/problems/largest-triangle-area/
Solution:
DSA: math
Intuition
Not really much to this problem. Just try all points with a triple for loop with the formula for calculating area with coordinates:
area = abs(0.5*(x1*(y2-y3)+ x2*(y3-y1) + x3*(y1-y2))
Implementation
Visual
Review 1
You need the formula. Memorize the above. x1, x2, x3 is the product outside the brackets and inside the brackets its (next_y - next_next_y)
. For x2, the next_y
is y3 and next_next_y
wraps around to y1. The ordering of y’s is important due to the non-commutative property of subtraction.