Skip to content

Commit

Permalink
Merge 31feafd into 69dde06
Browse files Browse the repository at this point in the history
  • Loading branch information
vaskoz committed May 30, 2019
2 parents 69dde06 + 31feafd commit fcc2355
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,4 @@ problems from
* [Day 278](https://github.com/vaskoz/dailycodingproblem-go/issues/565)
* [Day 279](https://github.com/vaskoz/dailycodingproblem-go/issues/566)
* [Day 280](https://github.com/vaskoz/dailycodingproblem-go/issues/568)
* [Day 281](https://github.com/vaskoz/dailycodingproblem-go/issues/572)
22 changes: 22 additions & 0 deletions day281/problem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package day281

// FewestBricksCut return the fewest number of
// bricks that must be cut to create a vertical line.
func FewestBricksCut(bricks [][]int) int {
cumm := make(map[int]int)
for _, row := range bricks {
last := 0
for _, brick := range row {
last += brick
cumm[last]++
}
delete(cumm, last)
}
var most int
for _, count := range cumm {
if count > most {
most = count
}
}
return len(bricks) - most
}
38 changes: 38 additions & 0 deletions day281/problem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package day281

import "testing"

// nolint
var testcases = []struct {
bricks [][]int
fewest int
}{
{
[][]int{
{3, 5, 1, 1},
{2, 3, 3, 2},
{5, 5},
{4, 4, 2},
{1, 3, 3, 3},
{1, 1, 6, 1, 1},
},
2,
},
}

func TestFewestBricksCut(t *testing.T) {
t.Parallel()
for _, tc := range testcases {
if result := FewestBricksCut(tc.bricks); result != tc.fewest {
t.Errorf("Expected %v, got %v", tc.fewest, result)
}
}
}

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

0 comments on commit fcc2355

Please sign in to comment.