Skip to content

[river20s] WEEK 01 solutions #1174

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

Merged
merged 9 commits into from
Apr 5, 2025
39 changes: 39 additions & 0 deletions contains-duplicate/river20s.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class Solution(object):

def containsDuplicate(self, nums):

"""

:type nums: List[int]

:rtype: bool

nums 리스트에 중복 값이 나오면 false,

리스트에 중복 값이 없으면 true를 반환한다.

- Time Complexity: O(n)

* 전체 배열을 한 번씩 순회하면서 중복 여부를 확인한다.

- Space Complexity: O(n)

* Python에서 set()은 배열의 각 원소를 해시 테이블에 저장한다.

배열에 중복된 요소가 전혀 없다면 모든 요소(n개)를 set에 저장하게 되므로

공간 복잡도는 O(n)이 된다.
Comment on lines +21 to +25
Copy link
Contributor

Choose a reason for hiding this comment

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

안녕하세요. 사실 딱히 리뷰해드릴 것도 없고, set을 쓰는 발상은 배워갑니다.
그래도 먼가 도움을 드려야할것 같아서 쥐어짜서 남깁니다.

배열에 중복된 요소가 전혀 없다면 모든 요소(n개)를 set에 저장하게 되므로
보다는

저장되는 요소의 개수가 n에 비례하여 선형적으로 증가하므로 공간 복잡도는 O(n)이 된다.

이게 좀 더 정확한 표현이지 않을까 싶네요.


"""

seen = set() # 중복 여부 확인하는 set 객체

for item in nums:

if item in seen:

return True

seen.add(item)

return False
48 changes: 48 additions & 0 deletions house-robber/river20s.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
class Solution(object):
def rob(self, nums):
"""
# 초기 풀이
rob_house_even = 0 # 짝수 집들을 터는 경우 금액의 합
rob_house_odd = 0 # 홀수 집들을 터는 경우 금액의 합
for index, value in enumerate(nums):
if index % 2 == 0:
rob_house_even += value
else:
rob_house_odd += value

return max(rob_house_even, rob_house_odd)
# 실패:
# 단순히 짝수와 홀수를 구분하는 것만으로
# 최적 해를 구할 수 없음

===========================================

동적 프로그래밍을 활용해서 문제를 해결
각각의 최적 해를 누적하여 마지막까지의 최적 해를 구해야 함

"""
if not nums: # 빈 리스트가 주어질 경우, 0을 반환
return 0
if len(nums) == 1: # 요소가 하나인 경우, 그 값을 반환
return nums[0]

# prev2: i-2번째까지 고려 했을 때의 최대 금액
# prev1: i-1번째까지 고려 했을 때의 최대 금액
# 초기값 설정:
# - 첫 번째 집(인덱스 0)만 고려한 경우 nums[0]
# - 두 번째 집(인덱스 1)까지 고려한 경우 첫 번째 집과 두 번째 집 중 큰 값
prev2, prev1 = nums[0], max(nums[0], nums[1])

for i in range(2, len(nums)):
# 두 선택지 중 더 금액이 큰 경우를 계산
# 1. i번째 집을 털지 않을 때: 이전까지의 최대 금액 prev1
# 2. i번째 집을 털 때: i-1번째 집은 털 수 없음
# i-2번째 집까지의 최대 금액 prev2에 현재 집의 금액 nums[i]를 더함
current = max(prev1, prev2 + nums[i])

# 이전 단계의 prev1은 다음 단계에서 prev2가,
# current 값은 새로운 prev1이 됨
prev2, prev1 = prev1, current

# 마지막 집까지의 최대 금액인 prev1 반환
return prev1
31 changes: 31 additions & 0 deletions longest-consecutive-sequence/river20s.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Solution(object):
def longestConsecutive(self, nums):
"""
:type nums: List[int]
:rtype: int
>> nums에서 연속된 수열 중 가장 긴 수열의 길이를 반환합니다.
(전체 풀이 과정은 다음 링크를 참고하시면 됩니다: https://blog.naver.com/kanosekai/223823200575)
- Time Complexity: O(n)
nums를 set으로 변환하는 데 O(n) 시간이 소요됩니다.
- Space Complexity: O(n)
모든 원소가 고유한 경우 전부 set에 저장하므로
O(n)의 공간을 추가로 사용할 수 있습니다.
"""
num_set = set(nums)
longest = 0

for num in num_set:
# num이 연속 구간의 시작점인지 판단
if num - 1 not in num_set:
current = num
current_streak = 1

# 현재 숫자에서 연속된 값들이 set에 있는지 확인하며 수열 확장
while current + 1 in num_set:
current += 1
current_streak += 1

# 가장 긴 수열의 길이를 반환
longest = max(longest, current_streak)

return longest
20 changes: 20 additions & 0 deletions top-k-frequent-elements/river20s.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution(object):
def topKFrequent(self, nums, k):
"""
주어진 정수 리스트에서 각 숫자의 등장 빈도를 계산한 후,
빈도수가 높은 순서대로 상위 k개의 숫자를 반환합니다.

:param nums: 정수로 이루어진 리스트
:param k: 반환할 상위 빈도 숫자의 개수
:return: 빈도수가 높은 순서대로 정렬된 상위 k개 숫자의 리스트
"""
freq = {} # 각 숫자 등장 횟수를 저장하는 딕셔너리
# 리스트의 각 숫자 등장 횟수를 누적 저장
for num in nums:
# num이 freq에 있다면 기존 값에 1을 더하고, 없으면 0에서 1을 더함
freq[num] = freq.get(num, 0) + 1

# freq의 키들을 등장 횟수 기준 내림차순 정렬
sorted_list = sorted(freq, key=freq.get, reverse=True)
# 정렬된 리스트에서 top-k개 숫자를 반환
return sorted_list[:k]
16 changes: 16 additions & 0 deletions two-sum/river20s.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
seen = {}
for i, num in enumerate(nums):
comp = target - num
if comp in seen:
return seen[comp], i
if num not in seen:
seen[num] = i
return None