Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Condition of traingle with sides a,b and c -> a<=b<=c and a+b>c
- we want the highest perimeter also so we will sort the order now so we will meet one condition a<=b<=c now you have to check a+b>c or not so take the right most element and check the previous two element's sum are greater than that elment or not?
- if not then move all a,b,c one side left as if previous c you couldn't beat by sum of prev a and b then after reducing a & b,the new a & b are more smaller so better reduce c also for checking all the possible sides.
- '''
- class Solution:
- def largestPerimeter(self, nums: List[int]) -> int:
- nums.sort()
- i = len(nums) - 1
- while i >= 2:
- if nums[i-2] + nums[i-1] > nums[i]:
- return nums[i-2] + nums[i-1] + nums[i]
- i -= 1
- return 0
Advertisement
Add Comment
Please, Sign In to add comment