Given a begin_word and end_word, find the words that transforms from begin_word to end_word. Example 1: Input: beginWord = "hit", endWord = "cog", wordList = Output: 5 Explanation: As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog", return its length 5. …
Example 1: Input: Output: 5 Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5. Not 7-1 = 6, as selling price needs to be larger than buying price. The solution for this problem is …
For example, given the following triangle , , , ] The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11). Follow up question: Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle. …
Below are the rules for a Sudoku to be valid: Each row must contain the digits 1-9 without repetition. Each column must contain the digits 1-9 without repetition. Each of the 9 3×3 sub-boxes of the grid must contain the digits 1-9 without repetition. Example 1: Input: , , , , , …
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 …