From cce115adad0cbbc0672e3331cd23bb975a0d8a5e Mon Sep 17 00:00:00 2001 From: Ayush Aryan Date: Mon, 3 Oct 2022 00:49:21 +0530 Subject: [PATCH 1/4] Solved Q22 Generate Parentheses c++ with comments --- .../Q22_Generate_Parentheses.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 leetCode Solutions/Q22_Generate_Parentheses.cpp diff --git a/leetCode Solutions/Q22_Generate_Parentheses.cpp b/leetCode Solutions/Q22_Generate_Parentheses.cpp new file mode 100644 index 0000000..e5fbc83 --- /dev/null +++ b/leetCode Solutions/Q22_Generate_Parentheses.cpp @@ -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&ans,int n){ + if(s.length() == n*2){ + ans.push_back(s); + return; + } + if(open generateParenthesis(int n) { + vector ans; + solve(0,0,"",ans,n); + return ans; + } +}; \ No newline at end of file From e4d8affe5fbee715ac55be31fbc7f44f5ac6051e Mon Sep 17 00:00:00 2001 From: Ayush Aryan Date: Mon, 3 Oct 2022 00:53:13 +0530 Subject: [PATCH 2/4] Solved Q 24 Swap Nodes in Pairs using c++ comments --- .../Q24_Swap_Nodes_in_Pairs.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 leetCode Solutions/Q24_Swap_Nodes_in_Pairs.cpp diff --git a/leetCode Solutions/Q24_Swap_Nodes_in_Pairs.cpp b/leetCode Solutions/Q24_Swap_Nodes_in_Pairs.cpp new file mode 100644 index 0000000..44490d0 --- /dev/null +++ b/leetCode Solutions/Q24_Swap_Nodes_in_Pairs.cpp @@ -0,0 +1,30 @@ +/** + * 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) {} + * }; + */ +class Solution { +public: + ListNode* swapPairs(ListNode* head) { + // 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; + } +}; \ No newline at end of file From 7b44561653169d3770714e1b3bb5e70703bde599 Mon Sep 17 00:00:00 2001 From: Ayush Aryan Date: Mon, 3 Oct 2022 00:58:06 +0530 Subject: [PATCH 3/4] Solved Q23. Merge k Sorted Lists in c++ comments --- .../Q23_Merge_k_Sorted_Lists.cpp | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 leetCode Solutions/Q23_Merge_k_Sorted_Lists.cpp diff --git a/leetCode Solutions/Q23_Merge_k_Sorted_Lists.cpp b/leetCode Solutions/Q23_Merge_k_Sorted_Lists.cpp new file mode 100644 index 0000000..31b5aa6 --- /dev/null +++ b/leetCode Solutions/Q23_Merge_k_Sorted_Lists.cpp @@ -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& lists) { + + // Our Priority Queue q will always store the K Smallest Elements from the K Sorted Lists + + priority_queue , 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) +*/ \ No newline at end of file From e0a8c574a5372c42b96638e14faaddab9327bc31 Mon Sep 17 00:00:00 2001 From: Ayush Aryan Date: Mon, 3 Oct 2022 18:09:19 +0530 Subject: [PATCH 4/4] making files correct --- .../Q22_Generate_Parentheses.cpp | 0 .../Q23_Merge_k_Sorted_Lists.cpp | 0 .../Q24_swapNodesInPairs.cpp | 24 ++++++++------- .../Q24_Swap_Nodes_in_Pairs.cpp | 30 ------------------- .../Q31_Next Permutation.cpp | 0 5 files changed, 13 insertions(+), 41 deletions(-) rename leetCode Solutions/{ => Q22_Generate_Parentheses}/Q22_Generate_Parentheses.cpp (100%) rename leetCode Solutions/{ => Q23_Merge_k_Sorted_Lists}/Q23_Merge_k_Sorted_Lists.cpp (100%) delete mode 100644 leetCode Solutions/Q24_Swap_Nodes_in_Pairs.cpp rename leetCode Solutions/{ => Q31_Next Permutation}/Q31_Next Permutation.cpp (100%) diff --git a/leetCode Solutions/Q22_Generate_Parentheses.cpp b/leetCode Solutions/Q22_Generate_Parentheses/Q22_Generate_Parentheses.cpp similarity index 100% rename from leetCode Solutions/Q22_Generate_Parentheses.cpp rename to leetCode Solutions/Q22_Generate_Parentheses/Q22_Generate_Parentheses.cpp diff --git a/leetCode Solutions/Q23_Merge_k_Sorted_Lists.cpp b/leetCode Solutions/Q23_Merge_k_Sorted_Lists/Q23_Merge_k_Sorted_Lists.cpp similarity index 100% rename from leetCode Solutions/Q23_Merge_k_Sorted_Lists.cpp rename to leetCode Solutions/Q23_Merge_k_Sorted_Lists/Q23_Merge_k_Sorted_Lists.cpp diff --git a/leetCode Solutions/Q24_SwapNodesInPairs/Q24_swapNodesInPairs.cpp b/leetCode Solutions/Q24_SwapNodesInPairs/Q24_swapNodesInPairs.cpp index 20a6c60..44490d0 100644 --- a/leetCode Solutions/Q24_SwapNodesInPairs/Q24_swapNodesInPairs.cpp +++ b/leetCode Solutions/Q24_SwapNodesInPairs/Q24_swapNodesInPairs.cpp @@ -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; } }; \ No newline at end of file diff --git a/leetCode Solutions/Q24_Swap_Nodes_in_Pairs.cpp b/leetCode Solutions/Q24_Swap_Nodes_in_Pairs.cpp deleted file mode 100644 index 44490d0..0000000 --- a/leetCode Solutions/Q24_Swap_Nodes_in_Pairs.cpp +++ /dev/null @@ -1,30 +0,0 @@ -/** - * 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) {} - * }; - */ -class Solution { -public: - ListNode* swapPairs(ListNode* head) { - // 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; - } -}; \ No newline at end of file diff --git a/leetCode Solutions/Q31_Next Permutation.cpp b/leetCode Solutions/Q31_Next Permutation/Q31_Next Permutation.cpp similarity index 100% rename from leetCode Solutions/Q31_Next Permutation.cpp rename to leetCode Solutions/Q31_Next Permutation/Q31_Next Permutation.cpp