My c solution 338ms any fast ones?


  • -1
    E
    bool canFinish(int numCourses, int** prerequisites, int prerequisitesRowSize, int prerequisitesColSize) {
        int takenCourse=0;
        int i=0,j=0,k=0,m=0;
        int exist=0;
        int count=0;
        int temp=0;
        int loop=0;
        int position_num=0;
        if(numCourses<0||prerequisitesRowSize<0||prerequisitesColSize<0) return 0;
        if(numCourses==0) return 0;
        if(numCourses>=1&&prerequisitesRowSize==0&&prerequisitesColSize==0) return 1;
        int *num=calloc(numCourses,sizeof(int));
        int *position=calloc(prerequisitesRowSize,sizeof(int));
        for(i=0;i<prerequisitesRowSize;i++){
            count=0;
            num[count]=prerequisites[i][0];
            count++;
            num[count]=prerequisites[i][1];
            position_num=0;
            position[position_num]=i;
            position_num++;
            for(j=0;j<prerequisitesRowSize;j++){
                if(prerequisites[j][0]==num[count]){
                exist=0;
                for(m=0;m<position_num;m++){
                    if(position[m]==j)
                    {
                        exist=1;
                    }
                    if(exist) break;
                }
                if(!exist){
                   
                        for(k=0;k<=count;k++){
                            if(num[k]==prerequisites[j][1]){
                                return 0;
                            }
                        }
                         count++;
                         num[count]=prerequisites[j][1];
                         position[position_num]=j;
                         position_num++;
                         j=0;
                         if(position_num==prerequisitesRowSize) return 1;
                    }
                }
            }
            
        }
        return 1;
    }

  • -1
    J

    My 249 ms
    bool canFinish(int numCourses, int** prerequisites, int prerequisitesRowSize, int prerequisitesColSize) {
    int chudu=(int)calloc(numCourses,sizeof(int));
    int flag=(int)calloc(prerequisitesRowSize,sizeof(int));
    int i=0;
    int k=0;
    while(i<prerequisitesRowSize)
    {
    chudu[prerequisites[i][0]]++;
    i++;
    }
    while(k==0)
    {
    k=1;
    for(i=0;i<prerequisitesRowSize;i++)
    {
    if(flag[i]==0)
    {
    if(chudu[prerequisites[i][1]]==0)
    {
    flag[i]=1;
    chudu[prerequisites[i][0]]--;
    k=0;
    }
    }
    }
    }
    for(i=0;i<prerequisitesRowSize;i++)
    {
    if(flag[i]==0)
    return false;
    }
    return true;
    }


Log in to reply
 

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