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
This is an O(n) time, O(1) space solution. Find out list length by iterating through it. Recalculate k by mod len to reduce k to less than len. If new k is 0 return. Use slow and fast pointers to … Continue reading
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
Process using one layer at a time, starting from the outer layer. The fiddly bit is getting the matrix coordinates correct and I plan that up front taking out the complication of layers. Here is the code for the above … Continue reading