Given a two-dimensional array with english upper- and lowercase letters, named
board and a
True if the word appears on the board and false if not. We can go up, down, left, and right but not diagonal. Letters cannot be used twice.
When looking at this problem, it helps to imagine the
board as a tree. Every letter is a potential root of the tree, depending if it is equal to the start of the word that we are looking for. This allows us to apply a slightly modified version of the breadth-first-search algorightm (BFS).
m * n grid, we start in the top left corner at position
(1,1). We want to move to the bottom right corner with either
down steps. How many different combinations of
down are there for a given grid of size
m * n? We need to find an algorithm that works on arbitrarily large grids.
Given this example grid of
3 different ways from start to finish:
1and of a right move as
0. This shows us, that we can either do
001. We observe that the one is in every position. Now, two different down moves are identical, hence there are less than
2^nsolutions. If we think of this problem as a backtracking problem, we can come up with a recursive algorithm. At the first position we can go either down or right. Depending on which way we go, we have to shrink the field either on the x or y-axis. We then add the result of going right to the result of going down. This step is repeated until we can only go down or right in which case we can return
Given an array of intervals of the form
[start, end], merge all overlapping intervals and return an array of non-overlapping intervals.
One example is the following array
[[1,2], [3,4]]. There is no overlap here. Hence the result is the same as the input. Another example is
[[1,2], [1,4]]. They are overlapping and the result is
[[1,4]]. If the start number of the next element is the same as the end number of the previous element, the items should be treated as overlapping.
One possible solution is to first sort the items by start element. This can be achieved with an in-place sorting and a
intervals.sort(key=lambda item: item).
head of a linked list, remove the
nth element from the end of the list and return the head of the list. As a follow-up, this should be done with one iteration.
The most obvious solution is to count the number of elements in the list in one iteration. This is necessary, because we don’t know initially how long the list is. Then iterate over the list again and once the index is reached, set
pointer = pointer.next. This way the
nth element is skipped.
However, there is a more elegant way to achieve this.
Given an array
n colored objects, sort the array in place so that the colors are orders. The colors are represented by the numbers
The easiest way to achieve this is via quicksort in