| | | 1 | | // -------------------------------------------------------------------------------- |
| | | 2 | | // Copyright (C) 2026 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | | 3 | | // All Rights Reserved. |
| | | 4 | | // -------------------------------------------------------------------------------- |
| | | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | | 9 | | // known as Yevhenii Yeriemeieiv). |
| | | 10 | | // -------------------------------------------------------------------------------- |
| | | 11 | | |
| | | 12 | | namespace LeetCode.Algorithms.LargestTriangleArea; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed class LargestTriangleAreaBruteForce : ILargestTriangleArea |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Time complexity - O(n^3) |
| | | 19 | | /// Space complexity - O(1) |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="points"></param> |
| | | 22 | | /// <returns></returns> |
| | | 23 | | public double LargestTriangleArea(int[][] points) |
| | 2 | 24 | | { |
| | 2 | 25 | | var maxArea = 0.0; |
| | | 26 | | |
| | 2 | 27 | | var pointsLength = points.Length; |
| | | 28 | | |
| | 12 | 29 | | for (var i = 0; i < pointsLength - 2; i++) |
| | 4 | 30 | | { |
| | 4 | 31 | | var a = points[i]; |
| | | 32 | | |
| | 4 | 33 | | var aX = a[0]; |
| | 4 | 34 | | var aY = a[1]; |
| | | 35 | | |
| | 22 | 36 | | for (var j = i + 1; j < pointsLength - 1; j++) |
| | 7 | 37 | | { |
| | 7 | 38 | | var b = points[j]; |
| | | 39 | | |
| | 7 | 40 | | var bX = b[0]; |
| | 7 | 41 | | var bY = b[1]; |
| | | 42 | | |
| | 36 | 43 | | for (var k = j + 1; k < pointsLength; k++) |
| | 11 | 44 | | { |
| | 11 | 45 | | var c = points[k]; |
| | | 46 | | |
| | 11 | 47 | | var cX = c[0]; |
| | 11 | 48 | | var cY = c[1]; |
| | | 49 | | |
| | 11 | 50 | | var area = CalculateArea(aX, aY, bX, bY, cX, cY); |
| | | 51 | | |
| | 11 | 52 | | maxArea = double.Max(maxArea, area); |
| | 11 | 53 | | } |
| | 7 | 54 | | } |
| | 4 | 55 | | } |
| | | 56 | | |
| | 2 | 57 | | return maxArea; |
| | 2 | 58 | | } |
| | | 59 | | |
| | | 60 | | private static double CalculateArea(int aX, int aY, int bX, int bY, int cX, int cY) |
| | 11 | 61 | | { |
| | 11 | 62 | | return int.Abs((aX * (bY - cY)) + (bX * (cY - aY)) + (cX * (aY - bY))) / 2.0; |
| | 11 | 63 | | } |
| | | 64 | | } |