Example: Input: 3 Output: Before solving this, have a look at similar problem “pascal’s triangle”. The only difference between this and the previous question is, we need to use o(k) space. We follow the same procedure, but instead of using a 2D vector, we use 1D …
Given a non-negative integer numRows, generate the first numRows of Pascal’s triangle. Example: Input: 5 Output: , , , , ] Solution analysis: In each row, the first and last element are 1. And the other element is the sum of the two elements in the …
Given an array of sorted intervals of non overlapping integers, insert a new interval into intervals by merging them Example 1: Input: intervals = ,], newInterval = Output: ,] The solution is very easy. The explanation has been provided in the code as a comment. …
Given a string containing only digits, get all the valid IP address. Example: Input: "25525511135" Output: The solution for this problem can be done in 2 ways. Iterative/Brute force approach Recursive/DFS But before we solve this, we need to consider below few points: An IP address …
Example: Input: 1->2->3->4->5->NULL, m = 2, n = 4 Output: 1->4->3->2->5->NULL Similar problem to reverse whole list. To solve this problem we need to use 4 pointers. Below are the 4 pointers are used: 1. new_head- track head position (new_head.next = head) case: if head is …
Example: Input: Output: , , , , , ] Before solving this problem, have a look at similar kind of problem Subsets. Similar to the subset 1 problem, we can solve this problem by 2 ways. Iterative Recursive/ Backtrack. We shall discuss both of the solutions …
Example 1: Input: 2 Output: Explanation: 00 - 0 01 - 1 11 - 3 10 - 2 Before solving this problem, let us understand what grey code is. Before knowing about grey code, we shall understand about the binary format. Below are some numbers and …
Example: Input: head = 1->4->3->2->5->2, x = 3 Output: 1->2->2->4->3->5 Solution: Let us understand the question first. The question has 2 parts: Part 1: Here we need to partition the list in such a way that the nodes less than “x” should come to left and …