Skip to content

Commit a2c4129

Browse files
authored
[daehyun99] WEEK02 solutions (#2687)
* Solve Valid-Anagram * Solve: Climbing Stairs * Solve: Product of Array except self * Solve
1 parent 6dfc9fc commit a2c4129

5 files changed

Lines changed: 125 additions & 0 deletions

File tree

3sum/daehyun99.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
class Solution:
2+
def threeSum(self, nums: List[int]) -> List[List[int]]:
3+
res = []
4+
nums.sort()
5+
6+
for i, a in enumerate(nums):
7+
if a > 0:
8+
break
9+
if i > 0 and a == nums[i-1]:
10+
continue
11+
12+
l, r = i+1, len(nums) - 1
13+
while l < r:
14+
total = a + nums[l] + nums[r]
15+
if total > 0:
16+
r -= 1
17+
elif total < 0:
18+
l += 1
19+
else:
20+
res.append([a, nums[l], nums[r]])
21+
l += 1
22+
r -= 1
23+
while nums[l] == nums[l - 1] and l < r:
24+
l += 1
25+
return res
26+
27+
"""from collections import Counter
28+
29+
class Solution:
30+
def threeSum(self, nums: list[int]) -> list[list[int]]:
31+
counts = Counter(nums)
32+
result: set[tuple[int, int, int]] = set()
33+
34+
for i in range(len(nums) - 2):
35+
for j in range(i + 1, len(nums) - 1):
36+
adding = -(nums[i] + nums[j])
37+
38+
if adding not in counts:
39+
continue
40+
41+
local_counts = Counter([nums[i], nums[j], adding])
42+
43+
for num, count in local_counts.items():
44+
if counts[num] < count:
45+
break
46+
else:
47+
triplet = tuple(sorted([nums[i], nums[j], adding]))
48+
result.add(triplet)
49+
50+
return [list(triplet) for triplet in result]
51+
"""

climbing-stairs/daehyun99.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def climbStairs(self, n: int) -> int:
3+
one_count = n
4+
two_count = 0
5+
total_count = 0
6+
7+
while one_count >=0 and two_count >=0:
8+
# 조합
9+
total = one_count + two_count
10+
11+
count = 1
12+
for i in range(two_count):
13+
count *= total - i
14+
for i in range(two_count, 0, -1):
15+
count /= i
16+
total_count += count
17+
18+
one_count -=2
19+
two_count +=1
20+
return int(total_count)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def productExceptSelf(self, nums: List[int]) -> List[int]:
3+
product = 1
4+
zero_pos = set()
5+
6+
for i in range(len(nums)):
7+
num = nums[i]
8+
if num != 0:
9+
product *= num
10+
else:
11+
zero_pos.add(i)
12+
13+
if len(zero_pos) >= 2:
14+
return [0 for num in nums]
15+
elif len(zero_pos) == 1:
16+
return [0 if i not in zero_pos else product for i in range(len(nums))]
17+
else:
18+
return [int(product / num) for num in nums]

valid-anagram/daehyun99.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from collections import defaultdict
2+
class Solution:
3+
def isAnagram(self, s: str, t: str) -> bool:
4+
count = defaultdict(int)
5+
6+
if len(s) != len(t):
7+
return False
8+
9+
for s_, t_ in zip(s, t):
10+
count[s_] += 1
11+
count[t_] -= 1
12+
13+
for key, val in count.items():
14+
if val != 0:
15+
return False
16+
return True
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
8+
class Solution:
9+
def isValidBST(self, root: Optional[TreeNode]) -> bool:
10+
def valid(node, left, right):
11+
if not node:
12+
return True
13+
if not (left < node.val < right):
14+
return False
15+
16+
return (
17+
valid(node.left, left, node.val) and
18+
valid(node.right, node.val, right)
19+
)
20+
return valid(root, float("-inf"), float("inf"))

0 commit comments

Comments
 (0)