-
Notifications
You must be signed in to change notification settings - Fork 98
2022年04月18日(월) jaewon1676 7문제의 풀이 추가 #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
ab5aeca
14860ae
531ffdb
5b64e15
22af6f2
b598c1e
1725b3d
8ee3128
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,4 +21,32 @@ function isPrime(num) { | |
if (num % i === 0) return false | ||
} | ||
return true | ||
} | ||
} | ||
|
||
//정답 2 - jaewon1676 | ||
// https://programmers.co.kr/learn/courses/30/lessons/12977?language=javascript | ||
Comment on lines
+26
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 문제 링크 추가해주셨네요! 감사합니다 👍 👍 |
||
|
||
function solution(nums) { | ||
let len = nums.length, answer = 0; | ||
|
||
for (let i = 0; i < len - 2; i++) { | ||
for (let j = i + 1; j < len - 1; j++) { | ||
for (let k = j + 1; k < len; k++) { | ||
if (isPrime(nums[i] + nums[j] + nums[k])) { | ||
answer++; | ||
} | ||
} | ||
} | ||
} | ||
return answer; | ||
} | ||
|
||
const isPrime = (n) => { | ||
for (let i = 2; i <= Math.sqrt(n); i++) { //n의 제곱근까지 순회 | ||
if (n % i === 0) { // 나머지가 0이 나오면 소수가 아니다. | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
// 세개의 수를 더해야 하기때문에 수 for문 하나당 수 하나를 넣어서 순회하였다. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,4 +60,29 @@ function solution(n, lost, reserve) { | |
} | ||
// 체육복 1개 이상을 가진 학생들의 수 반환 | ||
return students.filter((v => v >= 1)).length; | ||
} | ||
} | ||
|
||
//정답 3 - jaewon1676 | ||
function solution(n, lost, reserve) { | ||
let answer = Array(n).fill(1) // n만큼의 배열을 만들어서 1을 만들어줍니다 | ||
let cnt = 0; | ||
for(let i = 0; i < reserve.length; i++){ // reserve 를 순회하며 체육복 | ||
answer[reserve[i]-1] += 1 | ||
} | ||
for(let i = 0; i < lost.length; i++){ // lost 를 순회하며 체육복 수를 -1 | ||
answer[lost[i]-1] -= 1 | ||
} | ||
for(let i = 0; i < n; i++){ // n을 순회하며 앞사람과 뒷사람의 체육복 수를 비교한다. | ||
if (answer[i] == 2 && answer[i+1] == 0 || answer[i+1] == 2 && answer[i] == 0){ | ||
answer[i] = 1 | ||
answer[i+1] = 1 | ||
} | ||
} | ||
for(let i = 0; i < answer.length; i++){ | ||
(answer[i] >= 1 ? cnt += 1 : null) | ||
} | ||
return cnt | ||
Comment on lines
+81
to
+84
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
//그리디 | ||
// lost 배열과 reserve 배열을 순회하여 체육복을 추가, 제거 해줍니다. | ||
// 그 후에 최종적으로 i부터 n까지 for문을 순회하며 i번쨰 학생과 i+1번째의 학생이 가진 체육복 수를 비교하여 빌려 줄 수 있는지, 빌려줄 수 없는지 확인 합니다. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,19 @@ function solution(s) { | |
answer = s.split(' ').map(word => word.charAt(0).toUpperCase() + word.substring(1).toLowerCase()).join(' ') | ||
//word[0]은 빈 문자열을 만나면 undefined를, word.charAt(0)은 빈 문자열을 만나면 빈 문자열을 반환한다. | ||
return answer; | ||
} | ||
} | ||
|
||
//정답 2 - jaewon1676 | ||
function solution(s) { | ||
s = s.split(' ').map(el => el.split('').map((el, index) => | ||
index == 0 ? el.toUpperCase() : el.toLowerCase()).join('')).join(' ') | ||
return s; | ||
} | ||
// 문자열을 연습하기에 좋은 문제입니다. | ||
|
||
// s.split(' ') // 띄어쓰기를 기준으로 나눕니다. | ||
// .map(el => el.split('')) 나눈 덩어리를 다시 요소 하나 하나씩 나눠줍니다 | ||
// .map((el, index) => index == 0 ? el.toUpperCase() : el.toLowerCase()) | ||
// 덩어리의 요소가 첫번째이면 대문자, 그렇지 않으면 소문자로 변환 해줍니다. | ||
// .join('') 작은 배열들을 합쳐줍니다. | ||
// .join(' ') 큰 배열들을 합쳐줍니다. | ||
Comment on lines
+17
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 상세한 설명 감사합니다 👍 👍 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
//https://github.com/codeisneverodd/programmers-coding-test | ||
//완벽한 정답이 아닙니다. | ||
//정답 1 - jaewon1676 | ||
function solution (n, left, right) { | ||
const answer = []; | ||
|
||
for (let i=left; i <= right; i++) { // left부터 right까지를 구한다. | ||
let row = parseInt(i/n); // 행(row)을 구한다. | ||
let column = i%n; // 열(column)을 구한다. | ||
answer.push(Math.max(row, column) + 1) // 행과 열중 큰 값을 푸시한다. | ||
} | ||
return answer | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//https://github.com/codeisneverodd/programmers-coding-test | ||
//완벽한 정답이 아닙니다. | ||
//정답 1 - jaewon1676 | ||
function solution(routes) { | ||
let cctv = 1; // cctv의 개수는 최소 1개 | ||
routes.sort((a, b) => a[0] - b[0]); // 고속도로 진입 시점을 기준으로 오름차순 정렬 | ||
// [ [ -20, -15 ], [ -18, -13 ], [ -14, -5 ], [ -5, -3 ] ] | ||
let out = routes[0][1]; // -15 | ||
// 나간 시점(out)은 첫 차량의 나간시점으로 초기화 | ||
|
||
for(let i = 1; i < routes.length; i++) { | ||
// 나간 시점(out)보다 현재 차량의 진입이 느리다면 카메라 추가 설치 | ||
if(out < routes[i][0]) { | ||
cctv++; | ||
out = routes[i][1]; // out 시점 업데이트 | ||
} | ||
|
||
// 나간 시점(out)이 현재 차량의 진출시점보다 큰 경우 | ||
if(out > routes[i][1]) { | ||
out = routes[i][1]; // out 시점 업데이트 | ||
} | ||
} | ||
|
||
return cctv; | ||
} | ||
// 그리디 | ||
|
||
// 우리는 카메라를 최소로 설치 해야합니다. 그러기 위해서는 고속도로 진입 시점을 기준으로 오름차순 정렬을(빨리 진입한 순) 합니다. | ||
// 이렇게 되면 배열에 있는 모든 고속도로 진입 시점은 배열의 첫번째 고속도로 진입 시점보다 더 뒤에 있습니다. 그러므로 우리는 | ||
// 나간시점만 검사 해주면 됩니다. | ||
|
||
// 먼저 첫번째 routes의 고속도로를 빠져나간 시점을 out 변수에 담아줍니다. | ||
// 이 out 변수를 두번째 routes의 고속도로를 빠져나간 시점과 비교하여 out 변수보다 route[i][1]가 크면 ( 나간 시간이 느리면) | ||
// cctv를 하나 늘려줍니다. , out 변수를 갱신 하며 세번째, 네번째도 계속 비교해줍니다. |