Given the following code skeleton, implement an LRU cache with a variable capacity. The time complexity for get
and put
should be O(1)
respectively.
Given two lists or arrays, nums1
and nums2
as well as two integers m
and n
which give the length of the arrays nums1
and nums2
respectively. nums1
should be modified in place.
The first idea that came to my mind was a merge-sort. Given that those initial arrays are already sorted, I could have two pointers and iterate over both arrays simulatenously, copying the values into a third one. The problem here is that the array should be modified in place. The way to achieve this in O(2(n+m)) => O(n+m)
is to iterate over the temporary array and write it back to the initial array.
Given a two-dimensional array with english upper- and lowercase letters, named board
and a word
, return 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).
Given an 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 right
or down
steps. How many different combinations of right
and 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 (2,3)
:
3
different ways from start to finish:
1
and of a right move as 0
. This shows us, that we can either do 100
, 010
, or 001
. We observe that the one is in every position. Now, two different down moves are identical, hence there are less than 2^n
solutions.
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 1
.
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 lambda
expression: intervals.sort(key=lambda item: item[0])
.