You're given an array arr
represents a bar graph.
You can pick two bars to serve as a bucket, and must discard all the other bars. Find the maximum amount of water that you can get to pool in the bucket.
You should assume the width of each bar is 0.
Example:
Example: The best possible bucket (red) can catch 8 units of water.
To solve this problem efficiently, you should try to do a single pass through the array. The idea is to use 2 pointers which represent the walls of a bucket. The pointers start on the left and right sides of the array.
The trick is to realize that you will never get a larger area of water by moving the taller pointer inwards, like this:
This means you should only move the shortest of the 2 pointers inwards - this is the only way to get a larger area. You can solve the problem by repeatedly moving the shortest pointer inwards, until the pointers cross.
Here's an example of moving shorter pointer inwards:
To code this up, you have to start a pointer on the left and right sides of the array. You should keep moving the shortest pointer inwards, until the 2 pointers cross. Each time you move a pointer, you have to update the max area you've seen so far maxArea
. See the spoiler for details on how to compute the amount of water in a bucket.
The water in the bucket is always in the shape of a rectangle. This means we can get the area of water in the bucket by multiplying its height and width area = height*width
.
The water's height will go up to the shortest of the walls of the bucket min(arr[i], arr[j])
. The width will be equal to j - i
.
Here's the code:
TreeNode.of
function can be used to create an entire binary tree using 1 line of code. The input is a BFS traversal of the tree including nulls, and the output is the root node of the tree.TreeNode.of([1, None, 2, None, 3])
ListNode.of
function lets you create an entire Linked List using 1 line of code. The input is an array, and the output is the head of the Linked List.ListNode.of([1, 2, 3])
ListNode.of([1, 2, 3], 1)
GraphNode.of
function lets you create an entire Graph using 1 line of code. The input is an Adjacency Matrix, and the output is the first node in the matrix.GraphNode.of([ ['A', 1, 2], ['B', 2], ['C', 0] ])
[['__init__', 15], ['add', 16], ['get']]
c = MyClass(15) c.add(16) c.get()
TreeNode.of
function can be used to create an entire binary tree using 1 line of code. The input is a BFS traversal of the tree including nulls, and the output is the root node of the tree.TreeNode.of([1, None, 2, None, 3])
ListNode.of
function lets you create an entire Linked List using 1 line of code. The input is an array, and the output is the head of the Linked List.ListNode.of([1, 2, 3])
ListNode.of([1, 2, 3], 1)
GraphNode.of
function lets you create an entire Graph using 1 line of code. The input is an Adjacency Matrix, and the output is the first node in the matrix.GraphNode.of([ ['A', 1, 2], ['B', 2], ['C', 0] ])
[['__init__', 15], ['add', 16], ['get']]
c = MyClass(15) c.add(16) c.get()