Help, code works fine on the local machine, throws run time error while submission


  • 0
    S
    class Solution {
    public:
        int findMin(vector<ListNode*> & lists, vector<bool> isValid) {
            int smallest = INT_MAX;
            int smallestIndex = -1;
            for (int i = 0 ; i < lists.size() ; i++) {
                if (isValid[i]==false) continue;
                if (lists[i]->val < smallest) {
                    smallest = lists[i]->val;
                    smallestIndex = i; 
                }
            }
            return smallestIndex;
        }
        ListNode* mergeKLists(vector<ListNode*>& lists) {
            int n = lists.size();
            int boolSum = n;
            ListNode *head;
            ListNode *mainCurrent;
            vector<bool> isValid(n,true);
            for (int i = 0 ; i < n ; i++) {
                if (lists[i]==NULL) {isValid[i]=false; boolSum--;} 
            }
            int index = findMin(lists,isValid);
            head = lists[index];
            lists[index] = lists[index]->next;
            mainCurrent = head;
            mainCurrent -> next = NULL;        
            if (lists[index]==NULL) {isValid[index] = false; boolSum--;}; 
            while (boolSum!=0) {
                index = findMin(lists,isValid);
                mainCurrent -> next = lists[index];
                mainCurrent = mainCurrent -> next;
                lists[index] = lists[index]->next;
                if (lists[index]==NULL) {isValid[index] = false; boolSum--;};
            }
            return head;
        }
    };
    

    This code is working fine on my machine, but it is throwing run time error while submission, please help!


  • 0
    C

    Please check findMin return value, but I don't think you need isValid and boolSum variables, here's a link to another same solution https://discuss.leetcode.com/topic/95688/a-simple-c-solution


Log in to reply
 

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