Skip to content

Commit 3681a39

Browse files
committed
problem12
1 parent 257088b commit 3681a39

File tree

5 files changed

+50
-4
lines changed

5 files changed

+50
-4
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Solutions for [Daily Coding Problem](https://www.dailycodingproblem.com/ "Daily
99

1010
Run `make test` to test the solutions.
1111
```bash
12-
make test NO=1 # test problem1
12+
make test NO=001 # test problem1
1313
make test # test all
1414
```
1515

problem011/problem011.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,22 @@ func (node *trie) Find(query string) []string {
4242
var result []string
4343
if len(queryLeft) == 0 {
4444
prefix := prefixBuilder.String()
45-
for _, substring := range cursor.getStrings() {
45+
for _, substring := range cursor.subStrings() {
4646
result = append(result, fmt.Sprintf("%s%s", prefix, substring))
4747
}
4848
}
4949
return result
5050
}
5151

52-
func (node *trie) getStrings() []string {
52+
func (node *trie) subStrings() []string {
5353
if len(*node) == 0 {
5454
return []string{""}
5555
}
5656
childrenChannel := make(chan []string, len(*node))
5757
for keyString, subNode := range *node {
5858
go func(prefix string, node *trie) {
5959
var subStrings []string
60-
for _, subSubString := range node.getStrings() {
60+
for _, subSubString := range node.subStrings() {
6161
subStrings = append(subStrings, fmt.Sprintf("%s%s", prefix, subSubString))
6262
}
6363
childrenChannel <- subStrings

problem012/README.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
## Daily Coding Problem: Problem #12 [Hard]
2+
3+
Good morning! Here's your coding interview problem for today.
4+
5+
This problem was asked by Amazon.
6+
7+
There exists a staircase with N steps, and you can climb up either 1 or 2 steps at a time. Given N, write a function that returns the number of unique ways you can climb the staircase. The order of the steps matters.
8+
9+
For example, if N is 4, then there are 5 unique ways:
10+
11+
---
12+
* 1, 1, 1, 1
13+
* 2, 1, 1
14+
* 1, 2, 1
15+
* 1, 1, 2
16+
* 2, 2
17+
---
18+
19+
What if, instead of being able to climb 1 or 2 steps at a time, you could climb any number from a set of positive integers X? For example, if X = {1, 3, 5}, you could climb 1, 3, or 5 steps at a time.

problem012/problem012.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package problem012
2+
3+
func Run(maxStairCount int, stepSizes []int) int {
4+
waysToClimb := make([]int, maxStairCount+1, maxStairCount+1)
5+
waysToClimb[0] = 1
6+
for stairCount := 1; stairCount <= maxStairCount; stairCount++ {
7+
for _, stepSize := range stepSizes {
8+
if stepSize <= stairCount {
9+
waysToClimb[stairCount] += waysToClimb[stairCount-stepSize]
10+
}
11+
}
12+
}
13+
return waysToClimb[maxStairCount]
14+
}

problem012/problem012_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package problem012
2+
3+
import "testing"
4+
5+
func TestRun(t *testing.T) {
6+
if Run(4, []int{1, 2}) != 5 {
7+
t.FailNow()
8+
}
9+
}
10+
11+
func TestRun2(t *testing.T) {
12+
t.Log(Run(20, []int{1, 3, 5}))
13+
}

0 commit comments

Comments
 (0)