Skip to content
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

[HodaeSsi] Week 02 #762

Merged
merged 6 commits into from
Dec 21, 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
21 changes: 21 additions & 0 deletions 3sum/HodaeSsi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
answerSet = set()
nums.sort()
Comment on lines +3 to +4
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 i in range(len(nums) - 2):
leftIdx = i + 1
rightIdx = len(nums) - 1
while leftIdx < rightIdx:
sum = nums[i] + nums[leftIdx] + nums[rightIdx]
if sum < 0:
leftIdx += 1
elif sum > 0:
rightIdx -= 1
else:
answerSet.add((nums[i], nums[leftIdx], nums[rightIdx]))
leftIdx = leftIdx + 1
rightIdx = rightIdx - 1

return list(answerSet)
Copy link
Contributor

Choose a reason for hiding this comment

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

그러면 여기서 set을 list로 돌리지 않아도 될 것 같습니다

Copy link
Contributor Author

Choose a reason for hiding this comment

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

오 안그래도 시간 제한에 아슬아슬한 풀이라 고민 중이었는데,
반영해서 다시 풀어보겠습니다.
리뷰 감사합니다!


12 changes: 12 additions & 0 deletions climbing-stairs/HodaeSsi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution:
def climbStairs(self, n: int) -> int:
dp = []
dp.append(0)
dp.append(1)
dp.append(2)
Comment on lines +3 to +6
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
dp = []
dp.append(0)
dp.append(1)
dp.append(2)
dp = [0, 1, 2]

이게 더 깔끔할 것 같아서 제안드립니다


for i in range(3, n + 1):
dp.append(dp[i - 1] + dp[i - 2])

return dp[n]
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return dp[n]
return dp[-1]

파이썬에서는 이렇게 하면 배열 마지막 원소라는 점이 명확해서 가독성에 도움이 됩니다


Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution:
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
if inorder == []:
return None

mid = preorder.pop(0)
midIdx = inorder.index(mid)
left = self.buildTree(preorder, inorder[:midIdx])
right = self.buildTree(preorder, inorder[midIdx + 1:])

return TreeNode(mid, left, right)

28 changes: 28 additions & 0 deletions decode-ways/HodaeSsi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution:
def numDecodings(self, s: str) -> int:
dp = []
if (s[0] == '0'):
return 0
dp.append(1)

for idx, _ in enumerate(s):
if idx == 0:
continue
if s[idx] == '0':
Comment on lines +10 to +11
Copy link
Contributor

Choose a reason for hiding this comment

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

이렇게 if-elif-else 구문이 많으면 논리적으로 분리되는 지점에 나눠주시면 가독성에 도움이 됩니다

if s[idx-1] == '1' or s[idx-1] == '2':
if idx == 1:
dp.append(1)
else:
dp.append(dp[idx-2])
else:
return 0
elif s[idx-1] == '1' or (s[idx-1] == '2' and (s[idx] >= '1' and s[idx] <= '6')):
if idx == 1:
dp.append(2)
else:
dp.append(dp[idx-1] + dp[idx-2])
else:
dp.append(dp[idx-1])

return dp[-1]

19 changes: 19 additions & 0 deletions valid-anagram/HodaeSsi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
sLetterDict = {}
tLetterDict = {}

for letter in s:
sLetterDict[letter] = sLetterDict.get(letter, 0) + 1
Copy link
Contributor

Choose a reason for hiding this comment

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

기본 메서드를 활용한 깔끔한 라인이 좋습니다

for letter in t:
tLetterDict[letter] = tLetterDict.get(letter, 0) + 1

if len(sLetterDict) != len(tLetterDict):
return False

for sKey in sLetterDict.keys():
if sKey not in tLetterDict or sLetterDict[sKey] != tLetterDict[sKey]:
return False

return True

Loading