Friedrich Ewald My Personal Website

Posts


  • Leetcode: Three sum

    Given a list of integers, add three integers at the same time so that the sum of those integers is 0. There should be no repetition of integers, for example [-1, 0, 1] is equivalent to [-1, 1, 0].

    Continue reading

  • Leetcode: Longest Consecutive Sequence

    From an unsorted array nums, return the length of the longest consecutive sequence of numbers as an integer. For example, the list [100, 4, 200, 1, 3, 2] would return 4, because the longest consecutive sequence is [1, 2, 3, 4] with a length of 4. The algorithm should perform in O(n). Not mentioned in the task is that there can be duplicates of numbers. They should be ignored. The main problem when developing this algorithm is for it to perform it in O(n). A brute force method is quite easy and works for smaller lists. The pseudo code looks like this:

    for every element in list
      while next element in list
        take next element
        increment counter by 1
      endwhile
      return longest counter
    endfor
    This approach works but is very time consuming with a complexity of O(n^2). This is because for every element in the list we need to possibly visit every other element in the list.

    Continue reading

  • Leetcode: Happy number

    Write an algorithm to determine if a number n is happy. A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits. Repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy. Return true if n is a happy number, and false if not.
    For example, the number 19 is divided into 1^2 and 9^2, then added together as 82, then divided again, etc. After some iterations this reaches 100 which is 1^2 + 0^2 + 0^2 = 1. On the other hand, 2 keeps ever incremententing. The programming aspect is relatively easy to solve in Python when converting the types to a string and then converting the string to a list.

    Continue reading

  • Leetcode: Linked list cycle

    Given the head of a linked list, determine if there is a cycle in the list. A cycle is defined as a chain of arbitrary length that point to the same node. As an added difficulty, the solution should have a space complexity of O(1). The first observation is that the cycle can be of arbitrary length. Thus, saving the visited nodes in a set would have a space complexity of O(n). Secondly, the numbers cannot be compared because numbers might be duplicated throughout the list. Instead, we need to compare the list objects themselves.

    Continue reading

  • Leetcode: Contains duplicate

    Given an array of nums, return True if there is a duplicated number and False otherwise. The simple most solution I could find was to use a set and then see for every number if it is already contained in the set. A map would have worked as well. The time complexity for this is O(n), assuming that the access to the hash set is O(1), it takes only one iteration over the whole list.

    Continue reading

Page: 10 of 28