Skip to content
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
11 changes: 7 additions & 4 deletions contains-duplicate/ppxyn1.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# idea: Hash

# Ans 1
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
count_dict={}
Expand All @@ -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))
Copy link
Contributor

Choose a reason for hiding this comment

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

저랑 동일하게 푸셨네요!
저도 좀 더 찾아보았는데요 저희가 푼 방식은 조기 종료가 되지 않아서 아래 처럼 조기 종료하면 좀더빠르게 순회를 종료할꺼같아요!

조기 종료가 안 됨 — Set 방식은 항상 전체 배열을 순회합니다. 중복이 앞쪽에 있어도 끝까지 돌아야 하죠.

var containsDuplicate = function (nums) {
  const seen = new Set();
  for (const n of nums) {
    if (seen.has(n)) return true;
    seen.add(n);
  }
  return false;
};

Copy link
Contributor Author

Choose a reason for hiding this comment

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

전혀 생각하지 못했던 부분이네요, 감사합니다.



4 changes: 3 additions & 1 deletion longest-consecutive-sequence/ppxyn1.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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:
Expand Down
19 changes: 16 additions & 3 deletions top-k-frequent-elements/ppxyn1.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# idea: dictonary

# Ans 1
class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
count_dict = {}
Expand All @@ -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


44 changes: 23 additions & 21 deletions two-sum/ppxyn1.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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 []