Monthly Archives: September 2019
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.
Straightforward. Just add one to the rightmost number and then perform a carry to next significant digits if necessary.
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
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
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
I came up with 5 different solutions for this: Simple recursive O(n!) Recursive with dynamic programming memoization O(nm) Iterative with dynamic programming O(nm) Mathematical using binomial coefficient O(max(n,m)) Mathematical using binomial coefficient optimised Source code for the different solutions:RecursiveRecursive with … Continue reading
Fairly straightforward if the steps are thought through before coding. I found it easier to have separate functions for moving forward n nodes and getting the size of a list.
The simple, obvious, inefficient way to solve this is to call next_permutation() k times. But that results in O(n!), which is slow. It is also possible in O(n) time, which relies on cutting through the problem space for each digit. … Continue reading
I find it simplifies the logic if I have a separate function for doing one layer at a time. The fiddly bit is getting the matrix coordinates correct and I plan that up front taking out the complication of layers.