-
-
Notifications
You must be signed in to change notification settings - Fork 195
[jj7779607] Week1 #637
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
[jj7779607] Week1 #637
Changes from all commits
a674237
73b1341
b5adf9a
4c83904
00585cd
9427373
600b87a
0a0bab9
de2bbe6
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 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {boolean} | ||
*/ | ||
var containsDuplicate = function(nums) { | ||
|
||
// 첫 번째 방법: filter + indexOf 사용 => indexOf(),filter() 각각 시간복잡도 O(n) 두 개가 중첩이므로 시간복잡도 O(n^2) | ||
// Runtime: Time Limit Exceeded 발생 | ||
const method1 = function() { | ||
const filterNums = nums.filter((item,index) => nums.indexOf(item) !== index); | ||
return filterNums.length > 0; | ||
} | ||
|
||
// 두 번째 방법: Set 사용 => nums 배열을 set으로 변환할 때 한번씩 확인하면 되므로 시간복잡도 O(n) | ||
// Runtime: 14ms | ||
const method2 = function() { | ||
const setNums = new Set(nums); | ||
return setNums.size !== nums.length; | ||
} | ||
|
||
// 위 두 가지 방법 중 Set을 사용하는 것이 성능상 훨씬 나음 | ||
return method2(); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
var rob = function(nums) { | ||
// 1. nums 배열 0일 때와 1일 때 | ||
if (nums.length === 0) return 0; | ||
if (nums.length === 1) return nums[0]; | ||
|
||
// 2. i=1일 때 | ||
nums[1] = Math.max(nums[0], nums[1]); | ||
|
||
// 3. i=2일 때부터 for문 순회 | ||
for (let i=2; i<nums.length; i++) { | ||
nums[i] = Math.max(nums[i-2] + nums[i], nums[i-1]) | ||
} | ||
return nums[nums.length-1]; | ||
}; | ||
|
||
// 시간복잡도와 공간복잡도 | ||
// 시간복잡도: 배열의 길이 n 만큼 for문 순회하므로 -> O(n) | ||
// 공간복잡도: nums 배열 그대로 수정하여 계산 -> O(1) | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
var longestConsecutive = function(nums) { | ||
// 첫 번째 정렬을 한다. | ||
if (nums.length === 0) { | ||
return 0; | ||
} | ||
|
||
nums.sort((a, b) => a - b); // 오름차순으로 정렬 | ||
console.log(nums); | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. console을 찍어넣고 제거를 하지 못했네요 |
||
|
||
// 두 번째 숫자가 1씩 계속해서 증가하는 구간을 찾는다. | ||
let longest = 0; | ||
let length = 1; | ||
|
||
for (let i=0; i<nums.length-1; i++) { | ||
if (nums[i] === nums[i+1]) { | ||
continue; | ||
} | ||
// 연속해서 1씩 증가하는 구간 찾기 | ||
if (nums[i+1] - nums[i] === 1) { | ||
length += 1; | ||
} else { | ||
longest = Math.max(longest, length); | ||
length = 1; | ||
} | ||
} | ||
return Math.max(longest, length); | ||
}; | ||
|
||
// 시간복잡도와 공간복잡도 | ||
// 시간복잡도: 정렬 사용(O(nlogn)) + for문으로 배열 탐색(O(n)) = O(nlogn) | ||
// 공간복잡도: O(1) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @param {number} k | ||
* @return {number[]} | ||
*/ | ||
var topKFrequent = function(nums, k) { | ||
let map = new Map(); // key에는 해당되는 숫자, value에는 해당되는 숫자의 갯수 | ||
|
||
// 숫자별 빈도수 | ||
nums.forEach((num) => { | ||
map.set(num, (map.get(num) || 0) + 1); | ||
}); | ||
|
||
// 결과를 배열로 바꿔주고 내림차순 정렬 | ||
let sortedResult = Array.from(map.entries()).sort((a,b) => b[1] - a[1]); | ||
//console.log(sortedResult); | ||
//console.log(sortedResult.slice(0,k)); | ||
|
||
// k갯수의 숫자 반환 | ||
return sortedResult.slice(0,k).map(item => item[0]); | ||
}; | ||
|
||
// 여기서 위 코드의 시간복잡도와 공간복잡도를 생각해보자... | ||
// 시간복잡도 => O(m log m) ** 더 개선할 방법 찾아보기 | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. 아직 못 찾아 풀이와 다른 분들의 풀이 방법을 참고하려고 합니다! |
||
// forEach 통한 배열 순회: 배열의 크기가 n이라고 한다면 O(n) | ||
// sortedResult (정렬): O(m log m) | ||
// k갯수의 숫자 반환: k개 항목에 대해 연산하니까 O(k) | ||
|
||
// 공간복잡도 => O(n) | ||
// map의 크기: 배열의 크기가 n이라고 한다면 O(n) | ||
// sortedResult: O(m) | ||
// k개 숫자 저장: O(k) |
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,23 @@ | ||||||||||||||
/** | ||||||||||||||
* @param {string} s | ||||||||||||||
* @return {boolean} | ||||||||||||||
*/ | ||||||||||||||
var isPalindrome = function(s) { | ||||||||||||||
// 1. 문자열을 다 소문자로 바꾸고, 알파벳/숫자 아닌 거 다 제거 | ||||||||||||||
if (s.length === 1) { | ||||||||||||||
return true; | ||||||||||||||
} else { | ||||||||||||||
let lcLetter = s.toLowerCase().replace(/[^a-z0-9]/g, ''); | ||||||||||||||
//console.log(lcLetter); | ||||||||||||||
|
||||||||||||||
// 2. 문자열이 앞에서 시작할 때와 뒤에서 시작할 때 같으면 true, 아니면 false | ||||||||||||||
if(lcLetter) { | ||||||||||||||
for(let i=0; i<Math.floor(lcLetter.length/2); i++) { | ||||||||||||||
if (lcLetter[i] !== lcLetter[lcLetter.length - 1 - i]) return false; | ||||||||||||||
} | ||||||||||||||
return true; | ||||||||||||||
} else { | ||||||||||||||
return true; | ||||||||||||||
} | ||||||||||||||
Comment on lines
+18
to
+21
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. (안 중요) 요리하면 더 간단하지 않았을까 생각이 들었습니다.
Suggested change
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.
입력 배열을 재활용하셔서 메모리를 절약하시는 게 알고리즘 고수 같기도 하고 뭔가 멋지게 느껴집니다 😎
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.
아닙니다ㅜㅜ 완전 알고리즘 초보입니다ㅜㅜ
리뷰 감사합니다!