Skip to content
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

Solved Q22 Generate Parentheses c++ with comments #34

Merged
merged 4 commits into from
Oct 3, 2022
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
int this approachwe first create a vector and call our helper function with
open and close value as 0 , our n given in question ., an empty string and we pass address of
our freshly created vector.
we are having open and close parenthesis so we check if the size of our string is 2*n
if yes then we push that string in vector and finally return
else we recursively call for (open<n) && (close<open)
*/


class Solution {
public:
void solve(int open,int close,string s,vector<string>&ans,int n){
if(s.length() == n*2){
ans.push_back(s);
return;
}
if(open <n){solve(open+1,close,s+'(',ans,n);}
if(close < open){solve(open,close+1,s+')',ans,n);}
}



//
vector<string> generateParenthesis(int n) {
vector<string> ans;
solve(0,0,"",ans,n);
return ans;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/

/* The optimised approach of the problem - Merge K Sorted Lists, involves the usage of Min-Heap
In c++, Min-Heap is implemented with the help of Priority-Queue
At the begining, we will be storing only K Elements, into our Min-Heap.
We will also have a dummy Node to keep track of the Head of our Merged LinkedList.
Tail pointer will intially point to dummy but keeps on Nodes in order to form the Merged K Sorted LinkedList
After adding first K Elements, we will use a While Loop which runs till our Priority-Queue has not become empty.
Everytime, we will pick the Top Element from the Priority-Queue(which will also be smallest among all K Nodes present). We will make Tail's Next point to that Top Node. Make Tail shift to that Node. And, also add it's Next Node(if it exists)
By repeating the process, we will have our Merged K Sorted LinkedList ready. The Head of that LinkedList will be pointed by Dummy's Next
*/


// Class cmp uses Function Overloading to compare Two Nodes - A and B. It returns true if A's Value is smaller than B's Value. Else it returns false
// This cmp class is essential to make our Min-Heap using Priority-Queue in C++

class cmp
{
public :

bool operator() (ListNode *a , ListNode *b)
{
return a -> val > b -> val ;
}
};

class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {

// Our Priority Queue q will always store the K Smallest Elements from the K Sorted Lists

priority_queue<ListNode* , vector<ListNode*> , cmp> q ;

// Firstly, we enter all the K Nodes into our Min-Heap

for(int i = 0 ; i < lists.size() ; i++)
{
if(lists[i] != NULL)
q.push(lists[i]) ;
}

// Dummy Pointer's Nexy will point to the Head of our K Merged Sorted Linkedlist

ListNode *dummy = new ListNode(-1) ;

// Tail pointer will help us to add Nodes into our K Merged Sorted LinkedList

ListNode *tail = dummy ;

// While Loop will run till our the size of our Priority-Queue not becomes 0(zero)

while(q.size() )
{
// We pop the Topmost Element from our Priority-Queue and store it in Temp pointer

ListNode *temp = q.top() ;
q.pop() ;

// Tail's Next points to Temp

tail -> next = temp ;

// After that, Tail Pointer is moved to Temp

tail = temp ;

// We enter Temp's Next Node into our Priority-Queue if it exists

if(temp -> next != NULL)
q.push(temp -> next) ;
}

// At the end, we return Dummy's Next which contains the Head of our Merged K Sorted LinkedList

return dummy -> next ;
}
};

/*
Time Complexity: O(N*LogK)
Space Complexity: O(K)
*/
24 changes: 13 additions & 11 deletions leetCode Solutions/Q24_SwapNodesInPairs/Q24_swapNodesInPairs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,20 @@
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
//Solution using recursion

if(head == NULL || head -> next == NULL)
{
return head;
}
//Create a node such that it becomes the next of head
ListNode* temp;
temp = head->next;
head->next = swapPairs(head->next->next);
// if the head is itself NULL then the linked list doesnt exist
// if the next of head is null then it contains only one node
if(head == NULL || head->next == NULL){return head;}
// in both above senario we have to return our head only


// in this step we first create a dummy node with head->next's data
ListNode* temp = head->next;

//we recursively call the function for next node and it will call further one
head->next = swapPairs(head->next->next);
// we connect our temp node's next to head in order to swap it
temp->next = head;

//finally we return the list
return temp;
}
};