A simple C solution by quicksort [Accepted]


  • 0
    B
    int temp_1;
    int partition(int* array,int low,int high)
    {
        temp_1=array[low];
        while(low<high)
        {
            while(low<high&&array[high]>=temp_1)
                high--;
            if(low<high) 
            {
                array[low]=array[high];
                low++;
            }
            while(low<high&&array[low]<temp_1)
                low++;
            if(low<high)
            {
                array[high]=array[low];
                high--;
            }
        }
        array[low]=temp_1;
        return low;
    }
    void quick_sort(int* array,int s,int t)
    {   
        int i=0;
        if(s<t)
        {
            i=partition(array,s,t);
            quick_sort(array,s,i-1);
            quick_sort(array,i+1,t);
        }
    }
    int findContentChildren(int* g, int gSize, int* s, int sSize) {
        int temp=0;
        temp_1=0;
        quick_sort(g,0,gSize-1);
        quick_sort(s,0,sSize-1);   
        int result=0;
        for(int i=0;i<gSize;i++)
        {
            for(int j=temp;j<sSize;j++)
            {
                if(g[i]<=s[j])
                {
                    result++;
                    temp=j+1;
                    break;
                }
            }
        }
        return result;
    }
        
    

Log in to reply
 

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