Algorithm
-
[CodeKata] 프로그래머스 : 3.7(일), 게임 맵 최단거리Algorithm 2021. 3. 7. 15:34
🥋 Ooooth!! (Level 2) 🧮 풀이 최단거리이기에 BFS 문제임은 인지했으며, 모범답안을 통해 Javascript에서 queue를 통한 BFS 구현을 공부했다. 🖇 리뷰 function solution(maps) { let result = 0; let ySize = maps.length; let xSize = maps[0].length; let queue = [[0, 0]]; let visited = Array.from(new Array(ySize), () => new Array(xSize).fill(false)); while (queue.length) { result++; const size = queue.length; for (let i = 0; i < size; i++) { const p..
-
[CodeKata] 프로그래머스 : 3.4(목), 다음 큰 숫자Algorithm 2021. 3. 4. 12:24
🥋 Ooooth!! (Level 2) 🧮 풀이 function binOneCount(n) { const regex = /[1]/g; return n.toString(2).match(regex).length; } function solution(n) { const target = binOneCount(n) while (true) { n++; if (binOneCount(n) === target) { return n; break; } } } binOneCount() 함수는, n(숫자)를 이진법으로 변환한 뒤 '1'의 개수를 반환하는 함수이다. solution() 함수는 풀이를 진행할 함수이다. 먼저, 현재 숫자 n의 1의 개수를 target 변수에 저장한다. while 반복문을 실행한다. n에 1을 더하면서..
-
[CodeKata] 프로그래머스 : 3.3(수), 올바른 괄호 & 튜플Algorithm 2021. 3. 3. 12:45
🥋 Ooooth!! (Level 2) : 올바른 괄호 🧮 풀이 function solution(s){ let stack = []; let stackNum = 0; for (let bracket of s) { if (bracket === '(') { stack.push(bracket) stackNum++ } else { stack.pop(); stackNum-- } } return stack.length === 0 && stackNum === 0 } stack 과 stackNum 2가지 형태의 스택으로 s(괄호 문자열)의 누적값을 관리할 것이다. s(괄호 문자열)을 순회하며, '(' 라면 stack 배열 push & stackNum++, ')' 라면 stack 배열 끝을 pop() & stackNum-- ..
-
[CodeKata] 프로그래머스 : 3.2(화), 가장 큰 정사각형 찾기Algorithm 2021. 3. 2. 14:25
🥋 Ooooth!! (Level 2) 🧮 풀이 * 정확성 테스트 1개 실패, 효율성 테스트 실패로 통과하지 못했다. function solution(board) { let answer = 1; for (let i = 0 ; i board.length-1 || j+answer > board[0].length-1) { break; } answer++; if (board.slice(i, i+answer).map(row => row.slice(j, j+answer))..
-
[CodeKata] 프로그래머스 : 3.1(월), 쿼드압축 후 개수Algorithm 2021. 3. 1. 11:55
🥋 Ooooth!! (Level 2) 🧮 풀이 function solution(arr) { let l = arr.length; const count = arr.flat().filter(e => e === 0).length; let answer = { 0: count, 1: l*l - count, }; let check = Array.from({length: l}, () => Array.from({length: l}, () => false)) while (l > 2) { for (let i = 0 ; i < arr.length ; i += l/2) { for (let j = 0 ; j < arr.length ; j += l/2) { const quadBox = arr.slice(i, i+l/2).map(e..
-
[CodeKata] 데일리 프로그래머스 : 2.27(토), 순위 검색Algorithm 2021. 2. 27. 18:18
🥋 Ooooth!! (Level 2) 🧮 풀이 function solution(info, query) { let answer = [] info = info.map(e => e.split(' ')) query = query.map(e => e.split(' ').filter(f => f !== 'and')) for (let q of query) { let nowInfo = info.filter(e => Number(e[4]) >= Number(q[4])); for (let i = 0 ; i e[i] === q[i]) } } answer.push(nowInfo.length) } r..
-
[CodeKata] 프로그래머스 : 2.26(금), 타겟 넘버, 카펫Algorithm 2021. 2. 26. 15:28
🥋 Ooooth!! (Level 2) : 1️⃣ 타겟 넘버 🧮 풀이 function solution(numbers, target) { let answer = 0; function DFS(idx, now, acc) { acc += now; if (idx === numbers.length - 1) { answer = acc === target ? answer + 1 : answer; return; } DFS(idx+1, numbers[idx+1], acc) DFS(idx+1, -numbers[idx+1], acc) } DFS(-1, 0, 0); return answer; } answer는 최종정답이자, 누적값(acc)과 target이 일치할 때 1을 더하는 변수가 될 것이다. DFS() 깊이우선탐색 함수를 ..