-
-
Notifications
You must be signed in to change notification settings - Fork 245
[jeldo3] Week1 #646
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
[jeldo3] Week1 #646
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) |
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]) |
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: | ||
length += 1 | ||
max_length = max(max_length, length) | ||
|
||
return max_length |
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 부분의 시간복잡도는 O(n log k)가 맞지 않을까 싶습니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저도 이 부분에서 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아 o(nlogk)가 맞습니다. big O내 size를 더 명확하게 나타내야겠네요 😅 |
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
레퍼런스 카운팅에서는 효율적이겠지만, 인터뷰라면 이 임시 객체들의 의미를 물어볼 것 같습니다.