Skip to content

Commit 70732a7

Browse files
committed
fix pullrequests/combination_sum
1 parent 8c8e39b commit 70732a7

File tree

4 files changed

+60
-4
lines changed

4 files changed

+60
-4
lines changed

pullrequests/combination_sum/step1.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package template
55
時間:24分
66
少し前にバックトラッキングの問題を解いたので、ある条件を満たす全ての組み合わせを求めるためにバックトラッキングを使って解きました。
77
*/
8-
func combinationSum_step1(candidates []int, target int) [][]int {
8+
func combinationSumStep1(candidates []int, target int) [][]int {
99
var combinations [][]int
1010
var stack []int
1111
var findCombinations func(int, int)

pullrequests/combination_sum/step2.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package template
55
Step1では再帰を使ってバックトラッキングを解いたので、スタックを使った方法も実装しました。
66
また、Step1の実装のリファクタもしました。
77
*/
8-
func combinationSum_backtracking_stack(candidates []int, target int) [][]int {
8+
func combinationSumBacktrackingStack(candidates []int, target int) [][]int {
99
combinations := [][]int{}
1010
type state struct {
1111
combination []int
@@ -33,7 +33,7 @@ func combinationSum_backtracking_stack(candidates []int, target int) [][]int {
3333
return combinations
3434
}
3535

36-
func combinationSum_backtracking_recursion(candidates []int, target int) [][]int {
36+
func combinationSumBacktrackingRecursion(candidates []int, target int) [][]int {
3737
var combinations [][]int
3838
var stack []int
3939
var generateCombinations func(int, int)

pullrequests/combination_sum/step3.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package template
55
動的計画法を使った方法も実装しました。
66
targetごとに組み合わせを求めると重複する組み合わせが生じてしまうので、candidateごとに組み合わせを求めるようにしました。
77
*/
8-
func combinationSum_dp(candidates []int, target int) [][]int {
8+
func combinationSumDP(candidates []int, target int) [][]int {
99
combinationsGroups := make([][][]int, target+1)
1010
combinationsGroups[0] = [][]int{{}}
1111
for _, candidate := range candidates {

pullrequests/combination_sum/step4.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//lint:file-ignore U1000 Ignore all unused code
2+
package template
3+
4+
/*
5+
Step1では再帰を使ってバックトラッキングを解いたので、スタックを使った方法も実装しました。
6+
また、Step1の実装のリファクタもしました。
7+
*/
8+
func combinationSumBacktrackingStackStep4(candidates []int, target int) [][]int {
9+
combinations := [][]int{}
10+
type state struct {
11+
combination []int
12+
sum int
13+
index int
14+
}
15+
stack := []state{{[]int{}, 0, 0}}
16+
for len(stack) > 0 {
17+
current := stack[len(stack)-1]
18+
stack = stack[:len(stack)-1]
19+
if current.sum == target {
20+
combinations = append(combinations, current.combination)
21+
continue
22+
}
23+
for i := current.index; i < len(candidates); i++ {
24+
newSum := current.sum + candidates[i]
25+
if newSum > target {
26+
continue
27+
}
28+
newCombination := append([]int{}, current.combination...)
29+
newCombination = append(newCombination, candidates[i])
30+
stack = append(stack, state{newCombination, newSum, i})
31+
}
32+
}
33+
return combinations
34+
}
35+
36+
func combinationSumBacktrackingRecursionStep4(candidates []int, target int) [][]int {
37+
var combinations [][]int
38+
var stack []int
39+
var generateCombinations func(int, int)
40+
generateCombinations = func(currentIndex int, sum int) {
41+
if sum == target {
42+
combinations = append(combinations, append([]int{}, stack...))
43+
return
44+
}
45+
if sum > target {
46+
return
47+
}
48+
for i := currentIndex; i < len(candidates); i++ {
49+
stack = append(stack, candidates[i])
50+
generateCombinations(i, sum+candidates[i])
51+
stack = stack[:len(stack)-1]
52+
}
53+
}
54+
generateCombinations(0, 0)
55+
return combinations
56+
}

0 commit comments

Comments
 (0)