Skip to content

Commit e0422ba

Browse files
committed
feat(solutions): add combinations
1 parent 5699081 commit e0422ba

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
| # | Problem | Solution | Difficulty | Single Repetition Duration | LeetCode Run Time |
2727
| ---: | :----- | :--------: | :----------: | ----------: | ----------: |
28+
|77|[Combinations][Solutions-77]|[WindomZ][Solutions-77-golang]|Medium|[336 ns/op/6 test cases][Solutions-77-Test]|246 ms|
2829
|75|[Sort Colors][Solutions-75]|[WindomZ][Solutions-75-golang]|Medium|[13.4 ns/op/6 test cases][Solutions-75-Test]|3 ms|
2930
|74|[Search a 2D Matrix][Solutions-74]|[WindomZ][Solutions-74-golang]|Medium|[32.5 ns/op/6 test cases][Solutions-74-Test]|16 ms|
3031
|73|[Set Matrix Zeroes][Solutions-73]|[WindomZ][Solutions-73-golang]|Medium|[22.0 ns/op/6 test cases][Solutions-73-Test]|42 ms|
@@ -128,6 +129,9 @@ Welcome to report bugs, suggest ideas and discuss on [issues page](https://githu
128129
### Support
129130
If you like it then you can put a :star:Star on it.
130131

132+
[Solutions-77]:https://leetcode.com/problems/combinations/
133+
[Solutions-77-golang]:solutions/combinations/combinations.go
134+
[Solutions-77-Test]:solutions/combinations/combinations_test.go#L32
131135
[Solutions-75]:https://leetcode.com/problems/sort-colors/
132136
[Solutions-75-golang]:solutions/sort_colors/sortcolors.go
133137
[Solutions-75-Test]:solutions/sort_colors/sortcolors_test.go#L35

solutions/combinations/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# 77. Combinations
2+
3+
[Description](https://leetcode.com/problems/combinations/description/) |
4+
[Discuss](https://leetcode.com/problems/combinations/discuss/) |
5+
[Solution](https://leetcode.com/problems/combinations/solution/)
6+
7+
## Description
8+
9+
Given two integers _n_ and _k_, return all possible combinations of _k_ numbers out of 1 ... _n_.
10+
11+
For example,
12+
13+
If _n_ = 4 and _k_ = 2, a solution is:
14+
15+
```
16+
[
17+
[2,4],
18+
[3,4],
19+
[2,3],
20+
[1,2],
21+
[1,3],
22+
[1,4],
23+
]
24+
```
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package combinations
2+
3+
func combine(n int, k int) [][]int {
4+
if k <= 0 {
5+
return [][]int{}
6+
}
7+
rs := make([][]int, 0, k)
8+
combineNext(&rs, make([]int, 0, k), 1, n, k)
9+
return rs
10+
}
11+
12+
func combineNext(rs *[][]int, r []int, s, n, k int) {
13+
if k == 0 {
14+
t := make([]int, len(r))
15+
for k, v := range r {
16+
t[k] = v
17+
}
18+
*rs = append(*rs, t)
19+
return
20+
}
21+
for i := s; i <= n; i++ {
22+
r = append(r, i)
23+
combineNext(rs, r, i+1, n, k-1)
24+
r = r[:len(r)-1]
25+
}
26+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package combinations
2+
3+
import (
4+
"testing"
5+
6+
"github.com/WindomZ/testify/assert"
7+
)
8+
9+
func Test_combine(t *testing.T) {
10+
assert.Equal(t, [][]int{}, combine(1, 0))
11+
assert.Equal(t, [][]int{
12+
{1},
13+
}, combine(1, 1))
14+
assert.Equal(t, [][]int{
15+
{1, 2},
16+
}, combine(2, 2))
17+
assert.Equal(t, [][]int{
18+
{1, 2},
19+
{1, 3},
20+
{2, 3},
21+
}, combine(3, 2))
22+
assert.Equal(t, [][]int{
23+
{1, 2},
24+
{1, 3},
25+
{1, 4},
26+
{2, 3},
27+
{2, 4},
28+
{3, 4},
29+
}, combine(4, 2))
30+
}
31+
32+
func Benchmark_combine(b *testing.B) {
33+
b.StopTimer()
34+
b.ReportAllocs()
35+
b.StartTimer()
36+
b.RunParallel(func(pb *testing.PB) {
37+
for pb.Next() {
38+
combine(1, 0)
39+
combine(1, 1)
40+
combine(2, 2)
41+
combine(3, 2)
42+
combine(4, 2)
43+
combine(4, 3)
44+
}
45+
})
46+
}

0 commit comments

Comments
 (0)