Category Archives: programming

Leetcode 43: Multiply Strings

The general steps to do long multiplication are fairly straightforward but the implementation was a bit fiddly. I think it would be easier to to reverse the strings and work on them in reverse, then the least significant index will … Continue reading

Posted in programming | Tagged , , | Leave a comment

Leetcode 42: Trapping Rain Water

This is a tricky leetcode problem. There are a few observations that need to be made. Water traps between two bars and only fills each of those gaps to a volume that is the gap’s height taken away from the … Continue reading

Posted in programming | Tagged , , | Leave a comment

Leetcode 41: First Missing Positive

This is tricky and probably not typical of a real world problem, so I don’t know how useful it is know. Given the criteria of running it O(n) time and only using constant extra space, the only way we can … Continue reading

Posted in programming | Tagged , , | Leave a comment

Leetcode 39: Combination Sum

This is a problem that fits naturally to a recursive solution. I’m not sure how I’d implement without recursion. The recursion automatically does a backtrack to find valid solutions. By starting the search for sub-items from the current item and … Continue reading

Posted in programming | Tagged , , | Leave a comment

Leetcode 37: Sudoku Solver

This was an interesting problem that highlights that sometimes it is better to write an algorithm for a computer to execute in a different way to how we would solve the problem with a human mind. So I first attempted … Continue reading

Posted in programming | Tagged , , | Leave a comment

Leetcode 32: Longest Valid Parentheses

This was probably the easiest leetcode “hard” question that I’ve done so far. My first attempt worked on the two examples in the description but failed on the following test case “()(()”. My first attempt just used counters and did … Continue reading

Posted in programming | Tagged , , | Leave a comment

Leetcode 31: Next permutation

Here are my whiteboard notes for solving this: So it turns out this algorithm was first discovered a long time ago. According to Knuth (in The Art Of Computer Programming 7.2.1.2), it goes back to the 18th century. This is … Continue reading

Posted in programming | Tagged , , | Leave a comment

Leetcode 25: Reverse nodes in k-group

My first thoughts for reversing the group was to have start and end pointers, switching those and then moving pointers one step inwards. This is logically how I would tackle reversing array elements but it does not work well with … Continue reading

Posted in programming | Tagged , , | Leave a comment

Leetcode 23: Merge k sorted lists

Here are my whiteboard notes for solving this problem: And here is my c++ source code for the solution: https://gist.github.com/adamkorg/fb698c0785845576ff0b42a091547691 Initially I started off with what I thought was a simple inefficient way of finding the next smallest node but … Continue reading

Posted in programming | Tagged , , | Leave a comment

Leetcode 17: Phone key combinations

Recursion is a very natural fit for this problem. Iterate through each letter of each digit and recursively go into each of those letters to the next digit. If we are at the last digit then we add the current … Continue reading

Posted in programming | Tagged , , | Leave a comment