Flood Fill Problem

Problem An image is represented by an m x n integer grid image where image[i][j] represents the pixel value of the image. You are also given three integers sr, sc, and color. You should perform a flood fill on the image starting from the pixel image[sr][sc]. To perform a flood fill, consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color), and so on. Replace the color of all of the aforementioned pixels with color. ...

Merge Sort in a Linked list

Problem Given a Linked List, Sort it using merge sort. Examples Example 1: Input: head = [4,2,1,3] Output: [1,2,3,4] Solution Method 1 - Recursive Refer merge sort - Merge Sort Algorithm. Get the pointer to middle node Split the list at the middle node Continue 1 and 2 , till list size is 1 or 2. Sort the elements in step 3 Merge the sorted list Now this involves 2 problems - Middle of the linked list Problem and Merge Two Sorted Linked Lists. To split linked list into 2, we can also use: Split Linked List into two halves fractionally given n ...

3Sum0 - Find three elements in an array that sum to a zero

Problem Given an array of integer write an algorithm to find 3 elements that sum to a zero. In short a+b+c = 0. OR Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0. Notice that the solution set must not contain duplicate triplets. ...

2Sum Problem

Problem You are given an array of n integers and a target sum T. The goal is to determine whether or not there are two numbers x, y in A with x+y=T. OR Given an array of integers, find two numbers such that they add up to a specific target number. Variant: Return the indices instead of the elements in array. Example Example 1: ...

Meeting Rooms 2 - Minimum Meeting Rooms Required

Problem Given a list of intervals representing the start and end time of ‘N’ meetings, find the minimum number of rooms required to hold all the meetings. Examples Example 1: Meetings: [ [1,4], [2,5], [7,9] ] Output: 2 Explanation: Since [1,4] and [2,5] overlap, we need two rooms to hold these two meetings. [7,9] can occur in any of the two rooms later. ...

Meeting Rooms 1 - Can person attend all meetings

Problem Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), determine if a person could attend all meetings. Examples Example 1: Input: intervals = [(0,30),(5,10),(15,20)] Output: false Explanation: (0,30), (5,10) and (0,30),(15,20) will conflict ...

Encode and Decode Strings Problem

Problem Design an algorithm to encode a list of strings to a string. The encoded string is then sent over the network and is decoded back to the original list of strings. Please implement encode and decode Examples Example1 Input: ["lint","code","love","you"] Output: ["lint","code","love","you"] Explanation: One possible encode method is: "lint:;code:;love:;you" ...

Product of Array Except Self

Problem Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i]. Examples Example 1: Input: nums = [1,2,3,4] Output: [24,12,8,6] Example 2: Input: nums = [-1,1,0,-3,3] Output: [0,0,9,0,0] Constraints 2 <= nums.length <= 105 -30 <= nums[i] <= 30 The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer. Follow up Can you solve the problem in O(1) extra space complexity? (The output array does not count as extra space for space complexity analysis.) ...

Implement Queue using Stacks

Problem Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (push, peek, pop, and empty). Implement the MyQueue class: void push(int x) Pushes element x to the back of the queue. int pop() Removes the element from the front of the queue and returns it. int peek() Returns the element at the front of the queue. boolean empty() Returns true if the queue is empty, false otherwise. Notes: ...

Implement Stack using Queues

Problem Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (push, top, pop, and empty). Implement the MyStack class: void push(int x) Pushes element x to the top of the stack. int pop() Removes the element on the top of the stack and returns it. int top() Returns the element on the top of the stack. boolean empty() Returns true if the stack is empty, false otherwise. Notes: ...

This site uses cookies to improve your experience on our website. By using and continuing to navigate this website, you accept this. Privacy Policy