-
[Programmers] JavaScript 알고리즘 | Lv.0 삼각형의 완성조건 (2)► JS Algorithm/Programmers 2024. 8. 18. 13:22반응형
🔒 문제 설명
선분 세 개로 삼각형을 만들기 위해서는 다음과 같은 조건을 만족해야 합니다.
- 가장 긴 변의 길이는 다른 두 변의 길이의 합보다 작아야 합니다.
삼각형의 두 변의 길이가 담긴 배열 sides이 매개변수로 주어집니다. 나머지 한 변이 될 수 있는 정수의 개수를 return하도록 solution 함수를 완성해주세요.
🔒 제한사항
- sides의 원소는 자연수입니다.
- sides의 길이는 2입니다.
- 1 ≤ sides의 원소 ≤ 1,000
🔒 입출력 예
sides result [1, 2] 1 [3, 6] 5 [11, 7] 13
🔒 입출력 예 설명
입출력 예 #1
- 두 변이 1, 2 인 경우 삼각형을 완성시키려면 나머지 한 변이 2여야 합니다. 따라서 1을 return합니다.
입출력 예 #2
- 가장 긴 변이 6인 경우
- 될 수 있는 나머지 한 변은 4, 5, 6 로 3개입니다.
- 나머지 한 변이 가장 긴 변인 경우
- 될 수 있는 한 변은 7, 8 로 2개입니다.
- 따라서 3 + 2 = 5를 return합니다.
입출력 예 #3
- 가장 긴 변이 11인 경우
- 될 수 있는 나머지 한 변은 5, 6, 7, 8, 9, 10, 11 로 7개입니다.
- 나머지 한 변이 가장 긴 변인 경우
- 될 수 있는 한 변은 12, 13, 14, 15, 16, 17 로 6개입니다.
- 따라서 7 + 6 = 13을 return합니다.
🔐 solution of mine
Math.min()
Math.max()
Set
Set.prototype.add()
Array.prototype.filter()
function solution(sides) { const min = Math.min(...sides); const max = Math.max(...sides); let arr = new Set(); let answer = 0; for (let i = 1; i < min + max; i++) { arr.add(i); } return console.log( [...arr].filter((v) => max - min < v && v < min + max).length ); } solution([1, 2]); // expected output: 1 solution([3, 6]); // expected output: 5 solution([11, 7]); // expected output: 13
문제 풀이에대한 논리자체가 안되어서, GPT에게 문제에 대해 묻고 gpt가 알려준 범위를 js로 풀었다
gpt의 도움으로 js로 풀이는 했지만,
수학적인 문제라 와..... 논리자체가 이해가 안된다 ㅜ
아래에 다른분들 풀이도 메모해놓았는데,
더더더더더 이해가 어렵닼ㅋㅋㅋ
수학을 다시 파야하나 싶다 ㅋㅋ
나중에 차차 다시 복습해야할 문제다.
🔐 GPT's solution
Array.prototype.sort()
function solution(sides) { const [a, b] = sides.sort((x, y) => x - y); // 가장 긴 변이 b인 경우 const case1 = b - a + 1; // 나머지 한 변이 가장 긴 변인 경우 const case2 = a + b - 1; // 가능한 정수 개수는 두 경우의 범위를 합친 것 return console.log(case2 - case1 + 1); } solution([1, 2]); // expected output: 1 solution([3, 6]); // expected output: 5 solution([11, 7]); // expected output: 13
🔐 GPT's solutionArray.prototype.sort()
function solution(sides) { const [a, b] = sides.sort((x, y) => x - y); // 가장 긴 변이 b인 경우 const case1 = b - (b - a + 1) + 1; // 나머지 한 변이 가장 긴 변인 경우 const case2 = a + b - 1 - b; // 가능한 정수의 개수는 두 경우의 합 return console.log(case1 + case2); } solution([1, 2]); // expected output: 1 solution([3, 6]); // expected output: 5 solution([11, 7]); // expected output: 13
🔐 solution of others
Math.min()
function solution(sides) { return console.log(Math.min(...sides) * 2 - 1); } solution([1, 2]); // expected output: 1 solution([3, 6]); // expected output: 5 solution([11, 7]); // expected output: 13
🔐 solution of others
Math.min()
Math.max()
Set
Set.prototype.add()
Set.prototype.size
const findExtras = (widths) => { let [min, max] = [Math.min(...widths), Math.max(...widths)]; let answerSet = new Set(); // max가 가장 긴 변인 경우 /* 0 < x min + x > max x < max */ for (let i = 1; i < max; i++) { if (i > max - min) { answerSet.add(i); } } // newbie가 가장 긴 변인 경우 /* x >= max x < min+max */ for (let i = max; i < min + max; i++) { answerSet.add(i); } return answerSet.size; }; function solution(sides) { return console.log(findExtras(sides)); } solution([1, 2]); // expected output: 1 solution([3, 6]); // expected output: 5 solution([11, 7]); // expected output: 13
🔐 solution of others
Math.min()
Math.max()
function solution(sides) { let a = Math.min(...sides); let b = Math.max(...sides); let answer = 0; for (let i = 1; i < a + b; i++) { let arr = [a, b, i].sort((a, b) => a - b); let [q, w, e] = arr; if (q + w > e) { answer++; } } return console.log(answer); } solution([1, 2]); // expected output: 1 solution([3, 6]); // expected output: 5 solution([11, 7]); // expected output: 13
🔐 solution of othersArray.prototype.sort()
function solution(sides) { sides.sort((a, b) => b - a); return console.log(sides[1] + sides[1] - 1); } solution([1, 2]); // expected output: 1 solution([3, 6]); // expected output: 5 solution([11, 7]); // expected output: 13
반응형'► JS Algorithm > Programmers' 카테고리의 다른 글
[Programmers] JavaScript 알고리즘 | Lv.0 문자 개수 세기 (0) 2024.08.18 [Programmers] JavaScript 알고리즘 | Lv.0 문자열 계산하기 (0) 2024.08.18 [Programmers] JavaScript 알고리즘 | Lv.0 조건 문자열 (0) 2024.08.18 [Programmers] JavaScript 알고리즘 | Lv.0 배열 만들기 4 (0) 2024.08.17 [Programmers] JavaScript 알고리즘 | Lv.0 두 수의 합 (0) 2024.08.16