🔎 문제
🧩 구현과정 및 코드
개인 토글 영역에 구현 과정과 코드를 자유롭게 작성해주시면 됩니다.
사용할 데이터 구조와 풀이 방향성
적용할 알고리즘 혹은 메서드
수영
구현
- 데이터 구조: 큐/스택
- 배포 예정 배열(scheduled)는 큐로 구현
- 먼저 들어온 데이터가 먼저 나가기 때문
- 출력할 배포 개수 배열(deploys)은 스택으로 구현
- 데이터가 계속 쌓이기 때문
- 아이디어
- 배포 예정일을 계산해서 예정 배열(scheduled)을 만들어 놓는다.
- 예정 배열의 첫 번째 요소만큼 나머지도 빼버리면, 배포할 상태가 된다.
- 배포 상태에서 맨 앞 요소가 0 이하인지 체크해서 배포 기능 개수(count)를 구한다.
- 배포 배열에 기능 개수(count)를 push한다.
코드
function solution(progresses, speeds) { const deploys = []; // 배포 예정일 계산 let scheduled = Array.from(progresses, (progress, i) => Math.ceil((100 - progress) / speeds[i])) while (scheduled.length) { let count = 0; const firstValue = scheduled[0] // 첫 번째 예정일을 전부 빼면 배포일 scheduled = scheduled.map(x => x - firstValue) // 배포 기능 개수 체크(맨 앞 요소가 0 이하이면 배포 기능에 추가) while (scheduled.findIndex(x => x <= 0) === 0) { scheduled.shift() count++; } // 배포 deploys.push(count) } return deploys }
정은
구현
내 코드
function solution(progresses, speeds) { var answer = []; while (progresses.length) { //배포할 애들이 있을 동안 answer.push(0) const neededDays1 = Math.ceil((100-progresses[0])/speeds[0]) //배포하는 날 progresses = progresses.map((progress,idx) => progress+speeds[idx]*neededDays1) //배포하는 날에 각 기능의 완성도를 업데이트 while (progresses[0] >= 100) { //배포하는 날에 완성도가 100이상이면 같이 나감 progresses.shift() //배포된 애들 빼주기 speeds.shift() answer[answer.length-1]++ } } return answer; }
다른 코드
function solution(progresses, speeds) { let answer = [0]; let days = progresses.map((progress, index) => Math.ceil((100 - progress) / speeds[index])); //각 기능들이 완료까지 걸리는 날들을 계산 let maxDay = days[0]; for(let i = 0, j = 0; i< days.length; i++){ if(days[i] <= maxDay) { //배포 하는 날보다 적게 걸리거나 같게 걸린다면 같이 배포 나감 answer[j] += 1; } else { //더 걸린다면 다음 배포에 나감 maxDay = days[i]; //배포 날을 갱신 answer[++j] = 1; } } return answer; }
스택/큐를 사용하지 않고 O(n)으로 풀 수 있다
종혁
구현
- 앞에 있는 기능들부터 순차적으로 배포 가능
- 배포가 이루어질때 , 몇개씩 이루어지는지? - 몇일에 일어나는지는 중요하지 않음
코드
function solution(prog, speeds) { const result = [] const completeDays = prog.map((p,idx) => { return Math.ceil((100-p)/speeds[idx]) })//각 기능들이 100프로가 되기까지 걸리는 날짜 //[95, 90, 99, 99, 80, 99] -> [ 5, 10, 1, 1, 20, 1 ] let day = 0//현재 날짜 while(completeDays.length){ day = completeDays.shift()//제일 앞에있는 기능이 배포됨 -> 해당 날짜가 되었다는 의미 let num = 1 while(completeDays[0] <= day){//현재 날짜보다 먼저 배포되었다면 같이 배포 num ++ prog.shift() } result.push(num) } return result } //[5] //[10,1,1] //[20,1]
재웅
구현
- 각 기능별 남은 일수를 계산
- 남은 일수를 순회하며 함께 배포할 수 있는 것들을 누산
- 추가적으로 배포할 기능들이 있다면 count를 증가시키며 (2)를 반복 연산
코드
function solution(progresses, speeds) { const answer = []; const restDay = [] progresses.forEach((element,idx)=>{ restDay.push(Math.ceil((100-element)/speeds[idx])) }) console.log(restDay) // [ 5, 10, 1, 1, 20, 1 ] let count = 0 let deploy = restDay[0] restDay.forEach((element,idx)=>{ const next = restDay[idx+1] if(next && deploy < next){ count++; answer.push(count) count=0 deploy = next } else if(next && deploy >= next){ count++ }else{ count++ answer.push(count) } }) return answer; }
✏️ 후기
문제를 풀고 느낀 점, 막혔던 부분 혹은 개선 사항 등을 자유롭게 작성해주시면 됩니다.
수영
- 첫 코드에서 1, 2, 4, 5 테스트 케이스에서 실패했다.
findIndex(x => x < 0)
음수만 체크한 것이 문제였다.<=
으로 바꾸니 풀렸다.
정은
- 같은 레벨2인데 어제보단 수월했던 것 같다
종혁
- 어제 푼 문제랑 비슷했던것 같다
재웅