leetcode on Jens Neuhalfen
https://neuhalfen.name/tags/leetcode/
Recent content in leetcode on Jens NeuhalfenHugo -- gohugo.ioenTue, 07 Jan 2020 00:00:00 +0100LeetCode #746 : Min cost climbing stairs
https://neuhalfen.name/blog/2020-01-leetcode-746-min_cost_climbing_stairs/
Tue, 07 Jan 2020 00:00:00 +0100https://neuhalfen.name/blog/2020-01-leetcode-746-min_cost_climbing_stairs/Description On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed).
Once you pay the cost, you can either climb one or two steps. You need to find minimum cost to reach the top of the floor, and you can either start from the step with index 0, or the step with index 1.
Example 1:
Input: cost = [10, 15, 20] Output: 15 Explanation: Cheapest is start on cost[1], pay that cost and go to the top.LeetCode #931. Minimum Falling Path Sum
https://neuhalfen.name/blog/2020-01-931-minimum-falling-path-sum/
Tue, 07 Jan 2020 00:00:00 +0100https://neuhalfen.name/blog/2020-01-931-minimum-falling-path-sum/Description Given a square array of integers A, we want the minimum sum of a falling path through A.
A falling path starts at any element in the first row, and chooses one element from each row. The next row's choice must be in a column that is different from the previous row's column by at most one.
Example 1:
Input: [[1,2,3],[4,5,6],[7,8,9]] Output: 12 Explanation: The possible falling paths are:LeetCode #8 : String to Integer (atoi)
https://neuhalfen.name/blog/2019-12-leetcode-008-string-to-integer-atoi/
Fri, 27 Dec 2019 00:00:00 +0100https://neuhalfen.name/blog/2019-12-leetcode-008-string-to-integer-atoi/Preface This problem is quite simple and does not allow for many algorithmic improvements. In fact it would be difficult to come up with a non-optimal (bigger than \(O(n)\)) solution. This makes this problem a good candidate for benchmarking.
I used the following tools:
pytest-benchmark to generate candlestick charts to determine test cases with adverse behavior line_profiler to profile methods line by line CAVE: All tests and benchmarks are run while exporting this page to HTML.LeetCode #10 : Regular Expression Matching
https://neuhalfen.name/blog/2019-12-leetcode-010-regular-expression-matching/
Mon, 16 Dec 2019 00:00:00 +0100https://neuhalfen.name/blog/2019-12-leetcode-010-regular-expression-matching/Description Given an input string (s) and a pattern (p), implement regular expression matching with support for . and *.
. Matches any single character. * Matches zero or more of the preceding element.
The matching should cover the entire input string (not partial).
Note:
s could be empty and contains only lowercase letters a-z. p could be empty and contains only lowercase letters a-z, and characters like .LeetCode #23 : Merge k Sorted Lists
https://neuhalfen.name/blog/2019-12-leetcode-023-merge-k-sorted-lists/
Mon, 16 Dec 2019 00:00:00 +0100https://neuhalfen.name/blog/2019-12-leetcode-023-merge-k-sorted-lists/Description Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
Example: Input: [ 1->4->5, 1->3->4, 2->6 ] Output: 1->1->2->3->4->4->5->6 (From the problem description)
Test Vectors It is not said in the problem statement, but the implementation bases on the following assumptions
Ordering All lists are ordered ascending and have comparable types. Empty lists Any list can be empty (None) but every node as a non-empty value.LeetCode #1 : Two Sum
https://neuhalfen.name/blog/2019-12-leetcode-001-two-sum/
Sun, 08 Dec 2019 00:00:00 +0100https://neuhalfen.name/blog/2019-12-leetcode-001-two-sum/Description Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1]. (From the Two-Sum problem description)LeetCode #2: Add Two Numbers
https://neuhalfen.name/blog/2019-12-leetcode-002-add-two-numbers/
Sun, 08 Dec 2019 00:00:00 +0100https://neuhalfen.name/blog/2019-12-leetcode-002-add-two-numbers/Description You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Example:
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 Explanation: 342 + 465 = 807.LeetCode #3: Longest Substring Without Repeating Characters
https://neuhalfen.name/blog/2019-12-leetcode-003-longest_substring_without_repeating_characters/
Sun, 08 Dec 2019 00:00:00 +0100https://neuhalfen.name/blog/2019-12-leetcode-003-longest_substring_without_repeating_characters/Description Given a string, find the length of the longest substring without repeating characters.
*Example 1*: Input: "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. *Example 2*: Input: "bbbbb" Output: 1 Explanation: The answer is "b", with the length of 1. *Example 3*: Input: "pwwkew" Output: 3 Explanation: The answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.