我试图实现这个问题的算法:Need idea for solving this algorithm puzzle,但是我遗漏了一些导致我的代码进入无限循环的边缘情况我可以通过做一些表面的改变来修复它,但这表明我不理解算法。
有人能帮我吗,我错过了什么?
#include <stdio.h>
#define max(a, b) (((a)>(b))?(a):(b));
int get_max(int *a, int i, int size)
{
if (i >= size)
return 0;
return max(a[i], get_max(a, i+1, size));
}
int get_sum(int *a, int i, int size)
{
if (i >= size)
return 0;
return a[i] + get_sum(a, i+1, size);
}
int get_partition(int *a, int size, int bound) {
int running_sum = 0;
int partitions = 0, i;
for (i=0;i<size;i++) {
if (a[i] + running_sum <= bound) {
running_sum += a[i];
} else {
running_sum = 0;
running_sum += a[i];
partitions++;
}
}
return partitions;
}
int foo(int *a, int size, int k)
{
int lower = get_max(a, 0, size);
int higher = get_sum(a, 0, size);
int partition;
while (lower < higher) {
int bound = (lower + (higher))/2;
partition = get_partition(a, size, bound);
printf("partition %d bound %d lower %d higher %d\n", partition, bound, lower, higher);
if (partition >= k)
lower = bound;
else
higher = bound;
}
return partition;
}
#define SIZE(a) sizeof(a)/sizeof(a[0])
int main(void) {
int a[] = {2, 3, 4, 5, 6};
printf("%d\n", foo(a, SIZE(a), 3));
return 0;
}
输出:
partition 1 bound 13 lower 6 higher 20
partition 2 bound 9 lower 6 higher 13
partition 3 bound 7 lower 6 higher 9
partition 3 bound 8 lower 7 higher 9
partition 3 bound 8 lower 8 higher 9
...last line keeps repeating.
最佳答案
你有几个错误:
在二进制搜索期间,while测试应该是while (lower+1 < higher) {
而不是while (lower < higher) {
。当lower = 8
,higher = 9
时,您正在进入无限循环在这个阶段,您的bound
将是(lower+higher)/2=8
并且您将更新lower = bound
,这不会改变任何内容。
在foo
的末尾,您应该返回higher
(而不是分区),因为二进制搜索不变量是,对于具有bound <= lower
的数组,您可以将其分区为多个k
部分,而对于bound >= higher
的数组,您可以将其分区为k
或更少。
你对get_partition
的计算是错误的。由于只在溢出partitions
时更新running_sum
,因此不考虑最后一个分区组在for循环之后,您应该拥有以下语句:
if (running_sum > 0)
partitions++;
总而言之:
#include <stdio.h>
#define max(a, b) (((a)>(b))?(a):(b));
int get_max(int *a, int i, int size)
{
if (i >= size)
return 0;
return max(a[i], get_max(a, i+1, size));
}
int get_sum(int *a, int i, int size)
{
if (i >= size)
return 0;
return a[i] + get_sum(a, i+1, size);
}
int get_partition(int *a, int size, int bound) {
int running_sum = 0;
int partitions = 0, i;
for (i=0;i<size;i++) {
if (a[i] + running_sum <= bound) {
running_sum += a[i];
} else {
running_sum = 0;
running_sum += a[i];
partitions++;
}
}
if (running_sum > 0)
partitions++;
return partitions;
}
int foo(int *a, int size, int k)
{
int lower = get_max(a, 0, size);
int higher = get_sum(a, 0, size);
int partition;
while (lower+1 < higher) {
int bound = (lower + (higher))/2;
partition = get_partition(a, size, bound);
printf("partition %d bound %d lower %d higher %d\n", partition, bound, lower, higher);
if (partition > k)
lower = bound;
else
higher = bound;
}
printf("partition %dlower %d higher %d\n", partition, lower, higher);
return higher;
}
#define SIZE(a) sizeof(a)/sizeof(a[0])
int main(void) {
int a[] = {2, 3, 4, 5, 6};
printf("%d\n", foo(a, SIZE(a), 3));
return 0;
}
关于algorithm - 我可以将数组划分为K个大小吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33068405/