Friedrich Ewald My Personal Website

Posts


  • Leetcode: Best time to buy and sell stock

    The task is to find out of a series of stock prices the best time to buy and sell the stock. Only one stock can be owned at the time and it can be sold and bought at the same time.

    Continue reading

  • Leetcode: Repeated substring pattern

    The task is to find out if a given string is comprised of multiple identical substrings. The first observation is that the substring cannot be longer than half of the length of the original string. The second observation is that the length of the total string has to be a multiple of the string that is tested, if it is not it can be discarded right away. Lastly, a string with the length of one can be discarded immediately.

    Continue reading

  • Leetcode: String in String

    A classic find the needle in the haystack problem. The task is to find if a string is contained within another string and return the index of the first position. If the string is not contained return -1 and if the needle is empty, return 0.

    Continue reading

  • Leetcode: Merge two lists

    class Solution:
      def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
        root = ListNode()
        current = root
    
        while list1 is not None and list2 is not None:
          if list1.val < list2.val:
            current.next = ListNode(list1.val)
            current = current.next
            list1 = list1.next
          else:
            current.next = ListNode(list2.val)
            current = current.next
            list2 = list2.next
    
        if list1 is not None:
          current.next = list1
        if list2 is not None:
          current.next = list2
    
        return root.next

    Results

    Runtime: 34 ms, faster than 97.81% of Python3 online submissions for Merge Two Sorted Lists. Memory Usage: 13.9 MB, less than 79.56% of Python3 online submissions for Merge Two Sorted Lists.

  • Leetcode: All possible subsets

    A subset of a list with the elements {a,b,c} is {a,b}. The task is to find all possible subsets. Sets in programming languages are usually not ordered. Therefore, the set {a,b,c} is equivalent to {b,c,a}. The idea for the solution is a nested for-loop that iterates over the results again and again, starting with one empty ([]) result.

    Continue reading

Page: 17 of 32