Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
35 changes: 35 additions & 0 deletions longest-consecutive-sequence/mrlee7.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from typing import List


"""
Ideation:
배열을 정렬한 뒤, 인접한 숫자들을 비교하면서 가장 긴 연속 수열의 길이를 구한다.

- 같은 숫자는 중복이므로 건너뛴다.
- 현재 숫자 + 1 이 다음 숫자와 같으면 연속된 수이므로 길이를 증가시킨다.
- 연속이 끊기면 최대 길이를 갱신하고 길이를 1로 초기화한다.

Time Complexity: O(n log n)
Space Complexity: O(1)
"""
class Solution:

def longestConsecutive(self, nums: List[int]) -> int:
if len(nums) == 0:
return 0

longest = 0
length = 1

nums.sort()
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.

저는 set 을 사용해서 공간복잡도를 희생했는데, 정렬도 좋네요. 배워갑니다 👍


for idx in range(len(nums) - 1):
if nums[idx] == nums[idx + 1]:
continue
if nums[idx] + 1 == nums[idx + 1]:
length += 1
else:
longest = max(longest, length)
length = 1
longest = max(longest, length)
return longest
19 changes: 19 additions & 0 deletions top-k-frequent-elements/mrlee7.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from typing import List

"""
Ideation
해시맵에 각 원소별 빈도수를 저장한 뒤, 빈도수 높은 순으로 정렬한 다음 앞에서 k개를 반환합니다.
Time Complexity: O(n)
Space Complexity: O(n + m log m)
"""

class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
frequency = {}
for num in nums:
frequency[num] = frequency.get(num, 0) + 1
sorted_nums = sorted(frequency, key=lambda num: frequency[num], reverse=True)

return sorted_nums[:k]


11 changes: 11 additions & 0 deletions valid-anagram/mrlee7.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution:
"""
Ideation:
애너그램은 문자열 내 배치만 다를 뿐, 동일한 문자들이 동일한 카운트로 보장되어야 한다.
이를 위해 문자 기준으로 정렬하고 같은지 확인합니다.
Time Complexity: O(n log n)
Space Complexity: O(1)
"""

def isAnagram(self, s: str, t: str) -> bool:
return sorted(s) == sorted(t)
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.

간결하네요 👍

Loading