
길이가 서로 다른 A,B,C 세 개의 막대 길이가 주어진다.
이 세 막대로 삼각형을 만들 수 있다면 "YES", 만들 수 없으면 "NO"를 출력하라
- 첫 번째 줄에 100이하의 서로 다른 A,B,C 막대의 길이가 주어진다
- 첫 번째 줄에 "YES" , "NO"를 출력한다
- 입력 예) 6, 7, 11
- 출력) "YES"
const solution = (a,b,c) => {
const temp = [a,b,c]
const long = Math.max(a,b,c)
const other = temp.filter((el)=>el!==long).reduce((a,b)=>a+b)
return long < other ? "YES" : "NO"
}
하지만 이렇게하면 이등변 삼각형은 분별 불가
-동료의 최적 코드-
const solution = function(a, b, c) {
const sum = a + b + c
let max = a
if(max < b) max = b
if(max < c) max = c
const check = max - (sum - max)
return check < 0 ? "YES" : "NO"
}'Algorithm' 카테고리의 다른 글
| [algorithm] 홀수 (0) | 2022.10.23 |
|---|---|
| [algorithm] 최솟값 구하기 (0) | 2022.10.23 |
| [algorithm] 1부터 N까지 합 출력하기 (0) | 2022.10.21 |
| [algorithm] 연필 개수 (0) | 2022.10.20 |
| [algorithm] 세 수 중 최솟값 (0) | 2022.10.10 |