medium Array

Container With Most Water

Write a function maxArea that takes an array of integers where each element represents the height of a vertical line, and returns the maximum area of water it can contain.

Function Signature

function maxArea(height);

Example 1:

Input: height = [1,8,6,2,5,4,8,3,7]
Output: 49
Explanation: The maximum area is formed between the lines at index 1 and 8, resulting in an area of 49.

Example 2:

Input: height = [1,1]
Output: 1
Explanation: The maximum area is formed between the lines at index 0 and 1, resulting in an area of 1.

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 Problem

Related Problems

Merge Intervals

medium

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.

Array

Best Time to Buy and Sell Stock

medium

Write 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.

Array

Three Sum

medium

Write 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.

Array