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
18 changes: 18 additions & 0 deletions contains-duplicate/yolophg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Time Complexity: O(n)
# Space Complexity: O(n)

class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
# set to keep track of duplicates.
duplicates = set()

# go through each number in the list
for num in nums:
# if it's a duplicate, return true.
if num in duplicates:
return True
# otherwise, add it to the set to check for duplicates.
duplicates.add(num)

# if finish the loop and don't find duplicates, return false.
return False
25 changes: 25 additions & 0 deletions longest-consecutive-sequence/yolophg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Time Complexity: O(n)
# Space Complexity: O(n)

class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
# convert list to set to remove duplicates and allow quick lookups
num_set = set(nums)
longest_streak = 0

# loop through each number in the set
for num in num_set:
# only start counting if it's the beginning of a sequence
if num - 1 not in num_set:
current_num = num
current_streak = 1

# keep counting while the next number in the sequence exists
while current_num + 1 in num_set:
current_num += 1
current_streak += 1
Comment on lines +12 to +20
Copy link
Contributor

Choose a reason for hiding this comment

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

연속 수열의 시작부터 보는 방법이 있었네요
저는 괜히 어렵게 생각했네요 😅 좋은 아이디어 배우고 갑니다!


# update the longest streak found so far
longest_streak = max(longest_streak, current_streak)

return longest_streak
16 changes: 16 additions & 0 deletions top-k-frequent-elements/yolophg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Time Complexity: O(n log n)
# Space Complexity: O(n)

class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
# count how many times each number appears
counts = {}
for num in nums:
counts[num] = counts.get(num, 0) + 1

# sort numbers by their count (most frequent first) and grab top k
# counts.get gets the count of num
# reverse=True sorts in descending order
# [:k] gets the first k elements
top_nums = sorted(counts, key=counts.get, reverse=True)
Copy link
Contributor

Choose a reason for hiding this comment

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

파이썬에서는 딕셔너리에 대해 sorted를 쓸 수 있군요! 덕분에 자바의 TreeMap 자료구조에 대해서도 공부해볼 수 있었습니다 (비록 이 문제에는 적용하기 어렵지만 ㅎㅎ)

return top_nums[:k]
12 changes: 12 additions & 0 deletions valid-palindrome/yolophg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Time Complexity: O(n)
# Space Complexity: O(n)

class Solution:
def isPalindrome(self, s: str) -> bool:
# clean up the string: remove non-alphanumeric chars and convert to lowercase
# isalnum() checks if the character is alphanumeric
filtered = ''.join(filter(str.isalnum, s)).lower()

# check if it reads the same forwards and backwards
# filtered[::-1] flips the string
return filtered == filtered[::-1]
Loading