baolei1234
BAN USERoh i see where i am wrong. I will find out a way to correct this one.
Thanks very much.
Will this work?
AA=sort(A);
BB=sort(B);
pA=0;
pB=0;
count=1;
while(1)
{
....x=AA(pA)+BB(pB+1);
....y=AA(pA+1)+BB(pB);
....if(x<=y)
....{
........pB++;
....}
....if(y<x)
....{
........pA++;
....}
....count++;
....if(count==k)
........return AA[pA]+BB[pB]
}
should be O(nlogn+k)
please correct me if there is something wrong or i misunderstood the question
Say N=20, the 4 segments are 2,6,2,10
So we try all the following:
2 vs 18(6+2+10), 8(2+6) vs 12(2+10), 10(2+6+2)+10
see which is the lowest cost.
Then return 20 + the lowest cost
when we deal with 18(6+2+10), we can use the same rule
(try 6vs12 and 8vs10)
sorry for the duplicate reply and failure to include white space..
it's my first time to use this forum.
what is the best way to post code, so that the indents can be displayed? Thanks.
Repkylecfarrell, Personnel at Bocada
Property custodian with a reputed organization and help in keeping accurate check over the supplies and inventory of materials and ...
how about this way:
- baolei1234 October 15, 2010// AA and BB are sorted
for(int x=0;x<=k-1;x++)
{
..for(int y=0;y<=k-1-x;y++)
....C.add(AA[x]+BB[y]);
}
CC=sort(C);
return CC[k];