Skip to content

[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 8 commits into from
Jul 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions contains-duplicate/bskkimm.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

문제 풀이 파일들은 commit 시 마지막 줄을 갱신하셔 빈줄을 만들어 주시면 감사하겠습니다!
빈줄이 없으면 CI 진행이 되지 않아요 ㅜ.ㅜ

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

문제 풀이 파일들은 commit 시 마지막 줄을 갱신하셔 빈줄을 만들어 주시면 감사하겠습니다! 빈줄이 없으면 CI 진행이 되지 않아요 ㅜ.ㅜ

늦어서 죄송합니다 수정하겠습니다!!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

문제 풀이 파일들은 commit 시 마지막 줄을 갱신하셔 빈줄을 만들어 주시면 감사하겠습니다! 빈줄이 없으면 CI 진행이 되지 않아요 ㅜ.ㅜ

image 이렇게 끝내라는 말씀이신거죠??

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
21 changes: 21 additions & 0 deletions house-robber/bskkimm.py
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]
27 changes: 27 additions & 0 deletions longest-consecutive-sequence/bskkimm.py
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
17 changes: 17 additions & 0 deletions top-k-frequent-elements/bskkimm.py
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
22 changes: 22 additions & 0 deletions valid-palindrome/bskkimm.py
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