Solved Q22 Generate Parentheses c++ with comments #34
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a template on how to fill out a PR properly.
Description
Language Used: C++
Approach Used:
Q22 -> Dynamic Programming and Backtracking
Q23 -> Merge Sort ,Divide & Conquer, LinkedList
Q24 -> Recursion
Question(s) Solved: 3
Issue(s) Closes: [FEATURE]: Add solution to Q22 to Q30 of Leetcode [FEATURE]: Add solution to Q20 to Q30 of Leetcode #22,[FEATURE]: Add solution to Q31 to Q40 of Leetcode #23,[FEATURE]: Add solution to Q41 to Q50 of Leetcode #24
Q 22 . In this approach we 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)
Q23. 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
Q24. 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
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
we recursively call the function for next node and it will call further one
we connect our temp node's next to head in order to swap it
finally we return the list
Related Issue
Type of Changes
Testing Steps / QA Criteria
Q22.
![Screenshot (132)](https://user-images.githubusercontent.com/76223671/193581529-47d265a8-a1aa-401d-a054-58f51ac393d6.png)
Q23.
![image](https://user-images.githubusercontent.com/76223671/193583047-e0cb6395-0f30-486e-a6fc-52bb423d62e9.png)
Q24.
![image](https://user-images.githubusercontent.com/76223671/193583187-1cec50f5-a199-4845-bb83-7799e5f022fd.png)