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
4 changes: 4 additions & 0 deletions contains-duplicate/jeldo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Solution:
# O(n)
def containsDuplicate(self, nums: list[int]) -> bool:
return len(nums) != len(set(nums)) # O(n)
9 changes: 9 additions & 0 deletions house-robber/jeldo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Solution:
# O(n)
def rob(self, nums: list[int]) -> int:
if len(nums) <= 2:
return max(nums)
nums[2] += nums[0]
for i in range(3, len(nums)):
nums[i] += max(nums[i-3], nums[i-2])
return max(nums[-1], nums[-2])
13 changes: 13 additions & 0 deletions longest-consecutive-sequence/jeldo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution:
# O(n)
def longestConsecutive(self, nums: list[int]) -> int:
max_length = 0
nums_set = set(nums)
for n in nums_set:
if n - 1 not in nums_set:
length = 0
while n + length in nums_set:
Comment on lines +7 to +9
Copy link
Contributor

Choose a reason for hiding this comment

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

레퍼런스 카운팅에서는 효율적이겠지만, 인터뷰라면 이 임시 객체들의 의미를 물어볼 것 같습니다.

length += 1
max_length = max(max_length, length)

return max_length
9 changes: 9 additions & 0 deletions top-k-frequent-elements/jeldo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from collections import Counter
import heapq


class Solution:
# O(nlogn)
def topKFrequent(self, nums: list[int], k: int) -> list[int]:
ls = [(key, value) for key, value in Counter(nums).items()] # O(n)
return [key for _, key in heapq.nlargest(n=k, iterable=ls)] # O(nlogn)
Copy link
Contributor

Choose a reason for hiding this comment

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

이 부분의 시간복잡도는 O(n log k)가 맞지 않을까 싶습니다.

Copy link
Member

Choose a reason for hiding this comment

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

저도 이 부분에서 O(n log k) 시간이 걸릴 것 같네요?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

아 o(nlogk)가 맞습니다. big O내 size를 더 명확하게 나타내야겠네요 😅

5 changes: 5 additions & 0 deletions valid-palindrome/jeldo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Solution:
# O(n)
def isPalindrome(self, s: str) -> bool:
s = ''.join(ch.lower() for ch in s if ch.isalnum()) # O(n)
return s == s[::-1] # O(n)
Loading