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
34 changes: 34 additions & 0 deletions 3sum/mrlee7.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from typing import List


class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
result: List[List[int]] = []
nums.sort()

for i in range(len(nums) - 2):
if i > 0 and nums[i] == nums[i - 1]:
continue

left = i + 1
right = len(nums) - 1

while left < right:
total = nums[i] + nums[left] + nums[right]

if total == 0:
result.append([nums[i], nums[left], nums[right]])
left += 1
right -= 1

while left < right and nums[left] == nums[left - 1]:
left += 1
while left < right and nums[right] == nums[right + 1]:
right -= 1

elif total < 0:
left += 1
else:
right -= 1

return result
8 changes: 8 additions & 0 deletions climbing-stairs/mrlee7.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Solution:
def climbStairs(self, n: int) -> int:
stairs = {1: 1, 2: 2}

for i in range(3, n + 1):
stairs[i] = stairs[i - 1] + stairs[i - 2]

return stairs[n]
36 changes: 36 additions & 0 deletions longest-consecutive-sequence/mrlee7.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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
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
18 changes: 18 additions & 0 deletions product-of-array-except-self/mrlee7.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from typing import List


class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
result = [0] * len(nums)

prefix_product = 1
for index in range(len(nums)):
result[index] = prefix_product
prefix_product *= nums[index]

suffix_product = 1
for index in range(len(nums) - 1, -1, -1):
result[index] *= suffix_product
suffix_product *= nums[index]

return result
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
Contributor

Choose a reason for hiding this comment

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

간결하네요 👍

Loading