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
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
'''

21 changes: 21 additions & 0 deletions house-robber/ppxyn1.py
Original file line number Diff line number Diff line change
@@ -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]

51 changes: 51 additions & 0 deletions longest-consecutive-sequence/ppxyn1.py
Original file line number Diff line number Diff line change
@@ -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




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