-
-
Notifications
You must be signed in to change notification settings - Fork 195
[DoDo] Week 1 #655
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
[DoDo] Week 1 #655
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5f1eefe
217. Contains Duplicate
y00eunji 867cdbe
125. Valid Palindrome
y00eunji d5c86da
125. Valid Palindrome
y00eunji 116161f
347. Top K Frequent Elements
y00eunji 30a1246
128. Longest Consecutive Sequence
y00eunji d9a5988
347. Top K Frequent Elements
y00eunji 5d2718a
198. House Robber
y00eunji 23502e0
198. House Robber
y00eunji 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,9 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {boolean} | ||
*/ | ||
var containsDuplicate = function(nums) { | ||
// set에 넣어 중복을 줄이고 길이를 비교한다. | ||
|
||
return new Set(nums).size !== nums.length; | ||
}; |
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,23 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
var rob = function(nums) { | ||
const len = nums.length; | ||
if (len === 0) return 0; | ||
if (len === 1) return nums[0]; | ||
if (len === 2) return Math.max(nums[0], nums[1]); | ||
|
||
const dp = Array(len).fill(0); | ||
dp[0] = nums[0]; | ||
dp[1] = Math.max(nums[0], nums[1]); | ||
|
||
// 현재 집을 터는 경우와 안 터는 경우 중 최대값 선택 | ||
// 1. 이전 집까지의 최대 금액 (현재 집 스킵) | ||
// 2. 전전 집까지의 최대 금액 + 현재 집 금액 (현재 집 선택) | ||
for (let i = 2; i < len; i++) { | ||
dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]); | ||
} | ||
|
||
return dp[len - 1]; | ||
}; |
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,27 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
var longestConsecutive = function(nums) { | ||
const numSet = new Set(nums); | ||
let longestStreak = 0; | ||
|
||
for (const num of numSet) { | ||
// 현재 숫자가 연속 시퀀스의 시작점인지 확인 | ||
// 즉, num-1이 set에 없어야 함 | ||
if (!numSet.has(num - 1)) { | ||
let currentNum = num; | ||
let currentStreak = 1; | ||
|
||
// 현재 숫자의 연속된 다음 숫자들을 찾음 | ||
while (numSet.has(currentNum + 1)) { | ||
currentNum += 1; | ||
currentStreak += 1; | ||
} | ||
|
||
longestStreak = Math.max(longestStreak, currentStreak); | ||
} | ||
} | ||
|
||
return longestStreak; | ||
}; |
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,20 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @param {number} k | ||
* @return {number[]} | ||
*/ | ||
var topKFrequent = function(nums, k) { | ||
// 1. nums 배열을 순회하여 각 숫자의 빈도를 계산하고 obj 객체에 저장 | ||
const obj = nums.reduce((acc, cur) => { | ||
acc[cur] = (acc[cur] || 0) + 1; | ||
return acc; | ||
}, {}); | ||
|
||
// 2. obj 객체의 키-값 쌍을 배열로 변환하고, 값을 기준으로 내림차순 정렬, k개 추출 | ||
const frequentArr = Object.entries(obj) | ||
.sort(([, valueA], [, valueB]) => valueB - valueA) | ||
.slice(0, k) | ||
.map(([key]) => +key); | ||
|
||
return frequentArr; | ||
}; |
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,10 @@ | ||
/** | ||
* @param {string} s | ||
* @return {boolean} | ||
*/ | ||
var isPalindrome = function(s) { | ||
const filtered = s.toLowerCase().replace(/[^a-zA-Z0-9]/g, ''); | ||
const reversed = filtered.split('').reverse().join(''); | ||
|
||
return filtered === reversed; | ||
}; |
Oops, something went wrong.
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.
안녕하세요~ 😊
해당 점화식을 잘 살펴보면
dp
의i, i-1, i-2
번 요소만 사용하고 있다는 걸 알 수 있습니다아직 시간이 좀 남았으니 공간 복잡도 최적화에 도전해보시면 어떨까요?