Skip to content

Commit 8928b2b

Browse files
authored
Merge pull request #2102 from HYUNAHKO/main
[HYUNAHKO] WEEK 02 solutions
2 parents a5005a3 + b6436f7 commit 8928b2b

File tree

6 files changed

+114
-0
lines changed

6 files changed

+114
-0
lines changed

climbing-stairs/HYUNAHKO.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution:
2+
def climbStairs(self, n: int) -> int:
3+
if (n<1 or n>45):
4+
return 0
5+
6+
def factorial(num):
7+
if num <= 1:
8+
return 1
9+
10+
result = 1
11+
for i in range(2, num+1):
12+
result *= i
13+
return result
14+
15+
steps = 0
16+
cur_steps = 1
17+
quotient = n // 2
18+
k=0
19+
20+
for _ in range(quotient+1):
21+
cur_steps = factorial(n-k) / (factorial(k)*factorial(n-2*k))
22+
k+=1
23+
steps += cur_steps
24+
25+
return int(steps)
26+
27+

combination-sum/HYUNAHKO.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution:
2+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
3+
result = []
4+
5+
def backtrack(start_idx, current_combination, current_sum):
6+
if current_sum == target:
7+
result.append(current_combination[:])
8+
return
9+
if current_sum > target:
10+
return
11+
12+
# 모든 후보 탐색
13+
for i in range(start_idx, len(candidates)):
14+
# 현재 숫자 선택
15+
current_combination.append(candidates[i])
16+
17+
# 같은 숫자를 다시 사용할 수 있으므로 i부터 시작
18+
backtrack(i, current_combination, current_sum + candidates[i])
19+
20+
# 백트래킹
21+
current_combination.pop()
22+
23+
backtrack(0, [], 0)
24+
return result

number-of-1-bits/HYUNAHKO.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def hammingWeight(self, n: int) -> int:
3+
result = 1
4+
while (n//2 != 0):
5+
remainder = n % 2
6+
if (remainder==1):
7+
result+=1
8+
n = n//2
9+
return result
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+
n = len(nums)
4+
result_list = [0] * n
5+
if len(nums) <2 or len(nums) > 1e5:
6+
return None
7+
8+
p = 1
9+
for i in range(n):
10+
result_list[i] = p
11+
p *= nums[i]
12+
13+
p = 1
14+
for i in range(n - 1, -1, -1):
15+
result_list[i] *= p
16+
p *= nums[i]
17+
18+
return result_list

valid-anagram/HYUNAHKO.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from collections import Counter
2+
3+
class Solution:
4+
def isAnagram(self, s: str, t: str) -> bool:
5+
letter_dict1 = {}
6+
letter_dict2 = {}
7+
letter_list1 = list(s)
8+
letter_list2 = list(t)
9+
10+
for i in letter_list1:
11+
letter_dict1[i] = letter_dict1.get(i, 0) + 1
12+
13+
for j in letter_list2:
14+
letter_dict2[j] = letter_dict2.get(j, 0) + 1
15+
16+
if (letter_dict1 == letter_dict2):
17+
return True
18+
else:
19+
return False
20+
21+
22+
class Solution:
23+
def isAnagram(self, s: str, t: str) -> bool:
24+
return Counter(s) == Counter(t)
25+

valid-palindrome/HYUNAHKO.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def isPalindrome(self, s: str) -> bool:
3+
s = s.lower()
4+
result = [char for char in s if char.isalnum()]
5+
6+
n = len(result)
7+
for i in range(n):
8+
str1 = result[i]
9+
if (str1 != result[n-i-1]):
10+
return False
11+
return True

0 commit comments

Comments
 (0)