Given an array A of positive lengths, return the largest perimeter of a triangle with non-zero area, formed from 3 of these lengths.
If it is impossible to form any triangle of non-zero area, return 0.
 



Example 1:
Input: [2,1,2]
Output: 5


Example 2:
Input: [1,2,1]
Output: 0


Example 3:
Input: [3,2,3,4]
Output: 10


Example 4:
Input: [3,6,2,3]
Output: 8

 
Note:

3 <= A.length <= 10000
1 <= A[i] <= 10^6

Difficulty: EASY
Tags: Sorting

Data structures

Mathematics

Greedy algorithms
Skill types: Sorting

Data structures

Greedy algorithms

Starter code:
class Solution:
    def largestPerimeter(self, A: List[int]) -> int:
        

Solutions:
class Solution:

	def largestPerimeter(self, A: List[int]) -> int:
		A.sort(reverse=True)
		la = len(A)
		for i in range(la - 2):
			if A[i] < A[i + 1] + A[i + 2]:
				return A[i] + A[i + 1] + A[i + 2]
		return 0


class Solution:

	def largestPerimeter(self, A: List[int]) -> int:
		sortedA = sorted(A)
		for i in range(len(sortedA) - 3, -1, -1):
			if sortedA[i + 2] < sortedA[i] + sortedA[i + 1]:
				return sum(sortedA[i:i + 3])
		return 0


class Solution:

	def largestPerimeter(self, A: List[int]) -> int:
		A.sort()
		return ([0] + [a + b + c for (a, b, c) in zip(A, A[1:], A[2:]) if c < a + b])[-1]


class Solution:

	def largestPerimeter(self, A: List[int]) -> int:
		A.sort()
		for i in range(len(A) - 1, 1, -1):
			len1 = A[i]
			len2 = A[i - 1]
			len3 = A[i - 2]
			if self.check_triangle(len1, len2, len3):
				return len1 + len2 + len3
		return 0

	def check_triangle(self, len1, len2, len3):
		if len1 + len2 <= len3 or len2 + len3 <= len1 or len1 + len3 <= len2:
			return False
		return True


class Solution:

	def largestPerimeter(self, A: List[int]) -> int:
		A.sort(reverse=True)
		i = 0
		while i < len(A) - 2:
			if A[i] < A[i + 1] + A[i + 2]:
				return A[i] + A[i + 1] + A[i + 2]
			else:
				i = i + 1
		return 0


class Solution:

	def largestPerimeter(self, A: List[int]) -> int:
		p = 0
		A.sort()
		for i in range(len(A) - 2):
			if A[i] + A[i + 1] > A[i + 2]:
				p = A[i] + A[i + 1] + A[i + 2]
		return p


class Solution:

	def largestPerimeter(self, A: List[int]) -> int:
		A = sorted(A)
		perimeter = 0
		for i in range(len(A) - 2):
			(s1, s2, s3) = (A[i], A[i + 1], A[i + 2])
			p = s1 + s2 + s3
			if s1 + s2 > s3 and s2 + s3 > s1 and (s1 + s3 > s2):
				perimeter = max(perimeter, p)
		return perimeter


class Solution:

	def largestPerimeter(self, A: List[int]) -> int:
		result = 0
		A = sorted(A)
		for i in range(len(A) - 2):
			(a, b, c) = (A[i], A[i + 1], A[i + 2])
			if a + b > c and a + c > b and (b + c > a):
				result = max(result, a + b + c)
		return result


class Solution:

	def largestPerimeter(self, A: List[int]) -> int:
		A.sort()
		for i in range(len(A) - 1, 1, -1):
			a = A[i]
			b = A[i - 1]
			c = A[i - 2]
			if a < b + c:
				return a + b + c
		return 0


import numpy as np

class Solution:

	def largestPerimeter(self, A: List[int]) -> int:
		valid_area = lambda x, y, z: True if x < y + z else False
		A = sorted(A, reverse=True)
		for a in range(2, len(A)):
			if valid_area(A[a - 2], A[a - 1], A[a]):
				return np.sum([A[a - 2], A[a - 1], A[a]])
		return 0


class Solution:

	def largestPerimeter(self, A: List[int]) -> int:
		A.sort()
		ans = 0
		for i in range(1, len(A) - 1):
			if A[i - 1] + A[i] <= A[i + 1]:
				continue
			else:
				p = sum(A[i - 1:i + 2])
				ans = max(ans, p)
		return ans


class Solution:

	def largestPerimeter(self, A: List[int]) -> int:
		if len(A) < 3:
			return 0
		A.sort()
		counter = 3
		perimeter = 0
		while counter <= len(A):
			a = A[counter - 3]
			b = A[counter - 2]
			c = A[counter - 1]
			if a + b <= c:
				counter += 1
				continue
			elif b + c <= a:
				counter += 1
				continue
			elif c + a <= b:
				counter += 1
				continue
			perimeter = a + b + c
			if a + b + c > perimeter:
				perimeter = a + b + c
			counter += 1
		return perimeter


class Solution:

	def largestPerimeter(self, A: List[int]) -> int:
		A.sort()
		first = 0
		second = 1
		mx = 0
		for third in range(2, len(A)):
			if A[first] + A[second] > A[third]:
				mx = max(mx, A[first] + A[second] + A[third])
			first += 1
			second += 1
		return mx


class Solution:

	def largestPerimeter(self, A: List[int]) -> int:
		A.sort()
		a = len(A) - 1
		while a >= 2:
			if A[a] < A[a - 1] + A[a - 2]:
				return A[a] + A[a - 1] + A[a - 2]
			else:
				a -= 1
		return 0


class Solution:

	def largestPerimeter(self, A: List[int]) -> int:
		A.sort(reverse=True)
		n = len(A)
		for i in range(n - 2):
			for j in range(i + 1, n - 1):
				if A[j] <= A[i] // 2:
					break
				for k in range(j + 1, n):
					if A[i] < A[j] + A[k]:
						return A[i] + A[j] + A[k]
		return 0


class Solution:

	def largestPerimeter(self, A: List[int]) -> int:
		A = sorted(A)
		i = len(A) - 1
		while i >= 2:
			if A[i - 2] + A[i - 1] > A[i]:
				return A[i - 2] + A[i - 1] + A[i]
			i -= 1
		return 0


class Solution:

	def largestPerimeter(self, A: List[int]) -> int:
		A.sort(reverse=True)
		for (a, b, c) in zip(A, A[1:], A[2:]):
			if a < b + c:
				return a + b + c
			else:
				continue
		return 0


class Solution:

	def largestPerimeter(self, A: List[int]) -> int:
		A.sort(reverse=True)
		m = 0
		for i in range(len(A) - 2):
			if A[i] + A[i + 1] > A[i + 2] and A[i + 2] + A[i + 1] > A[i] and (A[i + 2] + A[i] > A[i + 1]):
				x = A[i:i + 3]
				if sum(x) > m:
					m = sum(x)
		return m


class Solution:

	def largestPerimeter(self, A: List[int]) -> int:

		def check(x, y, z):
			if x + y > z and y + z > x and (x + z > y):
				return True
			else:
				False
		A = sorted(A)
		A = A[::-1]
		x = A
		print(x)
		lar = 0
		for i in range(len(x) - 2):
			if check(x[i], x[i + 1], x[i + 2]):
				lar = max(lar, x[i] + x[i + 1] + x[i + 2])
		return lar


class Solution:

	def _isTriangle(self, queue):
		return queue[0] < queue[1] + queue[2]

	def largestPerimeter(self, A: List[int]) -> int:
		A.sort(key=lambda x: -x)
		nums = []
		for num in A:
			nums.append(num)
			if len(nums) == 3:
				if self._isTriangle(nums):
					return sum(nums)
				nums.pop(0)
		return 0


class Solution:

	def largestPerimeter(self, A: List[int]) -> int:
		n = len(A)
		A.sort(reverse=True)
		a = 0
		while a < n:
			b = a + 1
			c = a + 2
			while b < n and c < n:
				if A[b] + A[c] > A[a]:
					return A[a] + A[b] + A[c]
				else:
					b += 1
					c += 1
			a += 1
		return 0


class Solution:

	def largestPerimeter(self, A: List[int]) -> int:
		if len(A) < 3:
			return 0
		maxer = 0
		pointer1 = len(A) - 3
		pointer2 = len(A) - 2
		pointer3 = len(A) - 1
		A.sort()
		if len(A) == 3:
			if A[0] + A[1] > A[2]:
				return sum(A)
			else:
				return 0
		while pointer3 >= 2:
			if A[pointer1] + A[pointer2] > A[pointer3]:
				return A[pointer1] + A[pointer3] + A[pointer2]
			elif pointer1 == 0:
				pointer3 -= 1
				pointer2 = pointer3 - 1
				pointer1 = pointer2 - 1
			else:
				pointer1 -= 1
				pointer2 -= 1
		return maxer


class Solution:

	def largestPerimeter(self, A: List[int]) -> int:
		A.sort(reverse=True)
		p = 0
		for i in range(len(A) - 2):
			if A[i] < sum(A[i + 1:i + 3]):
				p = sum(A[i:i + 3])
				break
		return p


class Solution:

	def largestPerimeter(self, A: List[int]) -> int:
		A = sorted(A)
		while True:
			if A[-1] + A[-2] > A[-3] and A[-1] + A[-3] > A[-2] and (A[-3] + A[-2] > A[-1]):
				return A[-1] + A[-2] + A[-3]
			A.remove(A[-1])
			if len(A) == 2:
				return 0


class Solution:

	def largestPerimeter(self, A: List[int]) -> int:

		def area(a, b, c):
			s = (a + b + c) / 2
			val = (s * (s - a) * (s - b) * (s - c)) ** (1 / 2)
			if isinstance(val, complex):
				return 0
			return val
		A.sort()
		maxi = 0
		for i in range(len(A) - 2):
			val = area(A[i], A[i + 1], A[i + 2])
			if val != 0:
				p = A[i] + A[i + 1] + A[i + 2]
				if p > maxi:
					maxi = p
		return maxi


class Solution:

	def largestPerimeter(self, A: List[int]) -> int:

		def formT(a, b, c):
			if not a + b > c:
				return False
			if not b + c > a:
				return False
			if not a + c > b:
				return False
			return True
		sides = sorted(A)
		for i in range(len(sides) - 3, -1, -1):
			if formT(sides[i], sides[i + 1], sides[i + 2]):
				return sides[i] + sides[i + 1] + sides[i + 2]
		return 0


class Solution:

	def largestPerimeter(self, A: List[int]) -> int:
		if len(A) < 3:
			return 0
		A.sort(reverse=True)
		while len(A) > 2:
			max_num = A.pop(0)
			if max_num >= A[0] + A[1]:
				continue
			else:
				return max_num + A[0] + A[1]
		return 0


class Solution:

	def largestPerimeter(self, A: List[int]) -> int:
		res = 0
		A.sort()
		for i in range(len(A)):
			for j in range(i + 1, len(A)):
				source = A[i]
				target = A[j]
				lower = abs(target - source)
				upper = source + target
				if upper * 2 < res:
					continue
				for t in range(j + 1, len(A)):
					if lower < A[t] < upper:
						res = max(source + target + A[t], res)
						break
				if res != 0:
					break
		return res


class Solution:

	def largestPerimeter(self, A: List[int]) -> int:
		A.sort()
		res = 0
		for i in range(len(A) - 2):
			test = A[i:i + 3]
			if test[0] + test[1] > test[2] and test[2] - test[1] < test[0] and (sum(test) > res):
				res = sum(test)
		return res


class Solution:

	def largestPerimeter(self, A: List[int]) -> int:
		A.sort(reverse=True)
		for i in range(len(A) - 2):
			(a, b, c) = (A[i], A[i + 1], A[i + 2])
			c1 = a + b > c
			c2 = b + c > a
			c3 = c + a > b
			if c1 and c2 and c3:
				return a + b + c
		return 0