Skip to main content

Posts

Unique path

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 (marked 'Finish' in the diagram below). How many possible unique paths are there? Above is a 3 x 7 grid. How many possible unique paths are there? Note:   m  and  n  will be at most 100.  Solution-

Merge Intervals

Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8,10],[15,18]] Output: [[1,6],[8,10],[15,18]] Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6]. Example 2: Input: [[1,4],[4,5]] Output: [[1,5]] Explanation: Intervals [1,4] and [4,5] are considerred overlapping. Solution-

Group Anagrams together

Given an array of strings, group anagrams together. Example: Input: ["eat", "tea", "tan", "ate", "nat", "bat"], Output: [   ["ate","eat","tea"],   ["nat","tan"],   ["bat"] ] Note: All inputs will be in lowercase. The order of your output does not matter. Solution-

Access largest element of the stack

Design a max stack that supports push, pop, top, peekMax and popMax. push(x) -- Push element x onto stack. pop() -- Remove the element on top of the stack and return it. getMax() -- Retrieve the maximum element in the stack Example: MaxStack stack = new MaxStack(); stack.push(5); stack.push(1); stack.push(5); stack.getMax(); -> 5 stack.pop(); -> 1 stack.top(); -> 5 Note: -1e7 <= x <= 1e7 Number of operations won't exceed 10000. The last four operations won't be called when stack is empty. Solution: This problem can be solved with multiple approaches. This approach makes use of two stacks to get the largest element. we can however optimize the behavior to use doubly linked list and a treemap.