812. Largest Triangle area

Source: Internet
Author: User

You had a list of points in the plane. Return the area of the largest triangle so can be formed by any 3 of the points.

Example:
Input:points = [[0,0],[0,1],[1,0],[0,2],[2,0]]
Output:2
Explanation:
The five Points is show in the figure below. The red triangle is the largest.

Notes:

3 <= points.length <= 50.No points will be duplicated. -50 <= points[i][j] <= 50.Answers within 10^-6 of the true value will be accepted as correct.

Solution1:

class Solution:    def largestTriangleArea(self, points):        """        :type points: List[List[int]]        :rtype: float        """        def solve(a,b,c):            return 0.5*abs(a[0]*b[1] + b[0]*c[1] + c[0]*a[1] - a[0]*c[1] - b[0]*a[1] - c[0]*b[1])        l = len(points)        res = 0        for a in range(l):            for b in range(a+1,l):                for c in range(b+1,l):                    res = max(solve(points[a],points[b],points[c]),res)        return res

Solution2: (use Itertools.combinations to simplify the code, which returns all combinations of the specified number in the list)

class Solution(object):    def largestTriangleArea(self, points):        """        :type points: List[List[int]]        :rtype: float        """        return max(0.5*abs((y1-x1)*(z2-x2)-(z1-x1)*(y2-x2)) for (x1,x2),(y1,y2),(z1,z2) in itertools.combinations(points,3))

812. Largest Triangle area

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.