-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commit. Have yet to add code to install malloc hooks to calculate the heap memory used by user's and reference solutions.
- Loading branch information
Showing
5 changed files
with
498 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#pragma once | ||
|
||
struct ListNode { | ||
int val; | ||
ListNode *next; | ||
bool visited; | ||
ListNode() : val(0), next(nullptr), visited(false) {} | ||
ListNode(int x) : val(x), next(nullptr), visited(false) {} | ||
ListNode(int x, ListNode *next) : val(x), next(next), visited(false) {} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
|
||
void adjustChild(int32_t begin, int32_t childIndex, ListNode** array, int32_t& numEntries) { | ||
if (childIndex < 1) { | ||
return; | ||
} | ||
int32_t parentIndex = (childIndex - 1) / 2; | ||
while ((begin + childIndex) != begin) { | ||
if (array[begin + childIndex]->val < array[begin + parentIndex]->val) { | ||
ListNode* temp = array[begin + childIndex]; | ||
array[begin + childIndex] = array[begin + parentIndex]; | ||
array[begin + parentIndex] = temp; | ||
childIndex = parentIndex; | ||
parentIndex = (childIndex - 1) / 2; | ||
} else { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
void adjustRoot(int32_t begin, ListNode** array, int32_t& numEntries) { | ||
int32_t maxIndex = numEntries-1; | ||
if (maxIndex == 0) { | ||
return; | ||
} | ||
int32_t parentIndex = 0; | ||
|
||
while (parentIndex < maxIndex) { | ||
int32_t leftIndex = (parentIndex * 2) + 1; | ||
if (leftIndex > maxIndex) { | ||
return; | ||
} | ||
int32_t rightIndex = (parentIndex * 2) + 2; | ||
rightIndex = rightIndex > maxIndex ? leftIndex : rightIndex; | ||
bool swapped = false; | ||
if (rightIndex != leftIndex) { | ||
if (array[begin+rightIndex]->val < array[begin+leftIndex]->val) { | ||
if (array[begin+rightIndex]->val < array[begin+parentIndex]->val) { | ||
ListNode* temp = array[begin+rightIndex]; | ||
array[begin+rightIndex] = array[begin+parentIndex]; | ||
array[begin+parentIndex] = temp; | ||
parentIndex = rightIndex; | ||
swapped = true; | ||
} | ||
} else { | ||
if (array[begin+leftIndex]->val < array[begin+parentIndex]->val) { | ||
ListNode* temp = array[begin+leftIndex]; | ||
array[begin+leftIndex] = array[begin+parentIndex]; | ||
array[begin+parentIndex] = temp; | ||
parentIndex = leftIndex; | ||
swapped = true; | ||
} | ||
} | ||
} else { | ||
if (array[begin+leftIndex]->val < array[begin+parentIndex]->val) { | ||
ListNode* temp = array[begin+leftIndex]; | ||
array[begin+leftIndex] = array[begin+parentIndex]; | ||
array[begin+parentIndex] = temp; | ||
parentIndex = leftIndex; | ||
swapped = true; | ||
} | ||
} | ||
if (not swapped) { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
|
||
inline ListNode* popAndHeapify(ListNode** array, int32_t& numEntries) { | ||
ListNode* t = array[0]; | ||
if (numEntries == 1) { | ||
numEntries = 0; | ||
return t; | ||
} | ||
|
||
array[0] = array[numEntries-1]; | ||
--numEntries; | ||
adjustRoot(0, array, numEntries); | ||
return t; | ||
} | ||
|
||
inline ListNode* replaceFrontAndHeapify(ListNode* t, ListNode**array, int32_t& numEntries) { | ||
auto ret = array[0]; | ||
array[0] = t; | ||
adjustRoot(0, array, numEntries); | ||
return ret; | ||
} | ||
|
||
class Solution { | ||
public: | ||
static inline bool compareAscending(const ListNode* parent, const ListNode* child) { | ||
if (child->val < parent->val) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
ListNode* mergeKLists(vector<ListNode*>& lists) { | ||
int32_t numLists = lists.size(); | ||
int32_t emptyLists = 0; | ||
|
||
if (numLists == 0) { | ||
return nullptr; | ||
} | ||
|
||
ListNode* myVector[10000]; | ||
int32_t numEntries = 0; | ||
|
||
for (auto& ln : lists) { | ||
if (ln) { | ||
myVector[numEntries++] = ln; | ||
ln = ln->next; | ||
} else { | ||
++emptyLists; | ||
} | ||
} | ||
|
||
if (emptyLists == numLists) { | ||
return nullptr; | ||
} | ||
|
||
if (numEntries > 1) { | ||
int32_t i = 1; | ||
do { | ||
adjustChild(0, i++, myVector, numEntries); | ||
} while (i != numEntries); | ||
} | ||
|
||
ListNode* sortedList = nullptr; | ||
ListNode* sortedListTail = nullptr; | ||
|
||
while (numEntries != 0) { | ||
ListNode* temp; | ||
if (myVector[0]->next == nullptr) { | ||
temp = popAndHeapify(myVector, numEntries); | ||
} else { | ||
temp = replaceFrontAndHeapify(myVector[0]->next, myVector, numEntries); | ||
} | ||
|
||
if (sortedList == nullptr) { | ||
sortedList = sortedListTail = temp; | ||
} else { | ||
sortedListTail->next = temp; | ||
sortedListTail = temp; | ||
} | ||
|
||
} | ||
return sortedList; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#include "ListNode.hpp" | ||
|
||
class Verification { | ||
public: | ||
Verification() {} | ||
|
||
bool verify(ListNode* userOutput, ListNode* referenceOutput, string& developer) { | ||
ListNode* entry = userOutput; | ||
ListNode* refEntry = referenceOutput; | ||
|
||
bool success = true; | ||
|
||
while (refEntry != nullptr) { | ||
if (entry == nullptr || refEntry->val != entry->val) { | ||
success = false; | ||
break; | ||
} | ||
refEntry = refEntry->next; | ||
entry = entry->next; | ||
} | ||
if (entry != nullptr) { | ||
success = false; | ||
} | ||
|
||
refEntry = referenceOutput; | ||
entry = userOutput; | ||
|
||
if (not success) { | ||
cout << "Expected output:" << endl << "["; | ||
while (refEntry != nullptr) { | ||
cout << refEntry->val; | ||
refEntry = refEntry->next; | ||
if (refEntry) { | ||
cout << ","; | ||
} | ||
} | ||
cout << "]" << endl; | ||
|
||
cout << "Program output:" << endl << "["; | ||
while (entry != nullptr) { | ||
cout << entry->val; | ||
entry = entry->next; | ||
if (entry) { | ||
cout << ","; | ||
} | ||
} | ||
cout << "]" << endl; | ||
} | ||
return success; | ||
} | ||
}; |
Oops, something went wrong.