Skip to content
Open
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 combination-sum/jylee2033.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
output = []

def backtrack(remain, comb, start):
if remain == 0:
output.append(list(comb))
return

elif remain < 0:
return

for i in range(start, len(candidates)):
comb.append(candidates[i])
backtrack(remain - candidates[i], comb, i)
comb.pop()

backtrack(target, [], 0)
return output

# Time Complexity : O(N^T/M) N - number of candidates, T - target, M - minimum value in candidates
# Space Complexity : O(T/M) - maximum depth of the recursion tree
27 changes: 27 additions & 0 deletions number-of-1-bits/jylee2033.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution:
def hammingWeight(self, n: int) -> int:
# 1. Find highest power of 2 range that covers n
Copy link
Contributor

Choose a reason for hiding this comment

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

커버 가능한 2의 거듭제곱수를 먼저 구하고 역으로 계산하신 방법으로 푸신 거군요!
직관적으로 이해하기 쉬운 방법인 것 같습니다 👍🏼

10진수를 2진수로 변경하는 문제는 나누기 연산자와 모듈러 연산자로도 풀이가 가능해서
저는 그런 방식으로 풀어보았습니다!

# 2. Greedily subtract from largest power of 2 to smallest

# powers : 1 + 2 + 4 + 8 + 16 + ...
# total : 1 + 3 + 7 + 15 + 31 + ...
# index : 0 1 2 3 4

i = 0
total = 1

while n > total:
i += 1
total += 2 ** i

count = 0

for j in range(i, -1, -1):
if n >= 2 ** j:
n -= 2 ** j
count += 1

return count

# Time Complexity : O(log n)
# Space Complexity : O(1)
14 changes: 14 additions & 0 deletions valid-palindrome/jylee2033.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution:
def isPalindrome(self, s: str) -> bool:
# 1. Remove non-alphanumeric characters and convert to lowercase
# 2. Compare with reversed string

cleaned_s = ""
for ch in s:
if ch.isalnum():
cleaned_s += ch.lower()

return cleaned_s == cleaned_s[::-1]
Copy link
Contributor

Choose a reason for hiding this comment

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

이런 풀이도 가능하겠네요! 코드가 깔끔해지는 풀이라고 생각되네요 :)

다만 문자열의 반까지만 나눠서 풀이를 하는 것도 가능하겠습니다~!

for i in range(len(cleaned_s // 2):
    if cleaned_s[i] != cleaned_s [len(cleaned_s) -1 - i]:
        return False  # early return 처리!


# Time Complexity : O(n)
# Space Complexity : O(n)
Copy link
Contributor

Choose a reason for hiding this comment

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

투포인터를 이용하면 공간복잡도도 O(1)까지 줄일 수 있습니다🙌

Loading