-
-
Notifications
You must be signed in to change notification settings - Fork 195
[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
[anniemon78] Week 1 #657
Conversation
안녕하세요! 저는 @anniemon님 바로 전에 PR을 올린 스터디 참가자입니다. 리뷰어 지정이 잘못 되어있는 것 같아서, 제가 임의로 수정하였습니다! 자바스크립트는 처음이지만, 열심히 리뷰해볼게요! |
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; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
안녕하세요! 자바스크립트 언어를 다뤄본 적이 없어서 리뷰를 어떻게 할지 고민하다가, 지피티에게 Python 언어와의 비교를 부탁하여 속성으로 공부해봤습니다. 그 과정에서 Map
이 Python의 set
과 달리 키-값을 함께 저장한다는 점을 알게 되었는데, 어떤 알고리즘에서 효과적으로 쓰이는지 설명 부탁드려도 될까요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
안녕하세요. 공부까지 하셨다니 감사드려요!
자바스크립트의 Map은 기본적으로 해시 테이블이라서, 파이썬에서는 딕셔너리랑 비슷한 걸로 알고 있어요.
따라서 키-밸류를 기반으로 조회/삽입 등이 필요한 경우 사용하므로, 특정 알고리즘에 국한되기보다 광범위하게 사용되는 걸로 알고 있습니다:)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
자세한 답변 감사합니다! 리뷰하면서 공부가 많이 되었어요 👍
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]) | ||
}; |
There was a problem hiding this comment.
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님 코드를 리뷰하면서 자바스크립트에 대해 조금 더 알게 되었습니다!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
아, 맞아요.
저도 파이썬 잠깐 써봤을 때 그 부분이 헷갈렸던 기억이 나네요.
자바스크립트가 그런 면에서 좀 편하긴 한데, 엄격하지 않아서 근본 없다고 싫어하는 사람도 많은 거 같습니다.ㅎㅎㅎ
/** | ||
* 시간 복잡도: | ||
* set의 요소 중 연속하는 시퀀스의 첫번째 숫자일 때만 | ||
* while 루프를 실행 | ||
* 따라서 요소당 최대 1회 순회 | ||
* 즉, 시간 복잡도는 O(n) | ||
* 공간 복잡도: | ||
* set은 중복이 없을 경우 최대 O(n)를 차지함 | ||
* 즉, 공간 복잡도는 O(n) | ||
*/ | ||
/** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
문제마다 시간/공간 복잡도에 대해 자세히 설명해주셔서 이해하기 쉬웠습니다!
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; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
코드를 완전히 이해하지는 못했지만, @anniemon님의 코드를 보고 저도 공간 효율성을 높이기 위해 투 포인터 방식으로 문제를 다시 풀어봐야겠다는 아이디어를 얻었습니다! 감사합니다 :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
성의 있는 리뷰 감사드려요! 화이팅입니다:)
답안 제출 문제
체크 리스트
In Review
로 설정해주세요.