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

[jdy8739] week 8 #955

Merged
merged 9 commits into from
Jan 31, 2025
Merged

[jdy8739] week 8 #955

merged 9 commits into from
Jan 31, 2025

Conversation

jdy8739
Copy link
Contributor

@jdy8739 jdy8739 commented Jan 25, 2025

답안 제출 문제

  • 232
  • 244
  • 259
  • 274

체크 리스트

  • 우측 메뉴에서 PR을 Projects에 추가해주세요.
  • Projects의 오른쪽 버튼(▼)을 눌러 확장한 뒤, Week를 현재 주차로 설정해주세요.
  • 바로 앞에 PR을 열어주신 분을 코드 검토자로 지정해주세요.
  • 문제를 모두 푸시면 프로젝트에서 StatusIn Review로 설정해주세요.
  • 코드 검토자 1분 이상으로부터 승인을 받으셨다면 PR을 병합해주세요.

@jdy8739 jdy8739 requested a review from a team as a code owner January 25, 2025 15:42
@github-actions github-actions bot added the js label Jan 25, 2025
@jdy8739 jdy8739 requested a review from imsosleepy January 25, 2025 15:43
Copy link
Contributor

@obzva obzva left a comment

Choose a reason for hiding this comment

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

안녕하세요 이번 한 주도 고생 많으셨습니다.
설 연휴임에도 불구하고 4문제나 풀어주셨네요 :)
코멘트를 남겨 놓았으니 병합 전에 확인하고 병합 진행 부탁 드립니다
추가적인 코멘트 교환을 원하신다면 re-request review해주세요!


// 재귀를 활용한 dfs를 사용한 풀이
// 시간복잡도 O(v + e) -> v(노드의 수) + e(간선의 수) 만큼 탐색을 수행
// 공간복잡도 O(n) -> map이 총 노드의 수 만큼 크기를 가짐
Copy link
Contributor

Choose a reason for hiding this comment

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

  • 공간복잡도 분서 결과에는 영향을 미치지 않지만, 재귀호출 스택에 대한 언급도 해주면 좋을 것 같아요!
  • 시간복잡도에서 노드의 수를 v라고 표현하셨으니까 공간복잡도도 O(v)라고 써주면 더 나을 것 같아요

nodeMap.set(clone.val, clone);

for (const nei of nodeParam.neighbors) {
clone.neighbors = [...clone.neighbors, dfs(nei)];
Copy link
Contributor

Choose a reason for hiding this comment

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

Array를 매번 새로 만들어주는 대신에 새 원소를 push해주는 방식은 어떤가요?

Suggested change
clone.neighbors = [...clone.neighbors, dfs(nei)];
clone.neighbors.push(dfs(nei));

Copy link
Contributor Author

Choose a reason for hiding this comment

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

넵 풀이에 그렇게 push로 원소를 넣어 주던데 그 방식이 좀 더 간결하고 성능도 나은 것 같습니다 :)


// 큐를 활용한 bfs를 사용한 풀이
// 시간복잡도 O(v + e) -> v(노드의 수) + e(간선의 수) 만큼 탐색을 수행
// 공간복잡도 O(n) -> map이 총 노드의 수 만큼 크기를 가짐
Copy link
Contributor

Choose a reason for hiding this comment

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

시간복잡도에서 노드의 수를 v라고 표현하셨으니까 공간복잡도도 O(v)라고 써주면 더 나을 것 같아요

Copy link
Contributor Author

Choose a reason for hiding this comment

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

그러네요 수정해서 커밋하겠습니다. 감사합니다! :)

Comment on lines +58 to +59
// 이 queue에는 클론이 아니라 원본 노드가 들어가야함(neighbors에 대한 참조 때문)
const queue = [node];
Copy link
Contributor

Choose a reason for hiding this comment

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

알고리즘은 정상적으로 동작하지만 해당 Array는 queue가 아니네요 (stack처럼 동작해요!)

return dfs(0, 0);
};

// 시간복잡도 O(m * n) -> 재귀를 통해 m과 n이 1씩 증가하면서 상대편의 m or n 번 째 인덱스 문자열을 비교하므로??? (잘 모르겠습니다. 리뷰어분이 여유되시면 설명부탁드립니다 ㅜㅜ)
Copy link
Contributor

Choose a reason for hiding this comment

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

어디까지 이해되셨고 어디부터 이해가 잘 안되시는지 Discussion에 올려주세요~!

const countOfOthers = currLength - maxCountOfChar;

// 현재 문자열의 길이에서 가장 많이 나오는 문자열의 수를 뺸 값인 countOfOthers가
// k보다 작으면 현재 문자열에서 start번 째 문자의 수를 map에서 감소시키고 star의 값을 증가시킨다.
Copy link
Contributor

Choose a reason for hiding this comment

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

typo) k보다 크면

Comment on lines +1 to +30
/**
* @param {number} n
* @return {number}
*/
var hammingWeight = function (n) {
const binary = [1];

while (binary[0] < n) {
const latest = binary[0] * 2;

binary.unshift(latest);
}

let count = 0;

for (let i = 0; i < binary.length; i++) {
if (binary[i] <= n) {
count++;
n = n - binary[i];
}

if (n === 0) {
break;
}
}

return count;
};

// 시간복잡도 O(logn) -> 이진탐색처럼 2씩 곱해가며 n을 넘어가는 가장 큰 수를 찾으므로
Copy link
Contributor

Choose a reason for hiding this comment

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

  1. bitwise연산자에 대해서 한 번 찾아보시는 걸 추천드립니다 e.g. Right shift. bitwise 연산자를 이용한 풀이가 떠오르지 않는다면 다른 분들 풀이를 참고해주세요~!
  2. 제가 알기론 unshift의 시간 복잡도는 O(1)이 아닙니다. (이 부분도 찾아보시는게 좋을 것 같아요) 따라서 해당 풀이의 시간 복잡도 분석도 다시 이뤄져야할 것 같다는 생각이 듭니다.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

js의 배열이 원소들이 연달아 주소값을 가지는 형태가 아니다보니 O(1)라고 생각했는데요, 한 번 알아보고 주석을 다시 달아 커밋하겠습니다. 코멘트 감사합니다!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

js의 unshift는 배열의 요소가 맨 앞에 추가될 때마다 뒤 모든 요소들의 index가 1 씩 추가되므로 O(n)의 복잡도를 갖는다는 것을 알게되었습니다!!

  1. JavaScript 배열의 내부 구조
    JavaScript 엔진(V8 기준)에서 배열은 두 가지 형태로 저장될 수 있습니다.

✅ (1) Packed Elements (연속된 메모리, 빠름)
배열이 숫자로만 이루어져 있고, 인덱스가 연속적이면
내부적으로 일반적인 동적 배열처럼 연속된 메모리에 저장됨
O(1)로 빠른 조회가 가능

let arr = [1, 2, 3, 4]; // Packed Elements 사용
✅ (2) Sparse Elements (비연속적, 느림)
배열이 중간에 빈 요소가 있거나, 숫자가 아닌 객체가 포함된 경우
내부적으로 해시 테이블(Hash Map)처럼 동작
요소를 조회할 때 해시 테이블에서 검색해야 하므로 느려짐 (O(1) → O(n))

let arr = [];
arr[0] = 1;
arr[100] = 2; // 희소 배열 (Sparse Array)
✅ 위처럼 인덱스가 연속되지 않으면 메모리가 연속적이지 않음!

@jdy8739 jdy8739 merged commit a747d1f into DaleStudy:main Jan 31, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Completed
Development

Successfully merging this pull request may close these issues.

2 participants