Median of Two Sorted Arrays Problem

Problem Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). Examples Example 1: Input: nums1 = [1,3], nums2 = [2] Output: 2.00000 Explanation: merged array = [1,2,3] and median is 2. ...

Read N Characters Given Read4 2 - Call Multiple Times

Problem The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actual number of characters read. For example, it returns 3 if there is only 3 characters left in the file. By using the read4 API, implement the function int read(char *buf, int n) that reads n characters from the file. Examples Example 1 Input: "filetestbuffer" read(6) read(5) read(4) read(3) read(2) read(1) read(10) Output: 6, buf = "filete" 5, buf = "stbuf" 3, buf = "fer" 0, buf = "" 0, buf = "" 0, buf = "" 0, buf = "" ...

Find Minimum in Rotated Sorted Array 2 - Duplicates allowed

Problem Follow up for “Find Minimum in Rotated Sorted Array”: What if duplicates are allowed? Would this affect the run-time complexity? How and why? This is follow up of - Find Minimum in Rotated Sorted Array 1 - No duplicates allowed Examples Example 1: Input: nums = [1,3,5] Output: 1 ...

Edit Distance Problem

Problem Given two strings word1 and word2, return the minimum number of operations required to convert word1 to word2. You have the following three operations permitted on a word: Insert a character Delete a character Replace or substitute a character Examples Example 1: Input: word1 = "horse", word2 = "ros" Output: 3 Explanation: horse -> rorse (replace 'h' with 'r') rorse -> rose (remove 'r') rose -> ros (remove 'e') ...

Text Justification Problem

Problem Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified. OR Given an array of strings words and a width maxWidth, format the text such that each line has exactly maxWidth characters and is fully (left and right) justified. You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces ' ' when necessary so that each line has exactly maxWidth characters. ...

Trapping Rain Water Problem

Problem Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining. Examples Example 1: ...

First Missing Positive Problem

Problem Given an unsorted integer array, find the first missing positive integer. Follow up You have to find the smallest positive number missing from the array in O(n) time using constant extra space. You can modify the original array. Examples Example 1: Input: nums = [1,2,0] Output: 3 ...

Minimum Number of Days to Disconnect Island Problem

Problem You are given an m x n binary grid grid where 1 represents land and 0 represents water. An island is a maximal 4-directionally (horizontal or vertical) connected group of 1’s. The grid is said to be connected if we have exactly one island, otherwise is said disconnected. In one day, we are allowed to change any single land cell (1) into a water cell (0). ...

Unique Paths in Grid 3 - Count all paths moving in 4 directions with obstacles and custom start and end

Problem You are given an m x n integer array grid where grid[i][j] could be: 1 representing the starting square. There is exactly one starting square. 2 representing the ending square. There is exactly one ending square. 0 representing empty squares we can walk over. -1 representing obstacles that we cannot walk over. Return the number of 4-directional walks from the starting square to the ending square, that walk over every non-obstacle square exactly once. ...

Patching Array Problem

Problem Given a sorted integer array nums and an integer n, add/patch elements to the array such that any number in the range [1, n] inclusive can be formed by the sum of some elements in the array. Return the minimum number of patches required. Examples Example 1: Input: nums = [1,3], n = 6 Output: 1 Explanation: Combinations of nums are [1], [3], [1,3], which form possible sums of: 1, 3, 4. Now if we add/patch 2 to nums, the combinations are: [1], [2], [3], [1,3], [2,3], [1,2,3]. Possible sums are 1, 2, 3, 4, 5, 6, which now covers the range [1, 6]. So we only need 1 patch. ...

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