Skip to content

[printjin-gmailcom] WEEK 02 Solutions #1219

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 16 commits into from
Apr 13, 2025
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
22 changes: 22 additions & 0 deletions 3sum/printjin-gmailcom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution:
def threeSum(self, nums):
triplets = []
nums.sort()
for i in range(len(nums) - 2):
if i > 0 and nums[i - 1] == nums[i]:
continue
low, high = i + 1, len(nums) - 1
while low < high:
three_sum = nums[i] + nums[low] + nums[high]
if three_sum < 0:
low += 1
elif three_sum > 0:
high -= 1
else:
triplets.append([nums[i], nums[low], nums[high]])
while low < high and nums[low] == nums[low + 1]:
low += 1
while low < high and nums[high] == nums[high - 1]:
high -= 1
low, high = low + 1, high - 1
return triplets
8 changes: 8 additions & 0 deletions climbing-stairs/printjin-gmailcom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Solution:
def climbStairs(self, n: int):
if n <= 2:
return n
a, b = 1, 2
for _ in range(3, n+1):
a, b = b, a + b
return b
13 changes: 13 additions & 0 deletions product-of-array-except-self/printjin-gmailcom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution:
def productExceptSelf(self, nums):
n = len(nums)
answer = [1] * n
left = 1
for i in range(n):
answer[i] = left
left *= nums[i]
right = 1
for i in range(n-1, -1, -1):
answer[i] *= right
right *= nums[i]
return answer
5 changes: 5 additions & 0 deletions valid-anagram/printjin-gmailcom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from collections import Counter

class Solution:
def isAnagram(self, s: str, t: str):
return Counter(s) == Counter(t)
Comment on lines +3 to +5
Copy link
Contributor

Choose a reason for hiding this comment

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

python에서 제공하는 Counter 자료구조를 사용하면 코드가 정말 간단해지네요!

16 changes: 16 additions & 0 deletions validate-binary-search-tree/printjin-gmailcom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution:
def isValidBST(self, root):
max_val = float("-inf")
def dfs(node):
if not node:
return True
nonlocal max_val
if not dfs(node.left):
return False
if max_val >= node.val:
return False
max_val = node.val
if not dfs(node.right):
return False
return True
return dfs(root)
Comment on lines +1 to +16
Copy link
Contributor

Choose a reason for hiding this comment

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

저는 트리 탐색의 결과를 List 로 저장을 하여 최종적으로 오름차순 검증을 하였는데,
이 풀이 방법처럼 탐색 과정에서 max_val을 업데이트하면서 노드 값과 비교(= 오름차순 검증)를 할 수도 있군요!