-
-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
}; | ||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
}; |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 문제를 리뷰하면서 파이썬에서는 key-value를 다룰 때 dictionary를 사용하는데, 자바스크립트에서는 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아, 맞아요. |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. 성의 있는 리뷰 감사드려요! 화이팅입니다:) |
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.
자세한 답변 감사합니다! 리뷰하면서 공부가 많이 되었어요 👍