Category Archives: programming

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

There are a few different ways to solve this including some ways that destroy some of the (out of bounds) values. My favoured solution keeps all the values but just rearranges them. I iterate through all the numbers and at … 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

Note the following test case “()(()” will trip up a simple counting algorithm, which was my first attempt. So I then used a stack to keep track of outstanding open bracket positions. When they are matched with a close bracket, … Continue reading

Posted in programming | Tagged , , | Leave a comment

Leetcode 31: Next permutation

A few observations need to be made to solve this problem efficiently. The best way to make those observations is to write out a bunch of sequences (I used most of 1,2,3,4). Notice that we need to look for a … Continue reading

Posted in programming | Tagged , , | 1 Comment

Leetcode 25: Reverse nodes in k-group

This problem is similar to reverse a linked list but we wrap that in a loop so we do it repeatedly while there are k nodes ahead. We also need to wire the previous k sequence end with the the … 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

Use recursion to walk through the combination space. I use an array of codes, which maps the index to the letter combinations for that index. In the recursive function we loop through all the possible letters for the current number … Continue reading

Posted in programming | Tagged , , | Leave a comment

Median Of Two Sorted Arrays problem

This is a tricky little leetcode problem:https://leetcode.com/problems/median-of-two-sorted-arrays/ Here are some of my observations on a whiteboard: There are a lot of fiddly edge cases. I think my solution could be simplified but here is the gist anyway: https://gist.github.com/adamkorg/ffba283f298e7c2c2b748acb49ccc9f1

Posted in programming | Tagged , , | Leave a comment