Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
19 changes: 19 additions & 0 deletions contains-duplicate/ppxyn1.py
Original file line number Diff line number Diff line change
@@ -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
'''

17 changes: 17 additions & 0 deletions longest-consecutive-sequence/ppxyn1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# idea: -
class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
sorted_nums = sorted(list(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.

You must write an algorithm that runs in O(n) time.

  • 제약사항이 있습니다.
  • sorting 사용하면 nlog(n) 이라 성공하더라도 맞지 않는 것 같습니다.

# 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

21 changes: 21 additions & 0 deletions top-k-frequent-elements/ppxyn1.py
Original file line number Diff line number Diff line change
@@ -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
'''

34 changes: 34 additions & 0 deletions two-sum/ppxyn1.py
Original file line number Diff line number Diff line change
@@ -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