Skip to content
Merged
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
26 changes: 26 additions & 0 deletions combination-sum/daiyongg-kim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
candidates.sort()
result = []

def backtracking(start_index, current_combination, current_sum):
if current_sum == target:
result.append(list(current_combination))
return

if current_sum > target:
return

for i in range(start_index, len(candidates)):
candidate = candidates[i]

if current_sum + candidate > target:
Copy link
Contributor

Choose a reason for hiding this comment

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

sorting & pruning을 이용해 최적화까지 하신 점이 인상깊습니다!

break

current_combination.append(candidate)
backtracking(i, current_combination, current_sum + candidate)

current_combination.pop()

backtracking(0, [], 0)
return result
Empty file added decode-ways/daiyongg-kim.py
Empty file.
10 changes: 10 additions & 0 deletions maximum-subarray/daiyongg-kim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
current_sum = nums[0]
max_sum = nums[0]

for i in range(1, len(nums)):
current_sum = max(nums[i], current_sum + nums[i])
max_sum = max(current_sum, max_sum)

return max_sum
10 changes: 10 additions & 0 deletions number-of-1-bits/daiyongg-kim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Solution:
def hammingWeight(self, n: int) -> int:
cnt = 0

while n != 0:
if n & 1:
cnt += 1
n = n >> 1
Copy link
Contributor

Choose a reason for hiding this comment

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

n & n-1로 오른쪽에서부터 1인 비트를 하나씩 지워가며 개수를 세는 방식인 Brian Kernighan's 알고리즘이라는 방법도 있어 공유드립니다~!

https://leetcode.com/problems/number-of-1-bits/solutions/4341511/faster-lesser-3-methods-simple-count-brian-kernighan-s-algorithm-bit-manipulation-explained

Copy link
Contributor Author

Choose a reason for hiding this comment

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

감사합니다.


return cnt
4 changes: 4 additions & 0 deletions valid-palindrome/daiyongg-kim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Solution:
def isPalindrome(self, s: str) -> bool:
s = re.sub(r'[^0-9A-Za-z]', '',s).lower()
return s == s[::-1]