From 07c755912d4a73636ae359ae259b220caba6853b Mon Sep 17 00:00:00 2001 From: ymir0804 Date: Sun, 4 Jan 2026 16:03:54 +0900 Subject: [PATCH] =?UTF-8?q?Combination=20Sum=20=EB=AC=B8=EC=A0=9C=20?= =?UTF-8?q?=ED=92=80=EC=9D=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- combination-sum/ymir0804.java | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 combination-sum/ymir0804.java diff --git a/combination-sum/ymir0804.java b/combination-sum/ymir0804.java new file mode 100644 index 0000000000..e22f480a1a --- /dev/null +++ b/combination-sum/ymir0804.java @@ -0,0 +1,31 @@ +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +class Solution { + public List> combinationSum(int[] candidates, int target) { + List> result = new ArrayList<>(); + Arrays.sort(candidates); + backtrack(candidates, target, new ArrayList<>(), result, 0); + return result; + } + + + public void backtrack(int[] candidates, int remain, List current, List> result, int start) { + if (remain < 0) { + return; + } + if (remain == 0) { + result.add(new ArrayList<>(current)); + return; + } + + for (int i = start; i < candidates.length; i++) { + + if (candidates[i] > remain) break; + current.add(candidates[i]); + backtrack(candidates, remain - candidates[i], current, result, i); + current.remove(current.size() - 1); + } + } +}