Category Archives: programming

Leetcode 72: Edit Distance

This problem is also known as the Levenshtein distance. It is possible to solve recursively but that is very inefficient. Here is how to do it with dynamic programming. My initial logic didn’t work with recurring characters. I’ve highlighted the … Continue reading

Posted in programming | Tagged , , | Leave a comment

Leetcode 71: Simplify Path

My original solution (whiteboard notes below) involved manually parsing the directory names by stepping over characters one by one looking for the slash character. This was somewhat fiddly and error prone and required some fixes after trying the test cases. … Continue reading

Posted in programming | Tagged , , | Leave a comment

Leetcode 70: Climbing Stairs

I went down some time consuming dead ends on this problem. I focused on combinations and binomial coefficients. But these suffered from number variable overflow. What I initially missed was that the results of each n follows a fibonacci sequence. … Continue reading

Posted in programming | Tagged , , | Leave a comment

Leetcode 69: Sqrt(x)

Use binary search to solve this problem. Start with left as 1 and right as x. Then find the mid point and test by squaring it. If mid * mid is equal to x then we have our answer. If … Continue reading

Posted in programming | Tagged , , | Leave a comment

Leetcode 68: Text Justification

I quite liked this problem even though it seems to have a lot downvotes. It takes a while to solve because there is quite a lot of logic to encode but it is very doable. For me it was important … Continue reading

Posted in programming | Tagged , , | Leave a comment

Leetcode 67: Add Binary

Similar to the previous problem (Leetcode 66 – Plus One). Loop around both input strings from last char to first char. Add each digit and remember carries if needed. Source code for above solution:https://gist.github.com/adamkorg/7f7cc67a39fe8198ccdf1974c4eb7961And below is code for a slightly … Continue reading

Posted in programming | Tagged , , | Leave a comment

Leetcode 66: Plus One

Straightforward. Just add one to the rightmost number and then perform a carry to next significant digits if necessary.

Posted in programming | Tagged , , | Leave a comment

Leetcode 65: Valid Number

This is not a great leetcode question because it is not clearly defined. This would be fine as a real interview question where you can work out the requirements by asking the interviewer questions but in leetcode you just have … Continue reading

Posted in programming | Tagged , , | Leave a comment

Leetcode 64: Minimum Path Sum

This is a variation of the the two previous problems (Unique Paths). So we just use dynamic programming to calculate running costs at each cell in the grid. We add the current grid cell cost to the lesser value of … Continue reading

Posted in programming | Tagged , , | Leave a comment

Leetcode 63: Unique Paths II

This is fairly similar to the previous problem “Leetcode 62: Unique Paths” but here we will set the memo cell value to zero if there is an obstacle in that cell, as there is no route to the finish from … Continue reading

Posted in programming | Tagged , , | Leave a comment