Skip to content

[gayuna] Week 1 #671

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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/gayuna.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution(object):
# Time complexity: O(n)

# Iterate through all numbers.
# if there is same number in set, early return with True.
# if not exist, add the number to the set.
def containsDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
my_set = set()
for i in nums:
if i in my_set:
return True
my_set.add(i)

return False
27 changes: 27 additions & 0 deletions house-robber/gayuna.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution(object):
"""
the robber will have 2 choices for each house
1. include this house for target and skip next one
2. bypass this house.
from house 1 to n, repeat this choice and use memoization for maximum money until the end to prevent duplicate calculation.

time complexity: O(n) (forward iterating only)
"""
def rob(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
my_dict = {}
def process(idx): # -> maximum
if idx >= len(nums):
return 0

include_sum = nums[idx] + (my_dict[idx+2] if idx+2 in my_dict else process(idx+2))
exclude_sum = my_dict[idx+1] if idx+1 in my_dict else process(idx+1)
my_dict[idx] = max(include_sum, exclude_sum)
return my_dict[idx]

return process(0)


34 changes: 34 additions & 0 deletions longest-consecutive-sequence/gayuna.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Solution(object):
# Time complexity: O(n) to generate set and O(n) to pop all items from set -> O(n) + O(n) = O(n)

# create set from nums
# pop one number and calculate length with neighbors
# update longest_consivutive if the calculated length is bigger than existing number.
def longestConsecutive(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
start_dict = {}

my_set = set(nums)
longest_consicutive = 0

while len(my_set):
num = my_set.pop()
length = 1
start = num-1
while start in my_set:
my_set.remove(start)
length += 1
start -= 1
end = num+1
while end in my_set:
my_set.remove(end)
length += 1
end += 1
if length > longest_consicutive:
longest_consicutive = length

return longest_consicutive

31 changes: 31 additions & 0 deletions top-k-frequent-elements/gayuna.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import heapq
class Solution(object):
# Time complexity: O(nlogn)

# Iterate through all numbers, generate frequency dictionary.
# create max heap with frequency.
# pop k items from the heap
def topKFrequent(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: List[int]
"""

freq_dict = {}
for i in nums:
if i in freq_dict:
freq_dict[i] += 1
else:
freq_dict[i] = 1

my_heap = []
for key, value in freq_dict.items():
heapq.heappush(my_heap, (-value, key))

answer = []
for i in range(k):
freq, number = heapq.heappop(my_heap)
answer.append(number)

return answer
20 changes: 20 additions & 0 deletions valid-palindrome/gayuna.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution(object):
# Time complexity: O(n)

# Iterate through all characters, check for alphabet and number. trimmed string should only contain lower case letters and numbers.
# use two pointer. one from head and one from tail. iterate all char to the inside.
# early return if two characters are different. if loop ends, it's palindrome.
def isPalindrome(self, s):
"""
:type s: str
:rtype: bool
"""
trimmed = ""
for c in s:
if c.isalpha() or c.isnumeric():
trimmed += c.lower()

for i in range(len(trimmed)//2):
if trimmed[i] != trimmed[-(1+i)]:
return False
return True
Loading