-
-
Notifications
You must be signed in to change notification settings - Fork 248
[bskkimm] WEEK 01 solutions #1705
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
8 commits
Select commit
Hold shift + click to select a range
702eb2d
contains-duplicate
bskkimm 02928e1
Renewed bskkimm.py with latest version
bskkimm 956a651
Update bskkimm.py
bskkimm 4d20a86
Update bskkimm.py
bskkimm 01f7bb1
"create the bskkimm.py
bskkimm bb75a08
top-k-frequent-elements
bskkimm 6a3afb6
longest-consecutive-sequence, top-k-frequent-elements
bskkimm 07331bd
longest-consecutive-sequence
bskkimm 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,24 @@ | ||
from collections import defaultdict | ||
class Solution(object): | ||
def containsDuplicate(self, nums): | ||
""" | ||
:type nums: List[int] | ||
:rtype: bool | ||
""" | ||
# [1,2,3,1] | ||
|
||
# [1,1,1,3,3,4,3,2,4,2] | ||
|
||
# add element to a dict | ||
# if a same value appears, then return True | ||
# if a for loop ends without disruption, return False | ||
|
||
dict = defaultdict(bool) | ||
|
||
for num in nums: | ||
if dict[num]: | ||
return True | ||
else: | ||
dict[num] = True | ||
|
||
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,21 @@ | ||
class Solution: | ||
def rob(self, nums: List[int]) -> int: | ||
|
||
# [2,7,9,10,5,4] | ||
# No Consecutive robbing --> able to skip as many times as wanted | ||
|
||
# which one to add? --> dp | ||
|
||
# dp[i], dp[i-1] + nums[i+1] | ||
if len(nums) == 1: | ||
return nums[0] | ||
|
||
|
||
dp = [0]*len(nums) | ||
dp[0] = nums[0] | ||
dp[1] = max(dp[0], nums[1]) | ||
|
||
for i in range(2, len(nums)): | ||
dp[i] = max(dp[i-1], dp[i-2] + nums[i]) | ||
|
||
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,27 @@ | ||
from collections import defaultdict | ||
class Solution: | ||
def longestConsecutive(self, nums: List[int]) -> int: | ||
|
||
if not nums: | ||
return 0 | ||
|
||
dict_consecutive = defaultdict(int) | ||
group_num = 0 # consecutive group number | ||
|
||
dict_consecutive[group_num] += 1 # w.r.t the first num of nums | ||
|
||
# sort in the ascending order eliminating duplicates | ||
nums = sorted(set(nums)) | ||
|
||
# 2. build dict_consecutive | ||
for i in range(1, len(nums)): | ||
if nums[i] - nums[i-1] == 1: | ||
dict_consecutive[group_num] += 1 | ||
else: | ||
group_num += 1 | ||
dict_consecutive[group_num] += 1 | ||
|
||
# 3. Get the longest group | ||
longest_consecutive = max(list(dict_consecutive.values())) | ||
|
||
return longest_consecutive |
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 @@ | ||
from collections import defaultdict | ||
class Solution: | ||
def topKFrequent(self, nums: List[int], k: int) -> List[int]: | ||
# dict_num = {num: ocurrence_num,,,,,} | ||
|
||
# 1. Build the dict | ||
dict_num = defaultdict(int) | ||
for num in nums: | ||
dict_num[num] += 1 | ||
|
||
# 2. Resequence in the descending order w.r.t the num of ocurrence using lambda function | ||
dict_num_desc = dict(sorted(dict_num.items(), key=lambda x: x[1], reverse=True)) | ||
|
||
# 3. Extract top k frequent nums,, | ||
top_k = [num for i, (num, ocurrence) in enumerate(dict_num_desc.items()) if i < k] | ||
|
||
return top_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,22 @@ | ||
class Solution(object): | ||
def isPalindrome(self, s): | ||
""" | ||
:type s: str | ||
:rtype: bool | ||
""" | ||
only_al_nu = [] | ||
|
||
# "2 man, a plan, a canal: Panam2" | ||
# 1. Delete non-alphanumeric elements and space | ||
for cha in s: | ||
if cha.isalpha() or cha.isnumeric(): | ||
if cha.isalpha(): | ||
cha = cha.lower() | ||
only_al_nu.append(cha) | ||
|
||
# 2. extract the last and first elements and check if they are same | ||
while len(only_al_nu) > 1: | ||
if only_al_nu.pop() != only_al_nu.pop(0): | ||
return False | ||
|
||
return True |
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.
문제 풀이 파일들은 commit 시 마지막 줄을 갱신하셔 빈줄을 만들어 주시면 감사하겠습니다!
빈줄이 없으면 CI 진행이 되지 않아요 ㅜ.ㅜ
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.
늦어서 죄송합니다 수정하겠습니다!!
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.