Skip to content

Commit ab7c074

Browse files
committed
added problem 2305
1 parent 4689bc8 commit ab7c074

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package problem2305
2+
3+
import "math"
4+
5+
/*
6+
You are given an integer array cookies, where cookies[i] denotes the number of cookies in the ith bag.
7+
You are also given an integer k that denotes the number of children to distribute all the bags of cookies to.
8+
All the cookies in the same bag must go to the same child and cannot be split up.
9+
The unfairness of a distribution is defined as the maximum total cookies obtained by a single child in the distribution.
10+
Return the minimum unfairness of all distributions.
11+
*/
12+
13+
func distributeCookies(cookies []int, k int) int {
14+
return calcMinUnfairness(cookies, make([]int, k))
15+
}
16+
17+
// calcMinUnfairness calculates the minimum unfairness with
18+
// cookies as cookie bags, dist as the number of cookies each child got
19+
func calcMinUnfairness(cookies, dist []int) int {
20+
if len(cookies) == 0 {
21+
// If we ran out of cookies, find the unfairness value
22+
res := 0
23+
// Unfairness the the max cookies a child got
24+
for _, v := range dist {
25+
res = max(res, v)
26+
}
27+
return res
28+
}
29+
// If we still have cookies to give
30+
var res = math.MaxInt
31+
// Try giving the first bag to each child
32+
for i := range dist {
33+
// Give cookies
34+
dist[i] += cookies[0]
35+
// Calculate unfairness after giving the cookies
36+
res = min(res, calcMinUnfairness(cookies[1:], dist))
37+
// Take back the cookies to not interfere with the next calc
38+
dist[i] -= cookies[0]
39+
}
40+
return res
41+
}
42+
43+
func max(a, b int) int {
44+
if a > b {
45+
return a
46+
}
47+
return b
48+
}
49+
50+
func min(a, b int) int {
51+
if a < b {
52+
return a
53+
}
54+
return b
55+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package problem2305
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
type TestCase struct {
11+
Input []int
12+
K int
13+
Expected int
14+
}
15+
16+
var TestCases = []TestCase{
17+
{[]int{8, 15, 10, 20, 8}, 2, 31},
18+
{[]int{6, 1, 3, 2, 2, 4, 1, 2}, 3, 7},
19+
}
20+
21+
func TestFairCookieDistribution(t *testing.T) {
22+
assert := assert.New(t)
23+
24+
for _, tc := range TestCases {
25+
want := tc.Expected
26+
got := distributeCookies(tc.Input, tc.K)
27+
assert.Equal(want, got, fmt.Sprintf("%+v", tc))
28+
}
29+
}
30+
31+
func BenchmarkFairCookieDistribution(b *testing.B) {
32+
for i := 0; i < b.N; i++ {
33+
for _, tc := range TestCases {
34+
distributeCookies(tc.Input, tc.K)
35+
}
36+
}
37+
}

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,7 @@ Each problem is in it's own directory, with test files. There are helper package
505505
| 2279 | [Maximum Bags With Full Capacity of Rocks](https://leetcode.com/problems/maximum-bags-with-full-capacity-of-rocks) | [My Solution](./problems/problem2279) ||
506506
| 2284 | [Sender With Largest Word Count](https://leetcode.com/problems/sender-with-largest-word-count) | [My Solution](./problems/problem2284) ||
507507
| 2300 | [Successful Pairs of Spells and Potions](https://leetcode.com/problems/successful-pairs-of-spells-and-potions) | [My Solution](./problems/problem2300) ||
508+
| 2305 | [Fair Distribution of Cookies](https://leetcode.com/problems/fair-distribution-of-cookies) | [My Solution](./problems/problem2305) ||
508509
| 2306 | [Naming a Company](https://leetcode.com/problems/naming-a-company) | [My Solution](./problems/problem2306) ||
509510
| 2316 | [Count Unreachable Pairs of Nodes in an Undirected Graph](https://leetcode.com/problems/count-unreachable-pairs-of-nodes-in-an-undirected-graph) | [My Solution](./problems/problem2316) ||
510511
| 2326 | [Spiral Matrix IV](https://leetcode.com/problems/spiral-matrix-iv) | [My Solution](./problems/problem2326) ||

0 commit comments

Comments
 (0)