An "interval" is a set of points given by a start
and an end
.
When you include the start and end points in the interval, it's called "inclusive" bounds, and is denoted with brackets [start, end]
.
When you don't include them in the interval, it's called "exclusive" bounds, and is denoted with parentheses (start, end)
.
To find if intervals overlap, you should first consider when they do not overlap, like this:
The condition for non-overlapping is this. We'll assume the first interval starts before the second one, which is common in interval problems.
The condition for overlapping is the opposite:
You can do the same thing if you're given a 2D interval. You can furst find when 2D intervals do not overlap.
The condition for non-overlapping is this:
The condition for 2D intervals overlapping is the opposite:
You should usually sort intervals by their starting location. This lets you create greedy algorithms when not otherwise possible. For example, you might be given these intervals:
You should usually sort them by their starting location, like this:
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()