Skip to content

Commit 237b11a

Browse files
authored
Merge pull request #1247 from hestia-park/main
[hestia-park] WEEK 02 solutions
2 parents 4501713 + 01f1f0d commit 237b11a

File tree

6 files changed

+89
-0
lines changed

6 files changed

+89
-0
lines changed

3sum/hestia-park.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
def threeSum(self, nums: List[int]) -> List[List[int]]:
3+
nums.sort()
4+
answer=set()
5+
for i in range(len(nums)):
6+
if nums[i] > 0:
7+
break # 이후 숫자는 모두 양수 → 합이 0 불가능
8+
if i > 0 and nums[i] == nums[i - 1]:
9+
continue
10+
11+
left, right = i + 1, len(nums) - 1
12+
while left < right:
13+
term = nums[i] + nums[left] + nums[right]
14+
if term == 0:
15+
answer.add((nums[i], nums[left], nums[right]))
16+
left += 1
17+
right -= 1
18+
elif term < 0:
19+
left += 1
20+
else:
21+
right -= 1
22+
23+
return [list(triplet) for triplet in answer]
24+
25+

climbing-stairs/hestia-park.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution:
2+
def climbStairs(self, n: int) -> int:
3+
if n <= 1:
4+
return 1
5+
6+
# dp = [0] * (n + 1)
7+
# dp[0] = 1
8+
# dp[1] = 1
9+
10+
# for i in range(2, n + 1):
11+
# dp[i] = dp[i - 1] + dp[i - 2]
12+
13+
# return dp[n]
14+
# optima sol
15+
dp_0,dp_1=1,1
16+
for _ in range(2,n+1):
17+
dp_0,dp_1=dp_1,dp_0+dp_1
18+
return dp_1
19+
20+
21+

contains-duplicate/hestia-park.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution:
2+
def containsDuplicate(self, nums: List[int]) -> bool:
3+
return len(nums) != len(set(nums))
4+
5+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def productExceptSelf(self, nums: List[int]) -> List[int]:
3+
n=len(nums)
4+
answer=[1] * n
5+
#for left
6+
for i in range(1,n):
7+
answer[i]=answer[i-1]*nums[i-1]
8+
9+
tail=1
10+
#for right
11+
for i in range(n-1,-1,-1):
12+
answer[i] *= tail
13+
tail *= nums[i]
14+
return answer
15+
16+
17+

valid-anagram/hestia-park.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def isAnagram(self, s: str, t: str) -> bool:
3+
if len(s) != len(t):
4+
return False
5+
counter_s=Counter(s)
6+
counter_t=Counter(t)
7+
return counter_s == counter_t
8+
9+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def isValidBST(self, root: Optional[TreeNode]) -> bool:
3+
def check(node, min_val, max_val):
4+
if not node:
5+
return True
6+
if not (min_val < node.val < max_val):
7+
return False
8+
return check(node.left, min_val, node.val) and check(node.right, node.val, max_val)
9+
10+
return check(root, float('-inf'), float('inf'))
11+
12+

0 commit comments

Comments
 (0)