Skip to content

[anniemon78] Week 1 #657

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

Merged
merged 4 commits into from
Dec 14, 2024
Merged
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
23 changes: 23 additions & 0 deletions contains-duplicate/anniemon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* 시간 복잡도:
* 맵에서 nums[i]를 찾거나 삽입하는 데 걸리는 시간 O(1) * n(nums.length)
* 즉, O(n)
* 공간 복잡도:
* 최대 map의 크기는 nums.length만큼
* 즉, O(n)
*/
/**
* @param {number[]} nums
* @return {boolean}
*/
var containsDuplicate = function(nums) {
const map = new Map();
for(let i = 0; i < nums.length; i++) {
if(!map.has(nums[i])) {
map.set(nums[i], i);
} else {
return true;
}
}
return false;
};
Comment on lines +13 to +23
Copy link
Contributor

Choose a reason for hiding this comment

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

안녕하세요! 자바스크립트 언어를 다뤄본 적이 없어서 리뷰를 어떻게 할지 고민하다가, 지피티에게 Python 언어와의 비교를 부탁하여 속성으로 공부해봤습니다. 그 과정에서 Map이 Python의 set과 달리 키-값을 함께 저장한다는 점을 알게 되었는데, 어떤 알고리즘에서 효과적으로 쓰이는지 설명 부탁드려도 될까요?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

안녕하세요. 공부까지 하셨다니 감사드려요!
자바스크립트의 Map은 기본적으로 해시 테이블이라서, 파이썬에서는 딕셔너리랑 비슷한 걸로 알고 있어요.
따라서 키-밸류를 기반으로 조회/삽입 등이 필요한 경우 사용하므로, 특정 알고리즘에 국한되기보다 광범위하게 사용되는 걸로 알고 있습니다:)

Copy link
Contributor

@KwonNayeon KwonNayeon Dec 14, 2024

Choose a reason for hiding this comment

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

자세한 답변 감사합니다! 리뷰하면서 공부가 많이 되었어요 👍

30 changes: 30 additions & 0 deletions longest-consecutive-sequence/anniemon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* 시간 복잡도:
* set의 요소 중 연속하는 시퀀스의 첫번째 숫자일 때만
* while 루프를 실행
* 따라서 요소당 최대 1회 순회
* 즉, 시간 복잡도는 O(n)
* 공간 복잡도:
* set은 중복이 없을 경우 최대 O(n)를 차지함
* 즉, 공간 복잡도는 O(n)
*/
/**
Comment on lines +1 to +11
Copy link
Contributor

Choose a reason for hiding this comment

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

문제마다 시간/공간 복잡도에 대해 자세히 설명해주셔서 이해하기 쉬웠습니다!

* @param {number[]} nums
* @return {number}
*/
var longestConsecutive = function (nums) {
const set = new Set(nums);
let res = 0;
for (const n of set) {
let seqLen = 0, target = n;
const isSeqStart = !set.has(target - 1);
if (isSeqStart) {
while (set.has(target)) {
target++;
seqLen++;
}
}
res = Math.max(seqLen, res);
}
return res;
};
24 changes: 24 additions & 0 deletions top-k-frequent-elements/anniemon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* 시간 복잡도:
* nums.length만큼의 배열을 정렬해야 하므로
* 즉, O(n * log n)
* 공간 복잡도:
* 최대 nums.length만큼의 map 생성
* 즉, O(n)
*/
/**
* @param {number[]} nums
* @param {number} k
* @return {number[]}
*/
var topKFrequent = function(nums, k) {
const map = new Map();
for(const n of nums) {
if(!map.has(n)) {
map.set(n, 0);
}
map.set(n, map.get(n) + 1)
}
const sorted = Array.from(map).sort((a, b) => b[1]-a[1]);
return sorted.slice(0, k).map(e => e[0])
};
Comment on lines +14 to +24
Copy link
Contributor

Choose a reason for hiding this comment

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

이 문제를 리뷰하면서 파이썬에서는 key-value를 다룰 때 dictionary를 사용하는데, 자바스크립트에서는 Map이 그 역할을 한다는 걸 알게 됐습니다. 또, if문 안에서 값을 초기화하는 방식이 조금 생소하게 느껴졌어요. 파이썬에서는 보통 defaultdict를 사용해서 초기화를 미리 처리하잖아요. @anniemon님 코드를 리뷰하면서 자바스크립트에 대해 조금 더 알게 되었습니다!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

아, 맞아요.
저도 파이썬 잠깐 써봤을 때 그 부분이 헷갈렸던 기억이 나네요.
자바스크립트가 그런 면에서 좀 편하긴 한데, 엄격하지 않아서 근본 없다고 싫어하는 사람도 많은 거 같습니다.ㅎㅎㅎ

34 changes: 34 additions & 0 deletions valid-palindrome/anniemon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* 시간 복잡도:
* s.length만큼 탐색. 즉, O(n)
* 공간 복잡도:
* 변수와 함수만 저장. 즉, O(1)
*/

/**
* @param {string} s
* @return {boolean}
*/
var isPalindrome = function(s) {
const isAlphaNumeric = (v) => {
return (/^[a-z0-9]$/i).test(v);
};

let l = 0;
let r = s.length - 1;
while(l < r) {
while(!isAlphaNumeric(s[l]) && l < r) {
l++;
}
while(!isAlphaNumeric(s[r]) && l < r) {
r--;
}

if(s[l].toLowerCase() !== s[r].toLowerCase()) {
return false;
}
l++;
r--;
}
return true;
};
Comment on lines +12 to +34
Copy link
Contributor

Choose a reason for hiding this comment

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

코드를 완전히 이해하지는 못했지만, @anniemon님의 코드를 보고 저도 공간 효율성을 높이기 위해 투 포인터 방식으로 문제를 다시 풀어봐야겠다는 아이디어를 얻었습니다! 감사합니다 :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

성의 있는 리뷰 감사드려요! 화이팅입니다:)

Loading