-
-
Notifications
You must be signed in to change notification settings - Fork 245
[khyo] Week 1 #676
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
[khyo] Week 1 #676
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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,10 @@ | ||
// 풀이 | ||
// Set으로 중복 제거 후 nums와 길이 비교 | ||
|
||
// TC : O(N) | ||
// SC : O(N) | ||
|
||
var containsDuplicate = function(nums) { | ||
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,26 @@ | ||
// 풀이 | ||
// dp를 이용한 풀이 | ||
// 점화식 : dp[index] = Math.max(dp[index-1], dp[index-2] + nums[index]) | ||
// 사용되는 변수가 index-1, index-2 뿐이라 불필요한 배열을 제거하고자 함. | ||
|
||
// TC : O(N) | ||
// SC : O(1) | ||
|
||
var rob = function(nums) { | ||
let dp = new Array(2); | ||
|
||
dp[0] = nums[0] | ||
// nums의 길이가 1인 경우 예외처리 | ||
dp[1] = nums.length > 1 ? Math.max(nums[0], nums[1]) : nums[0] | ||
|
||
nums.forEach((num, index) => { | ||
if(index <= 1) return; | ||
|
||
let temp = Math.max(dp[1], dp[0] + nums[index]) | ||
dp[0] = dp[1] | ||
dp[1] = temp | ||
}) | ||
|
||
return dp[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,50 @@ | ||
// 정렬을 이용한 풀이 | ||
// TC : O(NlogN) | ||
// SC : O(N) | ||
|
||
var longestConsecutiveUsingSort = function (nums) { | ||
if (nums.length === 0) return 0; | ||
|
||
const uniqueNums = [...new Set(nums)].sort((a, b) => a - b); | ||
|
||
let max_cnt = 0; | ||
let cnt = 1; | ||
for (let i = 1; i < uniqueNums.length; ++i) { | ||
if (uniqueNums[i - 1] + 1 == uniqueNums[i]) { | ||
cnt += 1; | ||
} else { | ||
max_cnt = cnt > max_cnt ? cnt : max_cnt; | ||
cnt = 1; | ||
} | ||
} | ||
max_cnt = cnt > max_cnt ? cnt : max_cnt; | ||
return max_cnt; | ||
}; | ||
|
||
// 집합을 이용한 풀이 | ||
// TC : O(N) | ||
// SC : O(N) | ||
|
||
var longestConsecutive = function (nums) { | ||
if (nums.length === 0) return 0; | ||
|
||
const numSet = new Set(nums); | ||
let maxLength = 0; | ||
|
||
for (const num of numSet) { | ||
if (!numSet.has(num - 1)) { | ||
let currentNum = num; | ||
let currentLength = 1; | ||
|
||
while (numSet.has(currentNum + 1)) { | ||
currentNum++; | ||
currentLength++; | ||
} | ||
|
||
maxLength = Math.max(maxLength, currentLength); | ||
} | ||
} | ||
|
||
return maxLength; | ||
}; | ||
|
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,26 @@ | ||
// 풀이 | ||
// 1. 각 요소의 빈도를 계산하여 countObject 생성 | ||
// 2. countObject를 정렬하여 상위 k개의 요소 추출 | ||
// 3. 추출된 요소를 배열로 반환 | ||
|
||
// TC : O(NlogN) | ||
// SC : O(N) | ||
|
||
var topKFrequent = function(nums, k) { | ||
const countObject = {} | ||
nums.map((num) => { | ||
|
||
if(countObject[num]) { | ||
countObject[num] += 1 | ||
}else { | ||
countObject[num] = 1 | ||
} | ||
}) | ||
|
||
const sortedObject = Object.entries(countObject) | ||
.sort((a,b) => b[1] - a[1]) | ||
.slice(0, k) | ||
.map(item => Number(item[0])) | ||
|
||
return sortedObject | ||
}; | ||
|
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,24 @@ | ||
// 풀이 | ||
// 1. 영어, 숫자만 남기고 소문자로 변환한 newStr 생성 | ||
// 2. 투포인터를 이용해 문자열 팰린드롬 여부 확인 (초기값 : true) | ||
// 3. 중간에 팰린드롬이 아닌 지점을 발견하면 flag를 false로 변경 후 return | ||
|
||
// TC : O(N) | ||
// SC : O(N) | ||
|
||
var isPalindrome = function(s) { | ||
const newStr = s.replace(/[^a-zA-Z0-9]/g, '').toLowerCase(); | ||
let flag = true | ||
let left = 0, right = newStr.length - 1 | ||
while(left < right){ | ||
if(newStr[left] === newStr[right]) { | ||
left += 1; | ||
right -= 1; | ||
}else { | ||
flag = false; | ||
break; | ||
} | ||
} | ||
return flag | ||
}; | ||
|
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
안녕하세요 파일을 다른 폴더에 생성하신 것 같아요
longest-consecutive-sequence 폴더 아래에 생성해주세요~
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.
넵! 반영했습니다.