-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #505 from vaskoz/day243
Day243
- Loading branch information
Showing
3 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package day243 | ||
|
||
// MinimizePartitionMaximumSum returns the smallest max sum of k-partitions. | ||
func MinimizePartitionMaximumSum(nums []int, k int) int { | ||
if k > len(nums) { | ||
return 0 | ||
} | ||
return minimizePartitionMaximumSum(nums, k) | ||
} | ||
|
||
func minimizePartitionMaximumSum(nums []int, k int) int { | ||
if k == 1 { | ||
var sum int | ||
for _, v := range nums { | ||
sum += v | ||
} | ||
return sum | ||
} | ||
minimumMax := int(^uint(0) >> 1) | ||
for i := 1; i <= len(nums)-k+1; i++ { | ||
var partitionSumMax int | ||
for j := 0; j < i; j++ { | ||
partitionSumMax += nums[j] | ||
} | ||
if minMax := minimizePartitionMaximumSum(nums[i:], k-1); minMax > partitionSumMax { | ||
partitionSumMax = minMax | ||
} | ||
if partitionSumMax < minimumMax { | ||
minimumMax = partitionSumMax | ||
} | ||
} | ||
return minimumMax | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package day243 | ||
|
||
import "testing" | ||
|
||
var testcases = []struct { | ||
nums []int | ||
k int | ||
sum int | ||
}{ | ||
{[]int{5, 1, 2, 7, 3, 4}, 3, 8}, | ||
{[]int{3, 1, 1}, 3, 3}, | ||
{[]int{10, 1, 1}, 4, 0}, | ||
{[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 6, 11}, | ||
{[]int{5, 1, 2, 7, 3, 4}, 2, 14}, | ||
{[]int{5, 1, 2, 7, 3, 4}, 1, 22}, | ||
{[]int{89, 72, 92, 51, 34, 26, 51, 17, 32, 99}, 10, 99}, | ||
{[]int{89, 72, 92, 51, 34, 26, 51, 17, 32, 99}, 8, 99}, | ||
{[]int{89, 72, 92, 51, 34, 26, 51, 17, 32, 99}, 7, 99}, | ||
{[]int{89, 72, 92, 51, 34, 26, 51, 17, 32, 99}, 6, 111}, | ||
{[]int{89, 72, 92, 51, 34, 26, 51, 17, 32, 99}, 5, 143}, | ||
{[]int{89, 72, 92, 51, 34, 26, 51, 17, 32, 99}, 4, 161}, | ||
{[]int{89, 72, 92, 51, 34, 26, 51, 17, 32, 99}, 3, 203}, | ||
{[]int{89, 72, 92, 51, 34, 26, 51, 17, 32, 99}, 2, 304}, | ||
} | ||
|
||
func TestMinimizePartitionMaximumSum(t *testing.T) { | ||
t.Parallel() | ||
for _, tc := range testcases { | ||
if sum := MinimizePartitionMaximumSum(tc.nums, tc.k); sum != tc.sum { | ||
t.Errorf("Expected %v, got %v", tc.sum, sum) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkMinimizePartitionMaximumSum(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
MinimizePartitionMaximumSum(tc.nums, tc.k) | ||
} | ||
} | ||
} |