Skip to content

Commit 8c617af

Browse files
committed
D. J.:
- Added the leetcode problem and solution for 812
1 parent d356fba commit 8c617af

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@
270270
- [740 Delete and Earn](https://leetcode.com/problems/delete-and-earn/description/)
271271
- [746 Min Cost Climbing Stairs](https://leetcode.com/problems/min-cost-climbing-stairs/description/)
272272
- [790 Domino and Tromino Tiling](https://leetcode.com/problems/domino-and-tromino-tiling/description/)
273+
- [812 Largest Triangle Area](https://leetcode.com/problems/largest-triangle-area/description/)
273274
- [909 Snakes and Ladders](https://leetcode.com/problems/snakes-and-ladders/description/)
274275
- [918 Maximum Sum Circular Subarray](https://leetcode.com/problems/maximum-sum-circular-subarray/description/)
275276
- [931 Minimum Falling Path Sum](https://leetcode.com/problems/minimum-falling-path-sum/description/)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
"""Base class for all LeetCode Problems."""
6+
7+
def largestTriangleArea(self, points: List[List[int]]) -> float:
8+
"""
9+
Given an array of points on the X-Y plane points where points[i] = [xi, yi],
10+
return the area of the largest triangle that can be formed by any three
11+
different points. Answers within 10-5 of the actual answer will be accepted.
12+
"""
13+
# [x1, y1] [x2, y2] [x3, y3]
14+
# Area = 0.5 * |x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)|
15+
max_area = 0
16+
for i in range(len(points) - 2):
17+
for j in range(i + 1, len(points) - 1):
18+
for k in range(j + 1, len(points)):
19+
x1, y1 = points[i]
20+
x2, y2 = points[j]
21+
x3, y3 = points[k]
22+
new_area = 0.5 * abs(
23+
x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)
24+
)
25+
max_area = max(max_area, new_area)
26+
return max_area
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from typing import List
2+
3+
import pytest
4+
5+
from awesome_python_leetcode._812_largest_triangle_area import Solution
6+
7+
8+
@pytest.mark.parametrize(
9+
argnames=["points", "expected"],
10+
argvalues=[
11+
([[0, 0], [0, 1], [1, 0], [0, 2], [2, 0]], 2.0),
12+
([[1, 0], [0, 0], [0, 1]], 0.5),
13+
],
14+
)
15+
def test_func(points: List[List[int]], expected: List[int]):
16+
"""Tests the solution of a LeetCode problem."""
17+
triangle_area = Solution().largestTriangleArea(points)
18+
assert triangle_area == expected

0 commit comments

Comments
 (0)