diff --git a/contains-duplicate/ppxyn1.py b/contains-duplicate/ppxyn1.py new file mode 100644 index 0000000000..7c128588e3 --- /dev/null +++ b/contains-duplicate/ppxyn1.py @@ -0,0 +1,19 @@ +# idea: Hash + +class Solution: + def containsDuplicate(self, nums: List[int]) -> bool: + count_dict={} + for i in range(len(nums)): + # print(count_dict) + if nums[i] in count_dict.keys(): + return True + else: + count_dict[nums[i]] = 1 + return False + + +''' +Trial and error +Printing I/O inside the loop may cause Output Limit Exceeded +''' + diff --git a/house-robber/ppxyn1.py b/house-robber/ppxyn1.py new file mode 100644 index 0000000000..34afce791d --- /dev/null +++ b/house-robber/ppxyn1.py @@ -0,0 +1,21 @@ +# idea: DP Top-down / Bottom-up +# DP was comp up on my head, but it is not easy for me to utilse recursive function. + + +class Solution: + def rob(self, nums: List[int]) -> int: + length = len(nums) + if length == 0: + return 0 + if length == 1: + return nums[0] + + lst = [0]*length + lst[0] = nums[0] + lst[1] = max(nums[0], nums[1]) + + for i in range(2, length): + lst[i] = max(lst[i-1], lst[i-2] + nums[i]) + + return lst[-1] + diff --git a/longest-consecutive-sequence/ppxyn1.py b/longest-consecutive-sequence/ppxyn1.py new file mode 100644 index 0000000000..6358515f3a --- /dev/null +++ b/longest-consecutive-sequence/ppxyn1.py @@ -0,0 +1,51 @@ +# idea: Hash +class Solution: + def longestConsecutive(self, nums: List[int]) -> int: + if not nums: + return 0 + + num_set = set(nums) + max_len = 1 + + for num in num_set: + if num - 1 not in num_set: + current = num + tmp = 1 + while current + 1 in num_set: + current += 1 + tmp += 1 + if tmp > max_len: + max_len = tmp + return max_len + + + + + +''' +Trial and error + +The code below was passed on Leecode since their Constraints was 0 <= nums.length <= 10^5. +But I realised that I missed another constraint they mentioned, which is "You must write an algorithm that runs in O(n) time." +''' +# class Solution: +# def longestConsecutive(self, nums: List[int]) -> int: +# if len(nums)==0: +# return 0 +# sorted_nums = sorted(list(set(nums))) +# # sorted_nums = sorted(nums) # duplicate +# max_len = 1 +# tmp = 1 + +# for i in range(1, len(sorted_nums)): +# if sorted_nums[i] == sorted_nums[i - 1] + 1: +# tmp += 1 +# else: +# max_len = max(max_len, tmp) +# tmp = 1 +# ans = max(max_len, tmp) +# return ans + + + + diff --git a/top-k-frequent-elements/ppxyn1.py b/top-k-frequent-elements/ppxyn1.py new file mode 100644 index 0000000000..e6402c0bdb --- /dev/null +++ b/top-k-frequent-elements/ppxyn1.py @@ -0,0 +1,21 @@ +# idea: dictonary + +class Solution: + def topKFrequent(self, nums: List[int], k: int) -> List[int]: + count_dict = {} + ans = [] + for idx, val in enumerate(nums): + if val not in count_dict: + count_dict[val] = 1 + else: + count_dict[val] +=1 + sorted_items = sorted(count_dict.items(), key=lambda x: x[1], reverse=True) #sorted return list / dict.items() is tuple + # print(sorted_items) + for i in range(k): + ans.append(sorted_items[i][0]) + return ans + +''' +Similar way : Using Counter() function +''' + diff --git a/two-sum/ppxyn1.py b/two-sum/ppxyn1.py new file mode 100644 index 0000000000..8ba1cf394d --- /dev/null +++ b/two-sum/ppxyn1.py @@ -0,0 +1,34 @@ +# idea: For each number n in nums, check if (target - n) exists in the remaining elements. + +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + for idx, num in enumerate(nums): + required_num = target - num + if required_num in nums[idx+1:]: + return [idx, nums.index(required_num, idx+1)] + + +''' +Trial and error +idea : two pointer +I struggled to handle the indices of the original array after sorting it. +The code below fails when negative numbers are involved. +I realized it would be tricky to solve this problem with the two-pointer. +''' + +# class Solution: +# def twoSum(self, nums: List[int], target: int) -> List[int]: +# sorted_nums = sorted(nums) +# left, right = 0, len(nums) - 1 + +# while left < right: +# s = sorted_nums[left] + sorted_nums[right] +# if s == target: +# left_idx = nums.index(sorted_nums[left]) +# right_idx = nums.index(sorted_nums[right], left_idx + 1) +# return [left_idx, right_idx] +# elif s < target: +# left += 1 +# else: +# right -= 1 +