diff --git a/contains-duplicate/ppxyn1.py b/contains-duplicate/ppxyn1.py index 7c128588e3..3af34fcf90 100644 --- a/contains-duplicate/ppxyn1.py +++ b/contains-duplicate/ppxyn1.py @@ -1,5 +1,6 @@ # idea: Hash +# Ans 1 class Solution: def containsDuplicate(self, nums: List[int]) -> bool: count_dict={} @@ -12,8 +13,10 @@ def containsDuplicate(self, nums: List[int]) -> bool: return False -''' -Trial and error -Printing I/O inside the loop may cause Output Limit Exceeded -''' +# Ans 2 +# Time Complexity: O(n) (Set) +class Solution: + def containsDuplicate(self, nums: List[int]) -> bool: + return len(nums) != len(set(nums)) + diff --git a/longest-consecutive-sequence/ppxyn1.py b/longest-consecutive-sequence/ppxyn1.py index 6358515f3a..1b233c5dba 100644 --- a/longest-consecutive-sequence/ppxyn1.py +++ b/longest-consecutive-sequence/ppxyn1.py @@ -1,4 +1,6 @@ # idea: Hash +# start after checking left neightbour +# Time complexity = O(n) class Solution: def longestConsecutive(self, nums: List[int]) -> int: if not nums: @@ -8,7 +10,7 @@ def longestConsecutive(self, nums: List[int]) -> int: max_len = 1 for num in num_set: - if num - 1 not in num_set: + if num - 1 not in num_set: current = num tmp = 1 while current + 1 in num_set: diff --git a/top-k-frequent-elements/ppxyn1.py b/top-k-frequent-elements/ppxyn1.py index e6402c0bdb..cda401b8ea 100644 --- a/top-k-frequent-elements/ppxyn1.py +++ b/top-k-frequent-elements/ppxyn1.py @@ -1,5 +1,6 @@ # idea: dictonary +# Ans 1 class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: count_dict = {} @@ -15,7 +16,19 @@ def topKFrequent(self, nums: List[int], k: int) -> List[int]: ans.append(sorted_items[i][0]) return ans -''' -Similar way : Using Counter() function -''' + + +# With Couter +# Time Complexity: O(nlog(n)) + +from collections import Counter +class Solution: + def topKFrequent(self, nums: List[int], k: int) -> List[int]: + count_dict = Counter(nums) + count_dict = sorted(count_dict.items(), key=lambda x: x[1], reverse=True) + ans = [] + for i in range(k): + ans.append(count_dict[i][0]) + return ans + diff --git a/two-sum/ppxyn1.py b/two-sum/ppxyn1.py index 8ba1cf394d..f0deb83201 100644 --- a/two-sum/ppxyn1.py +++ b/two-sum/ppxyn1.py @@ -1,5 +1,6 @@ # idea: For each number n in nums, check if (target - n) exists in the remaining elements. +# Ans 1 class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: for idx, num in enumerate(nums): @@ -8,27 +9,28 @@ def twoSum(self, nums: List[int], target: int) -> List[int]: 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 +# Ans 2 +# idea : Two-pointer +# Time Complexity : O(n log n) + +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + # Keep original indices ! + new_nums = [(idx, num) for idx, num in enumerate(nums)] + new_nums.sort(key=lambda x: x[1]) + + left, right = 0, len(new_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 + while left < right: + idx_left, idx_right = new_nums[left][0], new_nums[right][0] + val = new_nums[left][1] + new_nums[right][1] + if val == target: + return [idx_left, idx_right] + elif val < target: + left +=1 + else: + right -=1 + return [] +