Sort Colors In Place
We can solve this by many different ways. Solution 1 Will be counting all the number of 0’s, 1’s and 2’s. Then insert those number of elements in the array. We have discussed this solution in the below link: Sort an array of 0s, 1s and 2s in C Solution 2: This will be an […]
Search a 2D Matrix in C++
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer of the previous row. This problem can be solved by 2 ways. […]
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place.
Example 1: Input: [ [1,1,1], [1,0,1], [1,1,1] ] Output: [ [1,0,1], [0,0,0], [1,0,1] ] Example 2: Input: [ [0,1,2,0], [3,4,5,2], [1,3,1,5] ] Output: [ [0,0,0,0], [0,4,5,0], [0,3,1,0] ] Follow up: • A straight forward solution using O(mn) space is probably a bad idea. • A simple improvement uses O(m + n) space, but still not […]
Jump Game II in CPP
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of jumps. Example: Input: [2,3,1,1,4] Output: 2 Explanation: The minimum number of jumps […]
Simplify Path CPP
Given an absolute path for a file (Unix-style), simplify it. For example, path = “/home/”, => “/home” path = “/a/./b/../../c/”, => “/c” Corner Cases: • Did you consider the case where path = “/../”? In this case, you should return “/”. • Another corner case is the path might contain multiple slashes ‘/’ together, such […]
Minimum Path Sum in CPP
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. Example: Input: [ [1,3,1], [1,5,1], [4,2,1] ] Output: 7 Explanation: Because the […]
Unique Paths II in CPP
Problem Explanation: A robot is located at the top-left corner of a m x n grid. The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid. Now consider if some obstacles are added to the grids. How many unique […]
Unique Paths Solution in CPP
A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid. How many possible unique paths are there? Example 1: […]
Rotate linked list by k nodes
Example 1: Input: 1->2->3->4->5->NULL, k = 2 Output: 4->5->1->2->3->NULL Explanation: rotate 1 steps to the right: 5->1->2->3->4->NULL rotate 2 steps to the right: 4->5->1->2->3->NULL Example 2: Input: 0->1->2->NULL, k = 4 Output: 2->0->1->NULL Explanation: rotate 1 steps to the right: 2->0->1->NULL rotate 2 steps to the right: 1->2->0->NULL rotate 3 steps to the right: 0->1->2->NULL […]
Intersection of Two Linked Lists in c++
Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: a1 → a2 ↘ c1 → c2 → c3 ↗ B: b1 → b2 → b3 begin to intersect at node c1. This problem can be solved in 2 ways: […]