Friedrich Ewald My Personal Website


  • Leetcode: Word search

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

    Continue reading

  • Leetcode: Unique paths

    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):

    | S |   |   |    S = Start
    |   |   | F |    F = Finish
    There are 3 different ways from start to finish:
    • Down, Right Right
    • Right, Down, Right
    • Right, Right, Down
    To simplify this problem, we can think of a down move as 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.

    Continue reading

  • Leetcode: Merge intervals

    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]).

    Continue reading

  • Leetcode: Remove nth node from end of list

    Given the 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 = This way the nth element is skipped. However, there is a more elegant way to achieve this.

    Continue reading

  • Leetcode: Sort colors

    Given an array nums with n colored objects, sort the array in place so that the colors are orders. The colors are represented by the numbers 0, 1, and 2. The easiest way to achieve this is via quicksort in O(n*log n).

    Continue reading

Page: 9 of 28