Skip to content
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

[최현웅] 3주차 문제 해결 #13

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

[최현웅] 3주차 문제 해결 #13

wants to merge 2 commits into from

Conversation

hwinkr
Copy link
Collaborator

@hwinkr hwinkr commented Nov 17, 2023

🖊️ 해결과정 기록

1️⃣ [1차]프렌즈 4블록 (1h)

💭 어떻게 접근했나요?

  • 애니팡과 유사한 게임을 구현하는 문제였다.
  • 한 블럭씩 순회하면서 2x2 블럭을 만들 수 있는지 판단하고, 만들 수 있다면 -로 변경해 파괴되었음을 표시한다.
  • 그 후, 파괴되지 않은 블럭들과 위치를 변경한다
  • 파괴할 수 있을 때 까지 이 과정을 반복한다

2️⃣ 연구소(1h 30m)

💭 어떻게 접근했나요?

  • 만들 수 있는 벽의 모든 경우의 수를 구하고, 바이러스를 퍼뜨린 후 안전영역의 최댓값을 업데이트 하는 방식으로 해결했다.

💪🏻 무엇을 얻었나요?

  • 알고리즘을 풀 때 사용하는 언어를 자바스크립트로 바꿔서 아직 어색하고 적응하기가 좀 힘들었다. 😂
  • 자바스크립트로 풀 때 최대한 내장 API(map, forEach, reduce, filter)를 활용하니 선언적인 코드가 된 것 같다.

자바스크립트의 배열은 객체다.

문제를 해결할 때 중복 제거가 필요해서 Set를 활용했는데, 자바스크립트에서 배열은 객체이고 객체를 비교할 때는 메모리 주소로 비교하므로 의도대로 중복제거가 되지 않았다. 객체를 비교할 때는 메모리 주소로 비교한다는 것을 알고 있었지만 막상 적용할 때는 떠오르지가 않아서 실수를 했다. 문제를 해결한 후, 자바스크립트 Set에 대해서 다시 알아보는 시간을 가졌다.(링크)

자바스크립트 2차원 배열 복사하기

자바스크립트는 spread 연산자를 사용해서 배열을 복사할 수 있다.

const arr = [1, 2, 3]
const copyArr = [...arr]

하지만 2차원 배열 같은 경우는 제대로 복사되지 않는다. 따라서 map을 활용해서 각 행에 있는 1차원 배열들을 따로 복사해줘야 한다.(링크)

🎸 기타 추가사항

@hwinkr hwinkr self-assigned this Nov 17, 2023
Copy link

@Cha-Ji Cha-Ji left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

항상 깔끔하시군요. 일급객체, 순수함수, 커링 등 함수형 프로그래밍에 대해 공부해보셔도 좋을 것 같네요!
모던 ~~ 책들에 들어있을 것 같네요.

Comment on lines +9 to +13
const getUniquePositions = (arr) => {
return [...new Set(arr.join("|").split("|"))].map((v) =>
v.split(",").map((v) => Number(v))
);
};
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set 없이 중복제거 해보시는건 어떤가요!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

생각해보니 map메서드를 활용하면 Set을 사용하지 않고도 충분히 중복제거를 할 수 있겠네요. map으로도 구현해보고 둘의 차이를 정리해 봐야겠습니다!

Copy link
Contributor

@mjj111 mjj111 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

너무 고생하셨습니다 👍

process.exit();
});

const VIRUS = 2;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이렇게 매직넘버를 처리할 수 있었네요... 배워갑니다..👍

const targetPositions = graph.reduce((accVirus, row, i) => {
const virusInRow = row
.map((cell, j) => cell === target && [i, j])
.filter((value) => !!value);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

자바스크립트에서는 이런식으로 stream처럼 사용하면 안느린가요??
자바 코테에서는 이렇게 사용하면 느려서 시간초과가 되는 경우가 꽤 있어서 궁금했습니다.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아직 속도에 대한 고민은 해보지 않았지만, 지연평가를 사용해서 속도를 충분히 개선시킬 수 있다고 합니다. 아직, 지연평가에 대해선 공부해보지는 않았는데 메서드 체이닝으로 인해서 시간초과가 발생 한다면 지연 평가를 적용해볼까 합니다 :)

@hwinkr
Copy link
Collaborator Author

hwinkr commented Nov 18, 2023

항상 깔끔하시군요. 일급객체, 순수함수, 커링 등 함수형 프로그래밍에 대해 공부해보셔도 좋을 것 같네요! 모던 ~~ 책들에 들어있을 것 같네요.

최근 함수형 프로그래밍 학습에 대한 필요성을 느껴서 조금씩 공부해보고 있는 중입니다! 선언적으로 UI를 다루는 리액트를 계속해서 사용하다보니 함수형 프로그래밍에 대해서 더 잘 알면 리액트를 더 잘 쓸 수 있을 것 같더라구요!

Copy link
Contributor

@mimpie mimpie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

js로 하시니 많이 배워갑니다! 블로그 글도 새롭게 알게되어 도움이 많이 되었습니다 감사합니다!

}
};

const checkDestructibleBlocks = (positions, board, targetPosition) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

같은 블록인지 확인하고 배열에 추가하는 코드를 함수로 빼고 배열 메소드를 사용하니 깔끔하고 잘 읽히는 것 같습니다! positions.push(...destoryedPositions) 이렇게 배열에 추가하는 방법도 배워갑니다!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants