728x90
문제💭
나의 풀이👨💻
function solution(lines) {
const linesArr = [...lines[0], ...lines[1], ...lines[2]]
const min = Math.min(...linesArr)
const max = Math.max(...linesArr)
const result = Array(max - min + 1).fill(0)
for (let line of lines) {
line.sort((a, b) => a - b)
if (min < 0) {
line[0] += Math.abs(min)
line[1] += Math.abs(min)
}
for (let i = line[0]; i < line[1]; i ++ ) {
result[i] ++;
}
}
return result.filter(x => x > 1).length;
}
다른 사람의 풀이👨🏫
1. 배열에 0을 채우고 겹치는 부분 찾기
function solution(lines) {
let line = new Array(200).fill(0);
lines.forEach(([a, b]) => {
for(; a < b; a++) line[a+100]++;
});
return line.reduce((a, c) => c > 1 ? a + 1 : a, 0)
}
2.
function solution(lines) {
var min = Math.min(...lines.flat());
var max = Math.max(...lines.flat());
var totalOverlappedLength = 0;
function isInbound(x, [a,b]){
var s = Math.min(a,b);
var e = Math.max(a,b);
x = x + 0.5;
if ((s < x)&&(x < e)){
return true;
}
return false;
}
for(let x = min; x < max; x++){
var overlappedOnX = 0;
lines.forEach((el) => {
if(isInbound(x,el)){
overlappedOnX = overlappedOnX + 1;
}
});
if(overlappedOnX > 1){
totalOverlappedLength = totalOverlappedLength + 1;
}
}
return totalOverlappedLength;
}
Reference
- 프로그래머스
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
728x90
'Programmers > Lv.0' 카테고리의 다른 글
[프로그래머스][Js] Lv.0 - 연속된 수의 합 (1) | 2023.03.14 |
---|---|
[프로그래머스][Js] Lv.0 - 안전지대 (0) | 2023.03.14 |
[프로그래머스][Js] Lv.0 - 평행 (0) | 2023.03.13 |
[프로그래머스][Js] Lv.0 - 옹알이 (1) (0) | 2023.03.10 |
[프로그래머스][Js] Lv.0 - 두 수의 합 (0) | 2023.03.09 |
댓글