본문 바로가기

Research/Coding Test24

스택/큐_올바른 괄호 나의 시도 1 function solution(s){ const stack = []; s.split('').forEach(chr => { stack.push(chr) while(stack[stack.length-2] + stack[stack.length-1] === "()") { stack.pop(); stack.pop(); } }) return stack.length === 0? true : false } console.log(solution(")()(")) 코드는 정상 작동하나, 효율성에서 탈락. 아마도 이중 반복문으로 인한 것으로 봉니다. 시도 2 class Node { constructor(value) { this.value = value; this.next = null; } } class Sta.. 2023. 11. 19.
스택/큐_기능개발 나의 풀이 function solution(progresses, speeds) { var deployCounter = []; while(progresses.length > 0) { let completed = 0; progresses.forEach((el, idx) => { progresses[idx] += speeds[idx] }) while(progresses[0] >= 100) { progresses.shift(); speeds.shift(); completed++; } if (completed > 0) { deployCounter.push(completed) } } return deployCounter; } progresses 배열에 아직 요소가 남아있는 한 무한 루프를 돌린다. 루프 도는 동안 .. 2023. 11. 19.
스택/큐_같은 숫자는 싫어 나의 풀이 function solution(arr) { var answer = []; let cur; arr.forEach(el => { if(arr.length === 0) { answer.push(el); return; } if (answer[answer.length-1] === el) { return; } answer.push(el) }) return answer; } answer 배열을 스택으로 생각한다. 주어진 arr 루프 돌다가 현재 값이 가장 최근에 적재된 데이터와 동일하면 패스하고 그렇지 않으면 적재하는 식으로 해결했다. 2023. 11. 19.
정렬_K번째수 나의 해결 방법 function solution(array, commands) { const result = []; commands.forEach(el => { const [i, j, k] = el const cutArr = array.slice(i-1, j).sort((a, b) => a - b); result.push(cutArr[k-1]) }) return result; } 이건 쉬운 문제. 타인의 해결 방법 function solution(array, commands) { return commands.map(command => { const [sPosition, ePosition, position] = command const newArray = array .filter((value, fIndex.. 2023. 11. 18.