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
55 changes: 55 additions & 0 deletions NeetCode/combination-sum-2/step1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""
candidatesに負の値が含まれるのかは重要
"""

from collections import Counter

class Solution:
def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
res = []
counts = Counter(candidates)
unique_candidates = list(counts.keys())
comb = []

def append_comb(index, sum_values):
if sum_values == target:
res.append(comb.copy())
return
if sum_values > target or index == len(unique_candidates):
return

append_comb(index + 1, sum_values)
num = unique_candidates[index]
for i in range(counts[num]):
comb.append(num)
append_comb(index + 1, sum_values + num * (i + 1))
for _ in range(counts[num]):
comb.pop()

append_comb(0, 0)
return res

class Solution:
def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
res = []
sorted_candidates = sorted(candidates)
comb = []

def append_comb(index, sum_vals):
if sum_vals == target:
res.append(comb.copy())
return
if sum_vals > target or index == len(candidates):
return

num = sorted_candidates[index]
comb.append(num)
append_comb(index + 1, sum_vals + num)
comb.pop()

while index + 1 < len(candidates) and sorted_candidates[index] == sorted_candidates[index + 1]:
index += 1
append_comb(index + 1, sum_vals)

append_comb(0, 0)
return res
3 changes: 3 additions & 0 deletions NeetCode/combination-sum-2/step2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""
まあtotalのほうが良いか
"""
7 changes: 7 additions & 0 deletions NeetCode/subsets-2/step3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
aaaa
ere

fa
f
ewq
af