From b2f5e21e45fc088fb86bdc1f66cb3f6cd4611027 Mon Sep 17 00:00:00 2001 From: mike <59136831+Mike0121@users.noreply.github.com> Date: Wed, 24 Apr 2024 21:55:54 +0900 Subject: [PATCH 1/2] =?UTF-8?q?39.=20Combination=20Sum=20=E3=83=AC?= =?UTF-8?q?=E3=83=93=E3=83=A5=E3=83=BC=E4=BE=9D=E9=A0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 39. Combination Sum https://leetcode.com/problems/combination-sum/description/?envType=list&envId=rbx9vwti --- .../39. Combination Sum" | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 "\347\253\266\346\212\200\343\203\227\343\203\255\345\260\261\346\264\273\351\203\250PR\347\224\250/39. Combination Sum" diff --git "a/\347\253\266\346\212\200\343\203\227\343\203\255\345\260\261\346\264\273\351\203\250PR\347\224\250/39. Combination Sum" "b/\347\253\266\346\212\200\343\203\227\343\203\255\345\260\261\346\264\273\351\203\250PR\347\224\250/39. Combination Sum" new file mode 100644 index 0000000..b3be849 --- /dev/null +++ "b/\347\253\266\346\212\200\343\203\227\343\203\255\345\260\261\346\264\273\351\203\250PR\347\224\250/39. Combination Sum" @@ -0,0 +1,27 @@ +``` python +3回目 +# 時間計算量: O(2^N) +# 空間計算量: O(N) +N = len(candidates) + +class Solution: + def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: + + all_valid_combination = [] + + def find_vaild_combination(index, current_target, current_combination): + if len(candidates) <= index: + if current_target == 0: + all_valid_combination.append(current_combination[:]) + return + + if candidates[index] <= current_target: + current_combination.append(candidates[index]) + find_vaild_combination(index, current_target-candidates[index], current_combination) + current_combination.pop() + + find_vaild_combination(index+1, current_target, current_combination) + + find_vaild_combination(0, target, []) + return all_valid_combination +``` From c64c22a0d4a28d42368584ad2ba520b50d53b887 Mon Sep 17 00:00:00 2001 From: mike <59136831+Mike0121@users.noreply.github.com> Date: Sun, 28 Apr 2024 17:55:23 +0900 Subject: [PATCH 2/2] Create 39.-Combination-Sum 4th.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 4周目 --- .../39.-Combination-Sum 4th.md" | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 "\347\253\266\346\212\200\343\203\227\343\203\255\345\260\261\346\264\273\351\203\250PR\347\224\250/39.-Combination-Sum 4th.md" diff --git "a/\347\253\266\346\212\200\343\203\227\343\203\255\345\260\261\346\264\273\351\203\250PR\347\224\250/39.-Combination-Sum 4th.md" "b/\347\253\266\346\212\200\343\203\227\343\203\255\345\260\261\346\264\273\351\203\250PR\347\224\250/39.-Combination-Sum 4th.md" new file mode 100644 index 0000000..c98a303 --- /dev/null +++ "b/\347\253\266\346\212\200\343\203\227\343\203\255\345\260\261\346\264\273\351\203\250PR\347\224\250/39.-Combination-Sum 4th.md" @@ -0,0 +1,24 @@ +### 時間: 3m51s +### Error: 0 +### 時間計算量: O(len(candidates)^(target/min(candidates) + 1)) +### 空間計算量: O(target/min(candidates) + space for Answer) +``` +class Solution: + def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: + result = [] + combination = [] + + def find_all_combiantions(index, current_target, combination): + if index == len(candidates): + if current_target == 0: + result.append(combination) + return + + if candidates[index] <= current_target: + find_all_combiantions(index, current_target - candidates[index], combination + [candidates[index]]) + + find_all_combiantions(index + 1, current_target, combination) + + find_all_combiantions(0, target, []) + return result +```