Skip to content

Commit 72c25c7

Browse files
authored
[alphaorderly] WEEK 02 Solutions (#2673)
* week 1 * [alphaorderly] WEEK 02 Solutions * fix: description에 맞게 코드 수정 * fix: 좀 더 간결하게 수정
1 parent 4e46337 commit 72c25c7

5 files changed

Lines changed: 176 additions & 0 deletions

File tree

3sum/alphaorderly.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution:
2+
def threeSum(self, nums: list[int]) -> list[list[int]]:
3+
"""
4+
O(N^2)
5+
"""
6+
cntr = Counter(nums)
7+
8+
ans = []
9+
10+
# First case -> [0, 0, 0] Triplet
11+
if cntr[0] >= 3:
12+
ans.append([0, 0, 0])
13+
14+
# Second case -> two same numbers + one diff number
15+
for k, v in cntr.items():
16+
if k == 0:
17+
continue
18+
if v >= 2 and -(k * 2) in cntr:
19+
ans.append([k, k, -(k * 2)])
20+
21+
s = set(nums)
22+
nums = list(s)
23+
nums.sort()
24+
25+
pos = {k: v for v, k in enumerate(nums)}
26+
N = len(nums)
27+
28+
# Third case -> all different numbers
29+
for i in range(N - 2):
30+
for j in range(i + 1, N - 1):
31+
target = -(nums[i] + nums[j])
32+
if target in pos:
33+
k = pos[target]
34+
if k > j:
35+
ans.append([nums[i], nums[j], nums[k]])
36+
37+
return ans

climbing-stairs/alphaorderly.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# class Solution:
2+
# def climbStairs(self, n: int) -> int:
3+
# """
4+
# O(N) with additional space
5+
# """
6+
# if n <= 2:
7+
# return n
8+
9+
# dp = [0] * (n + 1)
10+
# dp[1] = 1
11+
# dp[2] = 2
12+
13+
# for i in range(3, n + 1):
14+
# dp[i] = dp[i - 1] + dp[i - 2]
15+
16+
# return dp[-1]
17+
18+
# class Solution:
19+
# """
20+
# O(N) Found that solution is fibonacci number
21+
# """
22+
# def climbStairs(self, n: int) -> int:
23+
# if n <= 2:
24+
# return n
25+
26+
# p1, p2 = 1, 2
27+
# for i in range(3, n + 1):
28+
# p1, p2 = p2, p1 + p2
29+
30+
# return p2
31+
32+
33+
# Fibonacci hack
34+
class Solution:
35+
def climbStairs(self, n: int) -> int:
36+
"""
37+
O(Log N)
38+
"""
39+
40+
# O(1) operation
41+
def mat_mul(a: List[List[int]], b: List[List[int]]) -> List[List[int]]:
42+
ans = [[0] * 2 for _ in range(2)]
43+
44+
for i in range(2):
45+
for j in range(2):
46+
for k in range(2):
47+
ans[i][j] += a[i][k] * b[k][j]
48+
49+
return ans
50+
51+
# O(Log N) operation
52+
def mat_pow(a: List[List[int]], n: int) -> List[List[int]]:
53+
if n == 1:
54+
return [[1, 1], [1, 0]]
55+
56+
half = mat_pow(a, n // 2)
57+
multed = mat_mul(half, half)
58+
59+
if n % 2:
60+
return mat_mul(multed, [[1, 1], [1, 0]])
61+
else:
62+
return multed
63+
64+
ans = mat_pow([[1, 1], [1, 0]], n)
65+
66+
return ans[0][0]
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution:
2+
def productExceptSelf(self, nums: List[int]) -> List[int]:
3+
"""
4+
O(N) Time complexity
5+
"""
6+
N = len(nums)
7+
8+
ans = [0] * N
9+
10+
acc = 1
11+
12+
for i in range(N):
13+
ans[i] = acc
14+
acc *= nums[i]
15+
16+
acc = 1
17+
18+
for i in range(N - 1, -1, -1):
19+
ans[i] *= acc
20+
acc *= nums[i]
21+
22+
return ans

valid-anagram/alphaorderly.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def isAnagram(self, s: str, t: str) -> bool:
3+
"""
4+
O(N Log N)
5+
"""
6+
return sorted(s) == sorted(t)
7+
8+
# class Solution:
9+
# """
10+
# O(N)
11+
# """
12+
# def isAnagram(self, s: str, t: str) -> bool:
13+
# return Counter(s) == Counter(t)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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], lower: Optional[int] = -float('inf'), upper: Optional[int] = float('inf')) -> bool:
10+
# if not root:
11+
# return True
12+
13+
# if root.val <= lower or root.val >= upper:
14+
# return False
15+
16+
# return self.isValidBST(root.left, lower, root.val) and self.isValidBST(root.right, root.val, upper)
17+
18+
19+
class Solution:
20+
def isValidBST(self, root: Optional[TreeNode]) -> bool:
21+
"""
22+
O(N)
23+
"""
24+
s = [(root, -float("inf"), float("inf"))]
25+
26+
while s:
27+
node, lower, upper = s.pop()
28+
29+
if node.val <= lower or node.val >= upper:
30+
return False
31+
32+
if node.left:
33+
s.append((node.left, lower, node.val))
34+
35+
if node.right:
36+
s.append((node.right, node.val, upper))
37+
38+
return True

0 commit comments

Comments
 (0)