You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
27 lines
374 B
27 lines
374 B
2 years ago
|
package largestperimeter
|
||
|
|
||
|
import (
|
||
|
"sort"
|
||
|
)
|
||
|
|
||
|
func isTriangle(a, b, c int) bool {
|
||
|
return a < b+c
|
||
|
}
|
||
|
|
||
|
func largestPerimeter(nums []int) int {
|
||
|
sort.Ints(nums)
|
||
|
|
||
|
for i := len(nums) - 1; i >= 2; i-- {
|
||
|
a, b, c := nums[i], nums[i-1], nums[i-2]
|
||
|
if isTriangle(a, b, c) {
|
||
|
return a + b + c
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return 0
|
||
|
}
|
||
|
|
||
|
func Triangle(nums []int) int {
|
||
|
return largestPerimeter(nums)
|
||
|
}
|