This is my C implementation. When I run it in my laptop the answer is correct, but when I submit it, I get a Runtime Error. I used print to see at which point the code crashed and I see that it reaches the "return". Any ideas will be very helpful!!


  • -1
    S

    '''void merge(int* A1,int k1,int* A2,int k2,int* A){
    int l;
    int Index1,Index2;
    int k,Counter;

    Index1=0;
    Index2=0;
    k=k1+k2;
    Counter =k1+k2;
    while(Counter>0){
    if((Index1<k1)&&(Index2<k2)){
    if(A1[Index1]>A2[Index2]){
    A[k-Counter]=A1[Index1];

    		Index1++;		
    		Counter--;	
    		}			
    	else{
    		A[k-Counter]=A2[Index2];
    
    		Index2++;	
    		Counter--;
    	}
    }
    if(Index1==k1){
    	A[k-Counter] = A2[Index2];
    
    	Index2++;
    	Counter--;					
    }	
    
    if((Index2==k2)&&(Index1!=k1)){
    	A[k-Counter] = A1[Index1];
    
    	Index1++;
    	Counter--;					
    }
    

    }

    }

    int compare(int* A,int* B,int k){
    int result;
    int i;

    for(i=0;i<k;i++){
    if(A[i]>B[i]){return 0;}

    if(B[i]>A[i]){return 1;}
    

    }
    return 0;
    }

    int* Index_array(int* nums,int numsSize){
    int* B;
    int Max,i,j,flag;

    B = (int *) malloc(sizeof(int)*numsSize);
    for(i=0;i<numsSize;i++){
    Max=nums[i];
    B[i]=i;
    flag=0;
    for(j=i;j<numsSize;j++){
    if((nums[j]>Max)&&(flag==0)){
    B[i] = j;
    flag =1;
    }
    }

    }
    

    return B;
    }

    int* Create_Max1(int* nums,int numsSize,int k,int* Index_Array){
    int i,Index,Counter;
    int* A;

    A= malloc(sizeof(int)*k);
    Counter = k;
    Index=0;

    while(Counter>0){
    if((numsSize - Index_Array[Index] < Counter)||(Index == Index_Array[Index])){
    A[k-Counter] = nums[Index];
    Index++;
    Counter--;
    }
    else{
    Index = Index_Array[Index];
    }

    }

    return A;
    }

    int* maxNumber(int* nums1,int nums1Size,int* nums2,int nums2Size,int k,int* returnSize){
    int i,l,j,Index1,Index2;
    int* Index_Array1;
    int* Index_Array2;
    int* A1;
    int* A2;
    int* A3;
    int* A;
    int* Max;

    Index_Array1 = Index_array(nums1,nums1Size);
    Index_Array2 = Index_array(nums2,nums2Size);

    A= malloc(sizeof(int)*k);
    Max= malloc(sizeof(int)*k);

    if(nums1Size==0){
    Max = Create_Max1(nums2,nums2Size,k,Index_Array2);
    }

    if(nums2Size==0){
    Max = Create_Max1(nums1,nums1Size,k,Index_Array1);
    }

    if((nums1Size!=0)&&(nums2Size!=0)){
    for(l=0;l<=k;l++){
    Max[l]=-1;
    }
    if(k<=nums2Size){
    Max = Create_Max1(nums2,nums2Size,k,Index_Array2);
    }
    for(i=1;i<=k-1;i++){
    if((i<=nums1Size)&&(k-i<=nums2Size)){
    A1 = Create_Max1(nums1,nums1Size,i,Index_Array1);
    A2 = Create_Max1(nums2,nums2Size,k-i,Index_Array2);

    merge(A1,i,A2,k-i,A);
    
    printf("%d\n",i);
    }	
    if(compare(Max,A,k)==1){
    	Max =A;
    	}
    

    }
    if(k<=nums1Size){
    A3 = Create_Max1(nums1,nums1Size,k,Index_Array1);
    printf("%d\n",i);
    if(compare(Max,A3,k)==1){
    Max = A3;
    }
    }
    }
    printf("%d\n",i);
    *returnSize=k;
    printf("%d\n",i);
    free(A);

    printf("MAX\n");
    return Max;
    }


  • 0

    Nobody is gonna look at the mess you created.
    If you want to ask questions, please format them well so that others will at least take a look.
    No offense.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.