From 1f4a6c2d1b7c4852ffc32b6a114fa933bd231803 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=B6khan=20=C3=87a=C4=9Fr=C4=B1tekin?= Date: Thu, 7 Jul 2022 21:49:07 +0300 Subject: [PATCH 1/8] added leetcode 78 golang backtracking subsets --- golang/backtracking/subsets.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 golang/backtracking/subsets.go diff --git a/golang/backtracking/subsets.go b/golang/backtracking/subsets.go new file mode 100644 index 000000000..189e331e4 --- /dev/null +++ b/golang/backtracking/subsets.go @@ -0,0 +1,20 @@ +package main + +func subsets(nums []int) [][]int { + ans := make([][]int, 0) + curr := make([]int, 0) + var backtrack func(idx int) + backtrack = func(idx int) { + ans = append(ans, append([]int{}, curr...)) + if idx == len(nums) { + return + } + for i := idx; i < len(nums); i++ { + curr = append(curr, nums[i]) + backtrack(i + 1) + curr = curr[:len(curr)-1] + } + } + backtrack(0) + return ans +} From 2fa32b4f7543d544b022821d13af8f2779d5274f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=B6khan=20=C3=87a=C4=9Fr=C4=B1tekin?= Date: Fri, 15 Jul 2022 23:31:03 +0300 Subject: [PATCH 2/8] golang combination sum --- golang/backtracking/combinationSum.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 golang/backtracking/combinationSum.go diff --git a/golang/backtracking/combinationSum.go b/golang/backtracking/combinationSum.go new file mode 100644 index 000000000..26e47c64a --- /dev/null +++ b/golang/backtracking/combinationSum.go @@ -0,0 +1,24 @@ +package main + +func combinationSum(candidates []int, target int) [][]int { + ans := make([][]int, 0) + curr := make([]int, 0) + var backtrack func(idx int, currSum int, curr []int) + backtrack = func(idx int, currSum int, curr []int) { + if currSum == target { + ans = append(ans, append([]int{}, curr...)) + return + } + if currSum > target { + return + } + for i := idx; i < len(candidates); i++ { + curr = append(curr, candidates[i]) + backtrack(i, currSum+candidates[i], curr) + curr = curr[:len(curr)-1] + } + + } + backtrack(0, 0, curr) + return ans +} From 4f7d097933c318ee2118cbb4b312a7c0ddf94899 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=B6khan=20=C3=87a=C4=9Fr=C4=B1tekin?= Date: Fri, 15 Jul 2022 23:37:43 +0300 Subject: [PATCH 3/8] moving solutions under go folder --- {golang => go}/backtracking/combinationSum.go | 0 {golang => go}/backtracking/subsets.go | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename {golang => go}/backtracking/combinationSum.go (100%) rename {golang => go}/backtracking/subsets.go (100%) diff --git a/golang/backtracking/combinationSum.go b/go/backtracking/combinationSum.go similarity index 100% rename from golang/backtracking/combinationSum.go rename to go/backtracking/combinationSum.go diff --git a/golang/backtracking/subsets.go b/go/backtracking/subsets.go similarity index 100% rename from golang/backtracking/subsets.go rename to go/backtracking/subsets.go From bebf5635fa33a50737950d29f9f9bb0c7afb5346 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=B6khan=20=C3=87a=C4=9Fr=C4=B1tekin?= Date: Sat, 16 Jul 2022 22:03:02 +0300 Subject: [PATCH 4/8] permutations --- go/backtracking/permute.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 go/backtracking/permute.go diff --git a/go/backtracking/permute.go b/go/backtracking/permute.go new file mode 100644 index 000000000..f033de893 --- /dev/null +++ b/go/backtracking/permute.go @@ -0,0 +1,25 @@ +package main + +func permute(nums []int) [][]int { + n := len(nums) + ans := make([][]int, 0) + curr := make([]int, 0, n) + vis := make(map[int]int) + var backtrack func(idx int) + backtrack = func(idx int) { + if len(curr) == n { + ans = append(ans, append([]int{}, curr...)) + } + for i := 0; i < n; i++ { + if vis[i] == 0 { + vis[i]++ + curr = append(curr, nums[i]) + backtrack(i + 1) + curr = curr[:len(curr)-1] + vis[i]-- + } + } + } + backtrack(0) + return ans +} From cd8b031c8e293c5fcc6e18abfad706bf7b7c054d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=B6khan=20=C3=87a=C4=9Fr=C4=B1tekin?= Date: Mon, 18 Jul 2022 21:13:38 +0300 Subject: [PATCH 5/8] subsest with dups --- go/backtracking/subsetsWithDup.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 go/backtracking/subsetsWithDup.go diff --git a/go/backtracking/subsetsWithDup.go b/go/backtracking/subsetsWithDup.go new file mode 100644 index 000000000..9080c8da4 --- /dev/null +++ b/go/backtracking/subsetsWithDup.go @@ -0,0 +1,25 @@ +package main + +import "sort" + +func subsetsWithDup(nums []int) [][]int { + n := len(nums) + ans := make([][]int, 0, 1< idx && nums[i] == nums[i-1] { + continue + } + curr = append(curr, nums[i]) + //not backtrack(idx + 1)!! + backtrack(i + 1) + curr = curr[:len(curr)-1] + } + } + backtrack(0) + return ans +} From 3c8aa6cbee171b989d77471666a9388d586ea1ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=B6khan=20=C3=87a=C4=9Fr=C4=B1tekin?= Date: Tue, 19 Jul 2022 20:48:04 +0300 Subject: [PATCH 6/8] combinarion sum2 --- go/backtracking/combinationSum2.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 go/backtracking/combinationSum2.go diff --git a/go/backtracking/combinationSum2.go b/go/backtracking/combinationSum2.go new file mode 100644 index 000000000..ee3ed2f34 --- /dev/null +++ b/go/backtracking/combinationSum2.go @@ -0,0 +1,30 @@ +package main + +import "sort" + +func combinationSum2(candidates []int, target int) [][]int { + ans := make([][]int, 0) + curr := make([]int, 0) + sort.Ints(candidates) + var backtrack func(idx int, currSum int, curr []int) + backtrack = func(idx int, currSum int, curr []int) { + if currSum == target { + ans = append(ans, append([]int{}, curr...)) + return + } + if currSum > target { + return + } + for i := idx; i < len(candidates); i++ { + if i > idx && candidates[i] == candidates[i-1] { + continue + } + curr = append(curr, candidates[i]) + backtrack(i+1, currSum+candidates[i], curr) + curr = curr[:len(curr)-1] + } + + } + backtrack(0, 0, curr) + return ans +} From 975738489986ecd2362c2a445a0d4f014b1dc56f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=B6khan=20=C3=87a=C4=9Fr=C4=B1tekin?= Date: Sun, 24 Jul 2022 19:32:59 +0300 Subject: [PATCH 7/8] change directory --- go/{backtracking => }/combinationSum.go | 0 go/{backtracking => }/combinationSum2.go | 0 go/{backtracking => }/permute.go | 0 go/{backtracking => }/subsets.go | 0 go/{backtracking => }/subsetsWithDup.go | 0 5 files changed, 0 insertions(+), 0 deletions(-) rename go/{backtracking => }/combinationSum.go (100%) rename go/{backtracking => }/combinationSum2.go (100%) rename go/{backtracking => }/permute.go (100%) rename go/{backtracking => }/subsets.go (100%) rename go/{backtracking => }/subsetsWithDup.go (100%) diff --git a/go/backtracking/combinationSum.go b/go/combinationSum.go similarity index 100% rename from go/backtracking/combinationSum.go rename to go/combinationSum.go diff --git a/go/backtracking/combinationSum2.go b/go/combinationSum2.go similarity index 100% rename from go/backtracking/combinationSum2.go rename to go/combinationSum2.go diff --git a/go/backtracking/permute.go b/go/permute.go similarity index 100% rename from go/backtracking/permute.go rename to go/permute.go diff --git a/go/backtracking/subsets.go b/go/subsets.go similarity index 100% rename from go/backtracking/subsets.go rename to go/subsets.go diff --git a/go/backtracking/subsetsWithDup.go b/go/subsetsWithDup.go similarity index 100% rename from go/backtracking/subsetsWithDup.go rename to go/subsetsWithDup.go From be25a9755890efeaca239674468f7853672cf3bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=B6khan=20=C3=87a=C4=9Fr=C4=B1tekin?= Date: Sun, 24 Jul 2022 19:36:01 +0300 Subject: [PATCH 8/8] renaming --- go/{combinationSum.go => 39-Combination-Sum.go} | 0 go/{combinationSum2.go => 40-Combination-Sum-II.go} | 0 go/{permute.go => 46-Permutations.go} | 0 go/{subsets.go => 78-Subsets.go} | 0 go/{subsetsWithDup.go => 90-Subsets-II.go} | 0 5 files changed, 0 insertions(+), 0 deletions(-) rename go/{combinationSum.go => 39-Combination-Sum.go} (100%) rename go/{combinationSum2.go => 40-Combination-Sum-II.go} (100%) rename go/{permute.go => 46-Permutations.go} (100%) rename go/{subsets.go => 78-Subsets.go} (100%) rename go/{subsetsWithDup.go => 90-Subsets-II.go} (100%) diff --git a/go/combinationSum.go b/go/39-Combination-Sum.go similarity index 100% rename from go/combinationSum.go rename to go/39-Combination-Sum.go diff --git a/go/combinationSum2.go b/go/40-Combination-Sum-II.go similarity index 100% rename from go/combinationSum2.go rename to go/40-Combination-Sum-II.go diff --git a/go/permute.go b/go/46-Permutations.go similarity index 100% rename from go/permute.go rename to go/46-Permutations.go diff --git a/go/subsets.go b/go/78-Subsets.go similarity index 100% rename from go/subsets.go rename to go/78-Subsets.go diff --git a/go/subsetsWithDup.go b/go/90-Subsets-II.go similarity index 100% rename from go/subsetsWithDup.go rename to go/90-Subsets-II.go