Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 3 additions & 0 deletions contains-duplicate/WHYjun.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
return len(set(nums)) != len(nums)
21 changes: 21 additions & 0 deletions house-robber/WHYjun.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution:
def rob(self, nums: List[int]) -> int:
# memo for DP
dp = {}
return self.robs(nums, dp, len(nums) - 1)

def robs(self, nums: List[int], dp: Dict[int, int], houseToRob: int) -> int:
if houseToRob < 0:
return 0

if houseToRob - 2 not in dp:
dp[houseToRob - 2] = self.robs(nums, dp, houseToRob - 2)

if houseToRob - 1 not in dp:
dp[houseToRob - 1] = self.robs(nums, dp, houseToRob - 1)


return max(
dp[houseToRob - 2] + nums[houseToRob],
dp[houseToRob - 1]
)
Comment thread
WHYjun marked this conversation as resolved.
47 changes: 47 additions & 0 deletions longest-consecutive-sequence/WHYjun.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import heapq

class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
# remove duplicates by using set
numsSet = set(nums)

if len(numsSet) == 0 or len(numsSet) == 1:
return len(numsSet)

# Use priority queue to sort in O(n)
sorted = []
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

sorted는 파이썬 내장 정렬 함수라, 같은 이름의 변수를 정의하면 함수 참조가 가려집니다.
이후에 sorted 호출 시 혼동을 일으킬 수 있으니, pq 같은 변수명을 사용하는게 좋을 것 같습니다.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

그렇네요. 인터뷰용으로만 쓰다 보니 이런 부분을 놓치네요. 감사합니다.

for num in numsSet:
heapq.heappush(sorted, num)

prev = heapq.heappop(sorted)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

힙큐를 이용해 단순 정렬을 원하시면 heapify로 힙을 구성한 뒤, sorted()로 정렬하는 편이 더 효율적이고, 단순 sorted(numSet)을 호출하셔도 동일한 결과를 얻으실 수 있으십니다.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

감사합니다. 일단 heapify로 해두었습니다. 기록용으로 heapq 풀이법 먼저 생각해두었던 것을 남겨둔 거라서요.

answer, count = 1, 1

for i in range(len(numsSet) - 1):
popped = heapq.heappop(sorted)
if prev + 1 == popped:
count += 1
else:
count = 1

prev = popped
answer = max(answer, count)

return answer

def longestConsecutiveUsingSetOnly(self, nums: List[int]) -> int:
# remove duplicates by using set
numsSet = set(nums)

answer = 0

for num in numsSet:
# continue if it's not the longest consecutive
if num - 1 in numsSet:
continue
Comment thread
WHYjun marked this conversation as resolved.
else:
count = 1
while num + count in numsSet:
count += 1
answer = max(answer, count)

return answer
17 changes: 17 additions & 0 deletions top-k-frequent-elements/WHYjun.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
# Time complexity: O(nlog(n))
# Space Complexity: O(n)

if len(nums) <= k:
return list(set(nums))

counts = {}
for num in nums:
if num not in counts:
counts[num] = 0
counts[num] += 1

# The key of dictionary is unique.
sortedKeys = sorted(list(counts.keys()), key=lambda key: counts[key], reverse = True)
return sortedKeys[:k]
12 changes: 12 additions & 0 deletions two-sum/WHYjun.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
memo = {}

for i, num in enumerate(nums):
diff = target - num
if diff in memo:
# Each input would have exactly one solution
return [memo[diff], i]
memo[num] = i

return []