我可以将数组分区为 K 大小吗?
Can I partition the array in K sizes?
我正在尝试根据这个问题实现算法: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
的末尾你应该 return higher
(不是分区)因为你的二分搜索不变量是为了让 bound <= lower
你可以将数组分区在超过 k
个部分,对于 bound >= higher
个部分,您可以将其分区为 k
个或更少。
你计算的get_partition
是错误的。您没有考虑最后一个分区组,因为您只在溢出 running_sum
时更新 partitions
。在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;
}
我正在尝试根据这个问题实现算法: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
的末尾你应该 returnhigher
(不是分区)因为你的二分搜索不变量是为了让bound <= lower
你可以将数组分区在超过k
个部分,对于bound >= higher
个部分,您可以将其分区为k
个或更少。 你计算的
get_partition
是错误的。您没有考虑最后一个分区组,因为您只在溢出running_sum
时更新partitions
。在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;
}