반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 프로그래머스
- 알고리즘
- HTTP
- 크롤링
- TCP
- Algorithm
- javascript
- 자바스크립트
- 쉬운 문제
- ip
- typescript
- 가천대
- 백준
- Node.js
- 소켓
- type challenge
- Crawling
- dp
- 그래프
- 문자열
- Nestjs
- HTTP 완벽 가이드
- 타입 챌린지
- BFS
- 수학
- 프로그래머스 레벨 2
- 타입스크립트
- 레벨 1
- socket
- dfs
Archives
- Today
- Total
kakasoo
[node.js] 쿼드 압축 후 개수 세기 ( 프로그래머스 레벨2 ) 본문
반응형
const checkArr = (arr) => {
const firstValue = arr[0][0];
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr.length; j++) {
if (arr[i][j] !== firstValue) {
return { done: false };
}
}
}
return { done: true, value: firstValue };
};
const makeQuarter = (arr) => {
const upArr = arr.splice(0, arr.length / 2);
const downArr = arr;
const upLeftArr = upArr.map((el) => el.splice(0, el.length / 2));
const upRightArr = upArr;
const downLeftArr = downArr.map((el) => el.splice(0, el.length / 2));
const downRightArr = downArr;
return [upLeftArr, upRightArr, downRightArr, downLeftArr].map((el) => {
let cur;
if ((cur = checkArr(el)).done) {
return cur.value;
} else return makeQuarter(el);
});
};
const solution = (arr) => {
if (checkArr(arr).done) {
const answer = [0, 0];
answer[arr[0][0]]++;
return answer;
}
const quarters = makeQuarter(arr);
const board = quarters.flat(Infinity);
const zeroes = board.filter((el) => el === 0).length;
const ones = board.filter((el) => el === 1).length;
return [zeroes, ones];
};
내가 생각한 로직은,
- 일단 4등분을 하는 로직을 만든다.
- 4등분을 하고 나면 그 배열들을 순회하며, 전부 0 또는 1로 이루어져 있는지를 체크한다.
- 만약 전부 1 또는 0으로 된 경우, 1 또는 0을 반환한다.
- 만약 전부 하나의 값으로 되어 있지 않다면 재귀적으로 4등분하는 로직을 호출한다.
- 4등분을 하고 나면 그 배열들을 순회하며, 전부 0 또는 1로 이루어져 있는지를 체크한다.
- 위의 체크 로직을 구현한다.
- 돌아온 값은 예제의 그림처럼 되어 있을 테니, flatten 하게 만든 다음 각각 0과 1의 개수를 세서 배열에 담아 반환한다.
반응형
'프로그래밍 > 알고리즘 풀이' 카테고리의 다른 글
[node.js] 방문 길이 ( 프로그래머스 레벨2 ) (0) | 2021.07.02 |
---|---|
[node.js] 괄호 회전하기 ( 프로그래머스 레벨2 ) (0) | 2021.07.01 |
[node.js] 튜플 ( 프로그래머스 레벨2 ) (0) | 2021.07.01 |
[node.js] 땅따먹기 ( 프로그래머스 레벨2 ) (0) | 2021.07.01 |
[node.js] 2개 이하로 다른 비트 ( 프로그래머스 레벨2 ) (0) | 2021.07.01 |