-
-
Notifications
You must be signed in to change notification settings - Fork 245
[Helena] Week 1 #690
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
[Helena] Week 1 #690
Changes from all commits
Commits
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 @@ | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
|
||
class Solution: | ||
def containsDuplicate(self, nums: List[int]) -> bool: | ||
# set to keep track of duplicates. | ||
duplicates = set() | ||
|
||
# go through each number in the list | ||
for num in nums: | ||
# if it's a duplicate, return true. | ||
if num in duplicates: | ||
return True | ||
# otherwise, add it to the set to check for duplicates. | ||
duplicates.add(num) | ||
|
||
# if finish the loop and don't find duplicates, return false. | ||
return False |
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,25 @@ | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
|
||
class Solution: | ||
def longestConsecutive(self, nums: List[int]) -> int: | ||
# convert list to set to remove duplicates and allow quick lookups | ||
num_set = set(nums) | ||
longest_streak = 0 | ||
|
||
# loop through each number in the set | ||
for num in num_set: | ||
# only start counting if it's the beginning of a sequence | ||
if num - 1 not in num_set: | ||
current_num = num | ||
current_streak = 1 | ||
|
||
# keep counting while the next number in the sequence exists | ||
while current_num + 1 in num_set: | ||
current_num += 1 | ||
current_streak += 1 | ||
|
||
# update the longest streak found so far | ||
longest_streak = max(longest_streak, current_streak) | ||
|
||
return longest_streak |
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 @@ | ||
# Time Complexity: O(n log n) | ||
# Space Complexity: O(n) | ||
|
||
class Solution: | ||
def topKFrequent(self, nums: List[int], k: int) -> List[int]: | ||
# count how many times each number appears | ||
counts = {} | ||
for num in nums: | ||
counts[num] = counts.get(num, 0) + 1 | ||
|
||
# sort numbers by their count (most frequent first) and grab top k | ||
# counts.get gets the count of num | ||
# reverse=True sorts in descending order | ||
# [:k] gets the first k elements | ||
top_nums = sorted(counts, key=counts.get, reverse=True) | ||
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. 파이썬에서는 딕셔너리에 대해 sorted를 쓸 수 있군요! 덕분에 자바의 TreeMap 자료구조에 대해서도 공부해볼 수 있었습니다 (비록 이 문제에는 적용하기 어렵지만 ㅎㅎ) |
||
return top_nums[:k] |
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,12 @@ | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
|
||
class Solution: | ||
def isPalindrome(self, s: str) -> bool: | ||
# clean up the string: remove non-alphanumeric chars and convert to lowercase | ||
# isalnum() checks if the character is alphanumeric | ||
filtered = ''.join(filter(str.isalnum, s)).lower() | ||
|
||
# check if it reads the same forwards and backwards | ||
# filtered[::-1] flips the string | ||
return filtered == filtered[::-1] |
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.
연속 수열의 시작부터 보는 방법이 있었네요
저는 괜히 어렵게 생각했네요 😅 좋은 아이디어 배우고 갑니다!