diff --git "a/problems/\352\265\254\353\252\205\353\263\264\355\212\270/\354\204\234\355\235\254/README.md" "b/problems/\352\265\254\353\252\205\353\263\264\355\212\270/\354\204\234\355\235\254/README.md" new file mode 100644 index 0000000..f5fd756 --- /dev/null +++ "b/problems/\352\265\254\353\252\205\353\263\264\355\212\270/\354\204\234\355\235\254/README.md" @@ -0,0 +1,36 @@ +# 프로그래머스 [구명보트](https://programmers.co.kr/learn/courses/30/lessons/42885) + +## solution + +```js +function solution(people,limit){ + let [ answer, currentWeight, start, end ] = [0, 0, 0, people.length - 1 ]; + people.sort((a,b)=> b-a); + + while(start < end){ + if(currentWeight + people[start] <= limit ){ + currentWeight += people[start]; + start+=1; + } + + if(currentWeight + people[end] <= limit){ + currentWeight += people[end]; + end -= 1; + } else { + currentWeight = 0; + answer +=1 + } + } + answer += currentWeight != 0 ? 1 : 0 + return start === end ? answer + 1 : answer ; +} +``` + +1. 구조 분해 할당을 통해 결과, 현재 보트의 무게, 시작과 끝을 가리키는 포인터 변수를 초기화 한다. +2. 내림차순으로 people을 정렬한다. +3. 투포인터가 만나기전까지 반복문을 수행한다. + 1. 현재 보트 무게 + 시작 포인터 사람 무게가 제한보다 작거나 같으면 보트에 태우고, 시작 포인터와 현재 보트 무게 증가 시킨다. + 2. 현재 보트 무게 + 끝 포인터 사람 무게가 제한보다 작거나 같으면 보트에 태우고, 끝 포인터는 내리고, 현재 보트 무게를 증가시킨다. + 3. 2가 아니라면 현재 보트 무게 0으로 초기화 및 결과 +1로 보트 수를 갱신한다. +4. 현재 보트 무게가 0이 아니라면 사람이 타고 있으므로 결과 +1 를 진행한다. +5. 시작과 끝 포인터가 동일지점이라면 answer+1 한 값을, 그게 아니라면 answer값을 반환하고 종료한다. diff --git "a/problems/\352\265\254\353\252\205\353\263\264\355\212\270/\354\204\234\355\235\254/solution.js" "b/problems/\352\265\254\353\252\205\353\263\264\355\212\270/\354\204\234\355\235\254/solution.js" new file mode 100644 index 0000000..778b3f6 --- /dev/null +++ "b/problems/\352\265\254\353\252\205\353\263\264\355\212\270/\354\204\234\355\235\254/solution.js" @@ -0,0 +1,22 @@ +function solution(people,limit){ + let [ answer, currentWeight, start, end ] = [0, 0, 0, people.length - 1 ]; + people.sort((a,b)=> b-a); + + while(start < end){ + if(currentWeight + people[start] <= limit ){ + currentWeight += people[start]; + start+=1; + } + + if(currentWeight + people[end] <= limit){ + currentWeight += people[end]; + end -= 1; + } else { + currentWeight = 0; + answer +=1 + } + + } + answer += currentWeight != 0 ? 1 : 0; + return start === end ? answer + 1 : answer ; +} diff --git "a/problems/\354\213\240\352\263\240\352\262\260\352\263\274\353\260\233\352\270\260/\354\204\234\355\235\254/README.md" "b/problems/\354\213\240\352\263\240\352\262\260\352\263\274\353\260\233\352\270\260/\354\204\234\355\235\254/README.md" new file mode 100644 index 0000000..d9d65fd --- /dev/null +++ "b/problems/\354\213\240\352\263\240\352\262\260\352\263\274\353\260\233\352\270\260/\354\204\234\355\235\254/README.md" @@ -0,0 +1,31 @@ + +# 프로그래머스 [신고결과받기](https://programmers.co.kr/learn/courses/30/lessons/92334) + +## solution + +```js +function solution(id_list, report, k) { + const resultCount = new Array(id_list.length).fill(0); + const reportCount = new Array(id_list.length).fill(0); + + report = [...new Set(report)].map((sentence) => sentence.split(' ')); + + report.forEach(([_, reporter])=> { + reportCount[id_list.indexOf(reporter)] += 1; + }) + + report.forEach(([user,reporter])=>{ + if(reportCount[id_list.indexOf(reporter)] >= k ){ + resultCount[id_list.indexOf(user)] += 1; + } + }) + return resultCount; + +} +``` + +1. 결과 횟수, 신고 횟수 담을 배열 초기화 +2. report의 중복제거 후 이용자 / 신고자 나눠주기 진행 +3. 신고자 별 신고 당한 횟수를 반복문을 통해 저장한다. +4. k와 신고자별 신고당한 횟수를 비교하여 결과 횟수를 갱신해나간다. +5. 최종적으로 resultCount반환한후 종료. diff --git "a/problems/\354\213\240\352\263\240\352\262\260\352\263\274\353\260\233\352\270\260/\354\204\234\355\235\254/solution.js" "b/problems/\354\213\240\352\263\240\352\262\260\352\263\274\353\260\233\352\270\260/\354\204\234\355\235\254/solution.js" new file mode 100644 index 0000000..abb912c --- /dev/null +++ "b/problems/\354\213\240\352\263\240\352\262\260\352\263\274\353\260\233\352\270\260/\354\204\234\355\235\254/solution.js" @@ -0,0 +1,19 @@ +function solution(id_list, report, k) { + const resultCount = new Array(id_list.length).fill(0); + const reportCount = new Array(id_list.length).fill(0); + + report = [...new Set(report)] + .map((sentence) => sentence.split(' ')); + + report.forEach(([_, reporter])=> { + reportCount[id_list.indexOf(reporter)] += 1; + }) + + report.forEach(([user,reporter])=>{ + if(reportCount[id_list.indexOf(reporter)] >= k ){ + resultCount[id_list.indexOf(user)] += 1; + } + }) + + return resultCount; +}