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
37 changes: 37 additions & 0 deletions 3sum/YOOHYOJEONG.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# https://leetcode.com/problems/3sum

class Solution(object):
def threeSum(self, nums):
nums.sort()
result = []

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:
left += 1

elif total > 0:
right -= 1

else:
result.append([nums[i], nums[left], nums[right]])

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

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

left += 1
right -= 1

return result
12 changes: 12 additions & 0 deletions climbing-stairs/YOOHYOJEONG.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# https://leetcode.com/problems/climbing-stairs

class Solution(object):
def climbStairs(self, n):
if n <= 2:
return n

a, b = 1, 2
for i in range(3, n+1):
a, b = b, a+b
Copy link
Contributor

Choose a reason for hiding this comment

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

파이썬의 다중 할당이 편리하네요.

Copy link
Contributor

Choose a reason for hiding this comment

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

다른 변수를 할당하지 않고 이렇게도 풀이할 수 있군요 ㅎㅎ👍


return b
19 changes: 19 additions & 0 deletions product-of-array-except-self/YOOHYOJEONG.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# https://leetcode.com/problems/product-of-array-except-self/

class Solution(object):
def productExceptSelf(self, nums):

n = len(nums)
output = [1] * n

left = 1
for i in range(n):
output[i] = left
left *= nums[i]

right = 1
for i in range(n-1, -1, -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문 조건에서 감산하는 형태로 사용하는 걸 지양하는 편인데,
'몇번 반복하도록 되어있는가?'라는 측면에서 가독성이 조금 떨어진다고 생각하기때문입니다.
효정님은 어떻게 생각하시나요? ㅎㅎ 생각을 나누고 싶어 코멘트 남깁니다.

for i in range(n):
    idx = n - i
    output[idx] *= right
...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

좋은 의견 감사합니다!!
오른쪽 누적곱을 계산하려고 역순으로 순회하면서 작성했는데 말씀해주신 것처럼 반복 횟수 관점에서는 가독성이 떨어질 수 있다는 점 공감합니다.

Python에서는 보통 reversed(range(n)) 같은 방식으로도 표현할 수 있을 것 같아서 그 부분도 한 번 고려해 볼 수 있을 것 같습니다!!

output[i] *= right
right *= nums[i]

return output
8 changes: 8 additions & 0 deletions valid-anagram/YOOHYOJEONG.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# https://leetcode.com/problems/valid-anagram/

class Solution(object):
def isAnagram(self, s, t):
if 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.

sorted함수가 대부분의 언어에서 n log n 시간복잡도를 가지기 때문에,
보다 더 효율적인 방법(O(n))이 있을 것 같습니다!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@hyeri0903 님 덕분에 Counter를 사용하면 정렬 없이 O(n) 시간복잡도로 해결할 수 있다는 점을 알게 되었습니다!!

두 분 모두 감사합니다!

return True
else:
Copy link
Contributor

@hyeri0903 hyeri0903 Mar 14, 2026

Choose a reason for hiding this comment

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

if 구문을 사용하지 않고 한줄로 작성할수도 있을것 같네요!

return sorted(s) == sorted(t)

Counter 를 사용한다면 시간 복잡도를 O(n)으로 줄일 수 있을것 같습니다.

from collections import Counter

def isAnagram(self, s, t):
    return Counter(s) == Counter(t)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

정렬을 이용한 방식이 간단한 것 같아서 이렇게 풀었는데 말씀해주신 것처럼 Counter를 사용하면 O(n)으로 더 효율적으로 해결할 수 있을 것 같습니다!
또 return sorted(s) == sorted(t)처럼 더 간결하게 표현할 수도 있을 것 같습니다!

좋은 방법 공유해주셔서 감사합니다!

return False
25 changes: 25 additions & 0 deletions validate-binary-search-tree/YOOHYOJEONG.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# https://leetcode.com/problems/validate-binary-search-tree/description/

# 도저히 안풀어서 지피티의 도움을 받았습니다..TT
# 새로 알게 된 개념
# 이진 트리는 하나의 노드가 최대 두 개의 자식 노드를 가지는 트리 자료구조
# 두 개의 포인터를 가지고 있으며, 루트에서 시작해서 아래로 내려가는 계층 구조
# Binary Search Tree(BST)는 이진 트리의 한 종류로 어떤 노드의 왼쪽 서브트리에 있는 모든 값은 해당 노드보다 작고 오른쪽 서브트리에 있는 모든 값은 해당 노드보다 큼
# 이 문제는 주어진 트리가 이 규칙을 만족하는지 확인하는 문제임
# 각 노드가 가질 수 있는 허용 범위(min, max)를 유지하면서 검사해야 함
class Solution:
def isValidBST(self, root):

def dfs(node, low, high):
if not node:
return True

if node.val <= low or node.val >= high:
return False

return (
dfs(node.left, low, node.val) and
dfs(node.right, node.val, high)
)

return dfs(root, float("-inf"), float("inf"))
Loading