Skip to content

[thispath98] Week 1 #660

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 5 commits into from
Dec 10, 2024
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
15 changes: 15 additions & 0 deletions contains-duplicate/thispath98.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""
# Time Complexity: O(N)
- N번 순회
# Space Compelexity: O(N)
- 최악의 경우 (중복된 값이 없을 경우) N개 저장
"""
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
num_dict = {}
for num in nums:
if num not in num_dict:
num_dict[num] = True
Copy link
Member

Choose a reason for hiding this comment

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

사전에 저장하는 값의 종류가 한 가지인데, 이런 경우에는 세트를 쓰는 게 더 적합하지 않을까 생각이 들었습니다.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

코멘트 감사합니다!

else:
return True
return False
19 changes: 19 additions & 0 deletions house-robber/thispath98.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""
# Time Complexity: O(N)
- N개의 개수를 가지는 dp 리스트를 만들고, 이를 순회
# Space Compelexity: O(N)
- N개의 dp 리스트 저장
"""
class Solution:
def rob(self, nums: List[int]) -> int:
if len(nums) == 1:
return nums[0]

dp = [0 for _ in range(len(nums))]
dp[0] = nums[0]
dp[1] = max(nums[0], nums[1])

for i in range(len(nums) - 2):
dp[i + 2] = max(dp[i] + nums[i + 2], dp[i + 1])

return max(dp[-2], dp[-1])
37 changes: 37 additions & 0 deletions longest-common-subsequence/thispath98.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""
# Time Complexity: O(N)
- lce_dict 생성: N
- N개의 key에 대하여 순회하면서 값 확인: N
# Space Compelexity: O(k)
- 중복되지 않은 key k개 저장
"""
class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
if not nums:
return 0

lce_dict = {}
for num in nums:
lce_dict[num] = True

answer = 0
for num in nums:
cur_lce = 1
if lce_dict.pop(num, None) is None:
continue

down_num = num - 1
while down_num in lce_dict:
cur_lce += 1
lce_dict.pop(down_num)
down_num -= 1

up_num = num + 1
while up_num in lce_dict:
cur_lce += 1
lce_dict.pop(up_num)
up_num += 1

answer = answer if answer > cur_lce else cur_lce

return answer
16 changes: 16 additions & 0 deletions top-k-frequent-elements/thispath98.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
# Time Complexity: O(N log N)
- Counter 생성: N번 순회
- most common 연산: N log N
# Space Compelexity: O(N)
- 최악의 경우 (중복된 값이 없을 경우) N개 저장
"""
from collections import Counter


class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
count_dict = Counter(nums)
top_k_list = count_dict.most_common(k)
Copy link
Member

Choose a reason for hiding this comment

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

실제 코딩 인터뷰에서 다른 프로그래밍 언어에서 흔히 볼 수 없는 표준 라이브러리를 사용할 경우 해당 언어를 잘 모르는 면접관을 만날 경우 양날의 검으로 작용할 수 있으니 주의바라겠습니다.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

넵 감사합니다! 사실 sort를 이용해서 구현했었는데, Counter 메소드를 사용하는 게 성능이 더 좋아서 첨부했습니다. 다음부터는 여러 시행착오도 업로드해보겠습니다 ㅎㅎ

answer = [key for key, value in top_k_list]
return answer
13 changes: 13 additions & 0 deletions valid-palindrome/thispath98.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""
# Time Complexity: O(N)
- N개의 char를 각각 한번씩 순회
# Space Compelexity: O(N)
- 최악의 경우 (공백이 없을 경우) N개의 char 저장
"""
class Solution:
def isPalindrome(self, s: str) -> bool:
string = [char.lower() for char in s if char.isalnum()]
for i in range(len(string) // 2):
if string[i] != string[-i - 1]:
return False
return True
Loading