Skip to content

[week 6] 주간 결산_연서 #20

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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions 연서/week6/level1/가장가까운같은글자.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function solution(s) {
let answer = [];

let strings = new Map();

s.split("").map((el, idx) => {
if (strings.has(el)) {
answer.push(idx - strings.get(el));
} else {
answer.push(-1);
}
strings.set(el, idx);
});

return answer;
}
4 changes: 4 additions & 0 deletions 연서/week6/level1/내적.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function solution(a, b) {
// current value 인자에 _을 넣어서 빈 인자 값으로, idx 사용해서 곱하기
return a.reduce((acc, _, i) => (acc += a[i] * b[i]), 0);
}
19 changes: 19 additions & 0 deletions 연서/week6/level1/시저암호.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function solution(s, n) {
const answer = s
.split("")
.map((string) => {
const code = string.charCodeAt();
if (97 <= code && code <= 122) {
return String.fromCharCode(((code + n - 97) % 26) + 97);
}

if (65 <= code && code <= 90) {
return String.fromCharCode(((code + n - 65) % 26) + 65);
}

return " ";
})
.join("");

return answer;
}
11 changes: 11 additions & 0 deletions 연서/week6/level1/약수의개수와덧셈.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function solution(left, right) {
var answer = 0;
for (let i = left; i <= right; i++) {
if (Number.isInteger(Math.sqrt(i))) {
answer -= i;
} else {
answer += i;
}
}
return answer;
}
15 changes: 15 additions & 0 deletions 연서/week6/level1/카드뭉치.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function solution(cards1, cards2, goal) {
let answer = true;

goal.map((targetWord) => {
if (cards1[0] === targetWord) {
cards1.shift();
} else if (cards2[0] === targetWord) {
cards2.shift();
} else {
answer = false;
}
});

return answer ? "Yes" : "No";
}
70 changes: 70 additions & 0 deletions 연서/week6/level2/게임맵최단거리.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
function solution(maps) {
// 1. n 과 m 을 설정함
const n = maps.length;
const m = maps[0].length;

// 2. 정답 저장용
let answer = -1;

// 3. 방문을 체크할 배열
let visited = Array.from(Array(n), () => Array(m).fill(false));

// 4. BFS 를 하기 위한 queue, 초기 값을 저장해둠
let queue = [[0, 0, 1]];

// 5. queue 의 위치를 저장할 queueIndex
let queueIndex = 0;

// 6. x, y 가 움직일 배열을 저장함 (상, 우, 하, 좌)
const moveX = [-1, 0, 1, 0];
const moveY = [0, 1, 0, -1];

// 7. BFS 진행할 while 문
while (queue.length > queueIndex) {
// 8. 일단 queue 에 있는 값을 꺼냄
const now = queue[queueIndex];
// 9. 값을 꺼냈으므로 index 를 +1 해줌
queueIndex += 1;

// 10. 만약 꺼낸 값이 정답 (도착지) 이면
if (now[0] == n - 1 && now[1] == m - 1) {
// 11. answer 에 답을 저장함 (now[2] 는 이동 거리)
answer = now[2];
break;
}

// 12. 만약 꺼낸 값이 방문하지 않은 값이라면
if (!visited[now[0]][now[1]]) {
// 13. 방문
visit(now[0], now[1], now[2]);
}
}

// 14. 방문 함수. x, y 좌표와 count (이동거리) 를 파라미터로 받음
function visit(x, y, count) {
// 15. 먼저 방문했다고 체크함
visited[x][y] = true;

// 16. 현재 x, y 위치에서 상, 하, 좌, 우 로 이동할 반복문
for (let i = 0; i < moveX.length; i++) {
// 17. movedX, Y 로 설정함.
const movedX = x + moveX[i];
const movedY = y + moveY[i];

// 18. 만약, movedX, movedY 가 배열의 범위 안에 있고, 그 값 위치가 아직 방문하지 않았고, 그 위치를 방문할 수 있다면 (값이 1이라면)
if (
movedX >= 0 &&
movedX < n &&
movedY >= 0 &&
movedY < m &&
!visited[movedX][movedY] &&
maps[movedX][movedY] == 1
) {
// queue 에 그 값을 넣음
queue.push([x + moveX[i], y + moveY[i], count + 1]);
}
}
}

return answer;
}
14 changes: 14 additions & 0 deletions 연서/week6/level2/점프와순간이동.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function solution(n) {
let answer = 0;

while (n > 0) {
if (n % 2 === 0) {
n /= 2;
} else {
n--;
answer++;
}
}

return answer;
}
5 changes: 5 additions & 0 deletions 연서/week6/level2/최댓값과최솟값.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function solution(s) {
const arr = s.split(" ");

return Math.min(...arr) + " " + Math.max(...arr);
}
34 changes: 34 additions & 0 deletions 연서/week6/level2/튜플.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
function solution(s) {
const answer = [];
// 집합을 배열로 바꾸기(정규표현식 사용)
const arr = JSON.parse(s.replace(/{/g, "[").replace(/}/g, "]"));

// 요소 개수대로 오름차순 정렬
arr.sort((a, b) => a.length - b.length);

// 값이 있는지 확인 (중복 값인지 확인)
arr.forEach((col) => {
col.forEach((row) => {
if (!answer.includes(row)) answer.push(row);
});
});
return answer;
}

// 다른 풀이
const tupleFrom = (str) =>
str
.slice(2, -2)
.split("},{")
.map((it) => toNumbers(it))
.sort(accendingByLength)
.reduce(
(acc, cur) => [...acc, ...cur.filter((it) => !acc.includes(it))],
[]
);

const toNumbers = (str) => str.split(",").map((it) => Number(it));

const accendingByLength = (arr1, arr2) => arr1.length - arr2.length;

const solution = (s) => tupleFrom(s);
28 changes: 28 additions & 0 deletions 연서/week6/level2/피로도.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
function solution(k, dungeons) {
const length = dungeons.length;
const visited = Array(length).fill(false);
let answer = 0;
let temp = 0;

const dfs = (currentK, cnt) => {
for (let i = 0; i < length; i++) {
// 해당 던전을 아직 방문하지 않았고
// 던전의 최소 피로도가 현재 피로도보다 작거나 같으면
if (!visited[i] && currentK >= dungeons[i][0]) {
// 해당 던전 방문표시
visited[i] = true;
// 재귀
dfs(currentK - dungeons[i][1], cnt + 1);
// 추후 다시 방문해야 하므로 방문 표시 해제
visited[i] = false;
}
}
// 최대 던전 수 갱신
answer = Math.max(answer, cnt);
return;
};

dfs(k, 0);

return answer;
}