-
-
Notifications
You must be signed in to change notification settings - Fork 245
[reach0908] WEEK 01 solutions #1685
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 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6fdb0fa
solve: contains-duplicate
46057fc
solution: two sum
reach0908 b0fc4e3
solve: top-k-frequent-elements-1
reach0908 8e22356
solve: longest-consecutive-sequence
reach0908 dce519d
solve: longest-consecutive-sequence2 개선 문제 풀이 추가
reach0908 501b925
solve: house-robber 문제 풀이 추가 및 DP 방식으로 개선
reach0908 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,31 @@ | ||
/** | ||
* @description | ||
* time complexity: O(n log n) | ||
* space complexity: O(n) | ||
* runtime: 57ms | ||
* 풀이 방법: 중복을 제거하고 정렬한 다음에 연속된 숫자의 개수를 카운트하여 최대값을 반환한다. | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
const longestConsecutive = function (nums) { | ||
if (nums.length === 0) return 0; | ||
const sortedNums = [...new Set(nums)].sort((a, b) => a - b); | ||
|
||
let maxConsecutiveCount = 1; | ||
let currentConsecutiveCount = 1; | ||
|
||
for (let i = 1; i < sortedNums.length; i += 1) { | ||
if (sortedNums[i] === sortedNums[i - 1] + 1) { | ||
currentConsecutiveCount += 1; | ||
} else { | ||
currentConsecutiveCount = 1; | ||
} | ||
|
||
maxConsecutiveCount = Math.max( | ||
maxConsecutiveCount, | ||
currentConsecutiveCount | ||
); | ||
} | ||
|
||
return maxConsecutiveCount; | ||
}; |
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.
정렬이 꼭 필요한 문제는 아니라고 생각이 듭니다. 정렬을 쓰면 nlogn으로 시간복잡도가 올라가니깐 정렬 없는 풀이도 한번 생각해보시면 도움이 될 거 같습니다 :)
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.
해당 문제 한번 개선해보고 있습니다!