저장을 습관화
프로그래머스 LV.0 가까운 수 본문
프로그래머스 LV.0 가까운 수
https://school.programmers.co.kr/learn/courses/30/lessons/120890
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
1. 문제 명
가까운 수
2. 문제 설명
정수 배열 array와 정수 n이 매개변수로 주어질 때, array에 들어있는 정수 중 n과 가장 가까운 수를 return 하도록 solution 함수를 완성해주세요.
3. 제한 사항
- 1 ≤ array의 길이 ≤ 100
- 1 ≤ array의 원소 ≤ 100
- 1 ≤ n ≤ 100
- 가장 가까운 수가 여러 개일 경우 더 작은 수를 return 합니다.
4. 예시
array | n | result |
[3, 10, 28] | 20 | 28 |
[10, 11, 12] | 13 | 12 |
5. 기본 제공 코드
function solution(array, n) {
var answer = 0;
return answer;
}
6. 제출한 내 답
const solution = (array, n) => {
array.sort((a, b) => a - b);
let distance = array.map((v) => {
return Math.abs(v - n);
});
let smallest = Math.min(...distance);
let index = distance.indexOf(smallest);
return array[index];
};
6-2. VSC에 작성한 내용
const solution = (array, n) => {
// 오름차순 정렬 - 가장 가까운 수가 여러 개일 경우 더 작은 수를 반환하기 위해서
array.sort((a, b) => a - b);
// n과의 차이
let distance = array.map((v) => {
return Math.abs(v - n);
});
// 제일 작은 수
let smallest = Math.min(...distance);
// 제일 작은 수의 위치?
let index = distance.indexOf(smallest);
console.log(`n과의 차이 ${distance}`);
console.log(`가장 작은 수 ${smallest}`);
console.log(`가장 작은 수의 위치 ${index}`);
return array[index];
};
// 테스트
console.log(solution([3, 10, 20], 20));
console.log(solution([10, 11, 12], 13));
console.log(solution([4, 2], 3));
7. 특이사항
틀렸던 방법
const solution = (array, n) => {
// n과의 차이
let distance = array.map((v) => {
return Math.abs(v - n);
});
// 제일 작은 수
let smallest = Math.min(...distance);
// 제일 작은 수의 위치?
let index = distance.indexOf(smallest);
console.log(distance);
console.log(smallest);
console.log(index);
return array[index];
};
// 테스트
console.log(solution([3, 10, 28], 20));
console.log(solution([10, 11, 12], 13));
테스트 1 〉 통과 (0.05ms, 33.4MB)
테스트 2 〉 통과 (0.08ms, 33.5MB)
테스트 3 〉 통과 (0.05ms, 33.5MB)
테스트 4 〉 통과 (0.05ms, 33.5MB)
테스트 5 〉 실패 (0.05ms, 33.4MB)
테스트 6 〉 통과 (0.07ms, 33.5MB)
테스트 7 〉 통과 (0.05ms, 33.4MB)
테스트 8 〉 통과 (0.06ms, 33.4MB)
테스트 9 〉 통과 (0.05ms, 33.5MB)
테스트 10 〉 통과 (0.05ms, 33.5MB)
테스트 11 〉 통과 (0.06ms, 33.5MB)
테스트 12 〉 통과 (0.05ms, 33.4MB)
테스트 13 〉 실패 (0.05ms, 33.5MB)
테스트 14 〉 통과 (0.05ms, 33.4MB)
테스트 15 〉 통과 (0.05ms, 33.5MB)
테스트 16 〉 통과 (0.05ms, 33.5MB)
테스트 17 〉 통과 (0.06ms, 33.5MB)
테스트 18 〉 통과 (0.05ms, 33.5MB)
'가장 가까운 수가 여러 개일 경우 더 작은 수를 return 한다.' 라는 제한 사항에 걸리는 것 같다.
제일 작은 수와 그 위치를 구하는 과정은 배열에서 가장 앞에 있는 요소를 반환하기 때문에
solution([4, 2], 3) 같은 경우 4를 반환하게 된다.
이를 해결하기 위해서 함수의 기능 중 가장 첫번째로 오름차순 정렬을 추가해 주었다.
이게 되네 ㅋㅋ
8. 다른 사람이 작성한 답
8-1.
function solution(array, n) {
return array.reduce((a,c)=> Math.abs(a-n) < Math.abs(c-n) ? a : Math.abs(a-n) === Math.abs(c-n) ? Math.min(a, c) : c);
}
8-2.
function solution(array, n) {
array.sort((a,b) => Math.abs(n - a) - Math.abs(n - b) || a - b);
return array[0];
}
'코딩 테스트 > 프로그래머스 - 자바스크립트' 카테고리의 다른 글
프로그래머스 LV.0 진료순서 정하기 (0) | 2023.09.25 |
---|---|
프로그래머스 LV.0 특정 문자열로 끝나는 가장 긴 부분 문자열 찾기 (0) | 2023.09.25 |
프로그래머스 LV.0 1로 만들기 (0) | 2023.09.25 |
프로그래머스 LV.0 숨어있는 숫자의 덧셈 (2) (0) | 2023.09.25 |
프로그래머스 LV.1 없는 숫자 더하기 (0) | 2023.09.25 |