-
-
Notifications
You must be signed in to change notification settings - Fork 195
[uraflower] WEEK 01 solutions #1128
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ea47b64
[ PS ] : contains duplicate
uraflower 3e0e8b8
[ PS ] : contains duplicate 주석 추가
uraflower bc47794
[ PS ] : Two Sum
uraflower b23ec77
[ PS ] : Top K Frequent Elements
uraflower 96a9c88
[ PS ] : Longest Consecutive Sequence
uraflower 8249fd7
[ PS ] : House Robber
uraflower 333d742
Rename House Robber.js to uraflower.js
uraflower File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* 입력 배열 내 값 중복 여부를 반환하는 함수 | ||
* @param {number[]} nums | ||
* @return {boolean} | ||
*/ | ||
const containsDuplicate = function (nums) { | ||
const set = new Set(); | ||
|
||
for (const num of nums) { | ||
if (set.has(num)) return true; | ||
else set.add(num); | ||
} | ||
|
||
return false; | ||
}; | ||
|
||
// 시간복잡도: O(n) | ||
// 공간복잡도: O(n) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* 주어진 배열에서 인접하지 않은 숫자들의 최대 합을 반환하는 함수 | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
const rob = function(nums) { | ||
const dp = []; | ||
|
||
nums.forEach((num, idx) => { | ||
dp[idx] = Math.max((dp[idx - 2] || 0) + num, dp[idx - 1] || 0); | ||
}); | ||
|
||
return dp[dp.length - 1]; | ||
}; | ||
|
||
// 시간복잡도: O(n); | ||
// 공간복잡도: O(n); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* 주어진 배열의 숫자들로 만들 수 있는 가장 긴 연속 수열의 길이를 반환하는 함수 | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
const longestConsecutive = function(nums) { | ||
const sorted = Array.from(new Set(nums)).sort((a, b) => Number(a) - Number(b)); | ||
|
||
let maxLength = 0; | ||
let currentSequenceLength = 0; | ||
|
||
for (let i = 0; i < sorted.length; i++) { | ||
if (i === 0) { | ||
maxLength = 1; | ||
currentSequenceLength = 1; | ||
continue; | ||
} | ||
|
||
if (sorted[i] === sorted[i - 1] + 1) { | ||
currentSequenceLength += 1; | ||
} else { | ||
currentSequenceLength = 1; | ||
} | ||
|
||
maxLength = Math.max(maxLength, currentSequenceLength); | ||
} | ||
|
||
return maxLength; | ||
}; | ||
|
||
// 시간복잡도: O(n) | ||
// 공간복잡도: O(n) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* 주어진 배열에서 가장 많이 속해있는 숫자 k개를 반환하는 함수 | ||
* @param {number[]} nums | ||
* @param {number} k | ||
* @return {number[]} | ||
*/ | ||
const topKFrequent = function(nums, k) { | ||
const count = {}; | ||
nums.forEach((num) => { | ||
count[num] = count[num] + 1 || 1; | ||
}); | ||
return Object.keys(count).sort((a, b) => count[b] - count[a]).slice(0, k).map(Number); | ||
}; | ||
|
||
// 시간복잡도: O(n) | ||
// 공간복잡도: O(n) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* 주어진 배열 중 두 숫자의 합이 타겟일 때, 두 숫자의 인덱스를 반환하는 함수 | ||
* @param {number[]} nums | ||
* @param {number} target | ||
* @return {number[]} | ||
*/ | ||
const twoSum = function (nums, target) { | ||
const map = new Map(); | ||
for (let i = 0; i < nums.length; i++) { | ||
const diff = target - nums[i]; | ||
if (map.has(diff)) { | ||
return [map.get(diff), i]; | ||
} | ||
map.set(nums[i], i); | ||
} | ||
}; | ||
|
||
// 시간복잡도: O(n) | ||
// 공간복잡도: O(n) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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을 사용해 구현하신 걸 보고 Set을 활용할 수도 있었겠구나, 라는 생각을 했습니다!
1주차 고생하셨습니다~!