Skip to content

Commit 6a0a279

Browse files
authored
Merge pull request #1705 from bskkimm/main
[bskkimm] WEEK 01 solutions
2 parents b0893aa + 07331bd commit 6a0a279

File tree

5 files changed

+111
-0
lines changed

5 files changed

+111
-0
lines changed

contains-duplicate/bskkimm.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from collections import defaultdict
2+
class Solution(object):
3+
def containsDuplicate(self, nums):
4+
"""
5+
:type nums: List[int]
6+
:rtype: bool
7+
"""
8+
# [1,2,3,1]
9+
10+
# [1,1,1,3,3,4,3,2,4,2]
11+
12+
# add element to a dict
13+
# if a same value appears, then return True
14+
# if a for loop ends without disruption, return False
15+
16+
dict = defaultdict(bool)
17+
18+
for num in nums:
19+
if dict[num]:
20+
return True
21+
else:
22+
dict[num] = True
23+
24+
return False

house-robber/bskkimm.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution:
2+
def rob(self, nums: List[int]) -> int:
3+
4+
# [2,7,9,10,5,4]
5+
# No Consecutive robbing --> able to skip as many times as wanted
6+
7+
# which one to add? --> dp
8+
9+
# dp[i], dp[i-1] + nums[i+1]
10+
if len(nums) == 1:
11+
return nums[0]
12+
13+
14+
dp = [0]*len(nums)
15+
dp[0] = nums[0]
16+
dp[1] = max(dp[0], nums[1])
17+
18+
for i in range(2, len(nums)):
19+
dp[i] = max(dp[i-1], dp[i-2] + nums[i])
20+
21+
return dp[-1]
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from collections import defaultdict
2+
class Solution:
3+
def longestConsecutive(self, nums: List[int]) -> int:
4+
5+
if not nums:
6+
return 0
7+
8+
dict_consecutive = defaultdict(int)
9+
group_num = 0 # consecutive group number
10+
11+
dict_consecutive[group_num] += 1 # w.r.t the first num of nums
12+
13+
# sort in the ascending order eliminating duplicates
14+
nums = sorted(set(nums))
15+
16+
# 2. build dict_consecutive
17+
for i in range(1, len(nums)):
18+
if nums[i] - nums[i-1] == 1:
19+
dict_consecutive[group_num] += 1
20+
else:
21+
group_num += 1
22+
dict_consecutive[group_num] += 1
23+
24+
# 3. Get the longest group
25+
longest_consecutive = max(list(dict_consecutive.values()))
26+
27+
return longest_consecutive

top-k-frequent-elements/bskkimm.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from collections import defaultdict
2+
class Solution:
3+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
4+
# dict_num = {num: ocurrence_num,,,,,}
5+
6+
# 1. Build the dict
7+
dict_num = defaultdict(int)
8+
for num in nums:
9+
dict_num[num] += 1
10+
11+
# 2. Resequence in the descending order w.r.t the num of ocurrence using lambda function
12+
dict_num_desc = dict(sorted(dict_num.items(), key=lambda x: x[1], reverse=True))
13+
14+
# 3. Extract top k frequent nums,,
15+
top_k = [num for i, (num, ocurrence) in enumerate(dict_num_desc.items()) if i < k]
16+
17+
return top_k

valid-palindrome/bskkimm.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution(object):
2+
def isPalindrome(self, s):
3+
"""
4+
:type s: str
5+
:rtype: bool
6+
"""
7+
only_al_nu = []
8+
9+
# "2 man, a plan, a canal: Panam2"
10+
# 1. Delete non-alphanumeric elements and space
11+
for cha in s:
12+
if cha.isalpha() or cha.isnumeric():
13+
if cha.isalpha():
14+
cha = cha.lower()
15+
only_al_nu.append(cha)
16+
17+
# 2. extract the last and first elements and check if they are same
18+
while len(only_al_nu) > 1:
19+
if only_al_nu.pop() != only_al_nu.pop(0):
20+
return False
21+
22+
return True

0 commit comments

Comments
 (0)