Merge Intervals
Write a function mergeIntervals that takes an array of intervals and merges all overlapping intervals.
Function Signature
function mergeIntervals(intervals);
Example 1:
Input:
intervals = [[1, 3], [2, 6], [8, 10], [15, 18]]
Output:[[1, 6], [8, 10], [15, 18]]
Explanation: Since intervals [1, 3] and [2, 6] overlap, merge them into [1, 6].
Example 2:
Input:
intervals = [[1, 4], [4, 5]]
Output:[[1, 5]]
Explanation: Intervals [1, 4] and [4, 5] are considered overlapping.
Solve this problem
Start your 7-day free trial to solve this problem in our code editor with instant feedback.
Start 7-Day Free Trial to Solve This ProblemRelated Problems
Best Time to Buy and Sell Stock
mediumWrite a function `maxProfit` that takes an array where the `i-th` element is the price of a given stock on day `i`, and returns the maximum profit you can achieve by buying and selling one share of the stock. ## Function Signature ```function maxProfit(prices);``` ### Example 1: > **Input:** `prices = [7,1,5,3,6,4]` \ > **Output:** `5` \ > **Explanation:** Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5. ### Example 2: > **Input:** `prices = [7,6,4,3,1]` \ > **Output:** `0` \ > **Explanation:** In this case, no transactions are done, i.e. max profit = 0.
ArrayThree Sum
mediumWrite a function `threeSum` that takes an array of integers and returns all unique triplets that sum up to zero. ## Function Signature ```function threeSum(nums);``` ### Example 1: > **Input:** `nums = [-1, 0, 1, 2, -1, -4]` \ > **Output:** `[[-1, -1, 2], [-1, 0, 1]]` \ > **Explanation:** The triplets that sum up to zero are [-1, -1, 2] and [-1, 0, 1]. ### Example 2: > **Input:** `nums = []` \ > **Output:** `[]` \ > **Explanation:** No triplets can be formed from an empty array. ### Example 3: > **Input:** `nums = [0]` \ > **Output:** `[]` \ > **Explanation:** No triplets can be formed from a single element.
ArrayProduct of Array Except Self
mediumWrite a function `productExceptSelf` that takes an array of integers and returns an array such that each element at index `i` is the product of all the numbers in the original array except the one at `i`. ## Function Signature ```function productExceptSelf(nums);``` ### Example 1: > **Input:** `nums = [1, 2, 3, 4]` \ > **Output:** `[24, 12, 8, 6]` \ > **Explanation:** The product of all the numbers except the one at each index is [24, 12, 8, 6]. ### Example 2: > **Input:** `nums = [-1, 1, 0, -3, 3]` \ > **Output:** `[0, 0, 9, 0, 0]` \ > **Explanation:** The product of all the numbers except the one at each index is [0, 0, 9, 0, 0].
Array