diff --git a/contains-duplicate/bskkimm.py b/contains-duplicate/bskkimm.py new file mode 100644 index 000000000..7f83c3de3 --- /dev/null +++ b/contains-duplicate/bskkimm.py @@ -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 diff --git a/house-robber/bskkimm.py b/house-robber/bskkimm.py new file mode 100644 index 000000000..140839433 --- /dev/null +++ b/house-robber/bskkimm.py @@ -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] diff --git a/longest-consecutive-sequence/bskkimm.py b/longest-consecutive-sequence/bskkimm.py new file mode 100644 index 000000000..367ba22b9 --- /dev/null +++ b/longest-consecutive-sequence/bskkimm.py @@ -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 diff --git a/top-k-frequent-elements/bskkimm.py b/top-k-frequent-elements/bskkimm.py new file mode 100644 index 000000000..0532eee66 --- /dev/null +++ b/top-k-frequent-elements/bskkimm.py @@ -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 diff --git a/valid-palindrome/bskkimm.py b/valid-palindrome/bskkimm.py new file mode 100644 index 000000000..74794c62f --- /dev/null +++ b/valid-palindrome/bskkimm.py @@ -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