Skip to content

Update RotatedSortedArraySearch.cpp #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 10 additions & 97 deletions Binary-Search/RotatedSortedArraySearch.cpp
Original file line number Diff line number Diff line change
@@ -1,105 +1,18 @@
// https://www.interviewbit.com/problems/rotated-sorted-array-search/

int search1(const vector<int> &arr, int low, int high, int B)
{
if (low > high) return -1;

int mid = (low) + (high-low)/2;
if (arr[mid] == B) return mid;

if (arr[low] <= arr[mid])
{
if (B >= arr[low] && B <= arr[mid])
return search1(arr, low, mid-1, B);

return search1(arr, mid+1, high, B);
}

else if (arr[mid] <= arr[high])
{
if (B >= arr[mid] && B <= arr[high])
return search1(arr, mid+1, high, B);

return search1(arr, low, mid-1, B);
}
}


int Solution::search(const vector<int> &A, int B) {

int n = A.size();
int i = search1(A, 0, n-1, B);

if (i != -1)
return i;
else
return -1;

}

/*
int findPivot(const vector<int> &A){
int start = 0;
int end = A.size()-1;
int mid;
int n = A.size();
while(start <= end){
mid = start + (end-start)/2;
int next = (mid+1)%n;
int prev = (mid-1+n)%n;
if(A[start] <= A[end]){
return start;
}
else if((A[mid] <= A[prev]) && (A[mid] <= A[next])){
return mid;
}
else if(A[mid] <= A[end]){
end = mid-1;
}
else{ // A[mid] >= A[start]
start = mid+1;
}
}
return -1;
}

int binarySearch(const vector<int> &A, int start, int end, int key){
int mid;
while(start <= end){
mid = start + (end-start)/2;
if(A[mid] == key){
return mid;
}
else if(A[mid] > key){
end = mid-1;
}
else{ // A[mid] < key
start = mid+1;
int low = 0, high = n-1;
while(low<=high){
int mid = low + (high-low)/2;
if(A[mid] == B) return mid;
else if(A[0]<=A[mid]){//i.e. left part is sorted
if(A[0]<=B && B < A[mid]) high = mid-1;//B lies on left part
else low = mid+1;
}else{//right part is sorted
if(A[mid] < B && B<=A[n-1]) low = mid+1;//B lies on right part
else high = mid-1;
}
}
return -1;
}

int Solution::search(const vector<int> &A, int B) {
// Do not write main() function.
// Do not read input, instead use the arguments to the function.
// Do not print the output, instead return values as specified
// Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details

int pivot = findPivot(A);
if(B == A[pivot]){
return pivot;
}

int result = binarySearch(A, 0, pivot-1, B);

if(result == -1){
return binarySearch(A, pivot+1, A.size()-1, B);
}

return result;

// B < A[pivot]

}
*/
63 changes: 19 additions & 44 deletions Heaps-and-Maps/MergeKSortedLists.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// https://www.interviewbit.com/problems/merge-k-sorted-lists/

/**
* Definition for singly-linked list.
* struct ListNode {
Expand All @@ -8,49 +6,26 @@
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
struct Compare
{
bool operator()(ListNode* const& a, ListNode* const& b)
{
return a->val > b->val;
}
};
ListNode* Solution::mergeKLists(vector<ListNode*> &A) {
// Do not write main() function.
// Do not read input, instead use the arguments to the function.
// Do not print the output, instead return values as specified
// Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details

map<int, int> myMap;

for(int i = 0; i < A.size(); i++){
ListNode* curr = A[i];
while(curr != NULL){
int temp = curr->val;
if(myMap.find(temp) != myMap.end()){
myMap[temp]++;
}
else{
myMap[temp] = 1;
}
curr = curr->next;
}
priority_queue<ListNode*, vector<ListNode*>, Compare>pq;
if(A.empty()) return NULL;
ListNode* head = new ListNode(0);
ListNode* curr = head;
for(auto i = 0; i < A.size(); i++){
pq.emplace(A[i]);
}

auto it = myMap.begin();

ListNode* head = NULL;
ListNode* curr = NULL;

while(it != myMap.end()){
while(it->second != 0){
ListNode* list = new ListNode(it->first);
if(head == NULL){
head = list;
curr = list;
}
else{
curr->next = list;
curr = curr->next;
}
it->second--;
}
it++;
while (!pq.empty()){
curr->next = pq.top();
pq.pop();
curr = curr->next;
if(curr->next) pq.emplace(curr->next);
}

return head;

return head->next;
}