Skip to content

Commit

Permalink
Merge b241e3c into fe69e29
Browse files Browse the repository at this point in the history
  • Loading branch information
vaskoz committed Feb 1, 2020
2 parents fe69e29 + b241e3c commit 64fb335
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ problems from
* [Day 315](https://github.com/vaskoz/dailycodingproblem-go/issues/642)
* [Day 316](https://github.com/vaskoz/dailycodingproblem-go/issues/644)
* [Day 317](https://github.com/vaskoz/dailycodingproblem-go/issues/645)
* [Day 318](https://github.com/vaskoz/dailycodingproblem-go/issues/648)
* [Day 319](https://github.com/vaskoz/dailycodingproblem-go/issues/649)
* [Day 320](https://github.com/vaskoz/dailycodingproblem-go/issues/650)
* [Day 321](https://github.com/vaskoz/dailycodingproblem-go/issues/651)
Expand Down
53 changes: 53 additions & 0 deletions day318/problem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package day318

// NumberOfValidPlayListsRec is a recursive solution.
func NumberOfValidPlayListsRec(required, downloaded, buffer int) int {
if buffer >= downloaded {
return 0
}

if required == 0 {
return 1
}

downloadParam := downloaded

if buffer != 0 {
downloadParam--
}

return downloaded * NumberOfValidPlayListsRec(required-1, downloadParam, max(0, buffer-1))
}

func max(a, b int) int {
if a > b {
return a
}

return b
}

// NumberOfValidPlayListsIterative is an iterative solution.
func NumberOfValidPlayListsIterative(required, downloaded, buffer int) int {
if buffer >= downloaded {
return 0
}

total := 1
available := downloaded

for i := 0; i <= buffer; i++ {
total *= available
available--
}

if available < downloaded {
available++
}

for i := buffer + 1; i < required; i++ {
total *= available
}

return total
}
54 changes: 54 additions & 0 deletions day318/problem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package day318

import "testing"

// nolint
var testcases = []struct {
N, M, B int
expected int
}{
{4, 2, 1, 2},
{4, 2, 0, 16},
{4, 2, 2, 0},
{9, 2, 1, 2},
{9, 2, 0, 512},
{9, 3, 2, 6},
{9, 3, 1, 768},
{9, 3, 0, 19683},
}

func TestNumberOfValidPlayListsRec(t *testing.T) {
t.Parallel()

for _, tc := range testcases {
if result := NumberOfValidPlayListsRec(tc.N, tc.M, tc.B); result != tc.expected {
t.Errorf("Expected %v, got %v", tc.expected, result)
}
}
}

func BenchmarkNumberOfValidPlayListsRec(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, tc := range testcases {
NumberOfValidPlayListsRec(tc.N, tc.M, tc.B)
}
}
}

func TestNumberOfValidPlayListsIterative(t *testing.T) {
t.Parallel()

for _, tc := range testcases {
if result := NumberOfValidPlayListsIterative(tc.N, tc.M, tc.B); result != tc.expected {
t.Errorf("Expected %v, got %v", tc.expected, result)
}
}
}

func BenchmarkNumberOfValidPlayListsIterative(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, tc := range testcases {
NumberOfValidPlayListsIterative(tc.N, tc.M, tc.B)
}
}
}

0 comments on commit 64fb335

Please sign in to comment.