Leetcode Note: Go - Largest Perimeter Triangle

Add Binary - LeetCode
https://leetcode.com/problems/add-binary/

  • Go 言語で取り組んだメモ

回答

Largest Perimeter Triangle - LeetCode
https://leetcode.com/problems/largest-perimeter-triangle/solution/

Sort then O(1) space - LeetCode Discuss
https://leetcode.com/problems/largest-perimeter-triangle/discuss/2243859/Sort-then-O(1)-space

func largestPerimeter(nums []int) int {
    if len(nums) < 3 { return 0 }
    
    sort.Ints(nums)
    
    right := len(nums)-1
    
    for right >= 2 {
        if nums[right-2] > nums[right-1]-nums[right] && nums[right-2] > nums[right]-nums[right-1] {
            return nums[right-2]+nums[right-1]+nums[right]
        }
        
        right--
    }
        
    return 0
}