【发布时间】:2020-12-18 17:13:39
【问题描述】:
我正在做 geeksforgeeks 的练习题。而且我无法解决问题并获得所需的输出。
//I am finding the max path using the merge sort algo, where T(n)=O(m+n)
int MaxPath(int a[],int b[],int n,int m){
int i, j, result=0, sum1=0, sum2=0; // sum1 and sum2 store the sums of 1st and 2nd arrays respectively
while(i<n && j<m){
if(a[i]<b[j]){
sum1+=a[i++];
}
else if(a[i]>b[j]){
sum2+=b[j++];
}
else{
result+=Max(sum1,sum2);
sum1=0;
sum2=0;
int temp=i;
while(i<n && a[i]==b[j]){
sum1+=a[i++];
}
while(j<m && a[temp]==b[j]){
sum2+=b[j++];
}
result+=Max(sum1,sum2);
sum1=0;
sum2=0;
}
}
while(i<n){
sum1+=a[i++];
}
while(j<m){
sum2+=b[j++];
}
result+=Max(sum1,sum2);
return result;
}
【问题讨论】:
-
Max()是如何定义的? -
@ryyker 我在我的程序中为此编写了一个函数。
-
好的 - 在这种情况下,如果未初始化的变量是唯一的问题,那么这不是问题。但如果存在更多问题,最好提供minimal reproducible example,其中包括
Max()的定义,以帮助解决问题的人。 :) -
好的,谢谢@ryyker 我下次发布代码时一定会记住这一点。
标签: arrays c data-structures mergesort