-
Recent Posts
Recent Comments
Archives
Categories
Meta
Tag Archives: leetcode
Leetcode 57: Insert Interval
This was a little bit fiddly with some edge cases to consider. Iterate through the intervals array attempting to insert the new range or merge it. So at every step we need to check if the new range is before … Continue reading
Leetcode 56: Merge Intervals
Key insight is that it is much easier and more efficient to merge if we sort the ranges by the range begin. That results in O(nlogn) solution and I don’t think it can be done quicker than that.
Leetcode 55: Jump Game
The key insight to find the optimal O(n) solution is that we should evaluate our situation at every step as we iterate through the array, trying to maximise our remaining steps available. So in the first whiteboard example [2,3,1,1,4], we … Continue reading
Leetcode 51: N-Queens
The n-queens problem is about arranging n queens on an n x n chessboard such that no queen attacks another queen. It turns out that this is quite a common example for using a backtracking algorithm. However my initial solution … Continue reading
Leetcode 50: Pow(x, n)
This problem solution relies on binary exponentiation. The key insight is that 2^10 = 2^5 * 2^52^5 = 2^2 * 2^2 * 2So we can base our solution on recursively solving pow(x, n/2) and multiplying it by itself to get … Continue reading
Leetcode 49: Group Anagrams
Use a hash map with key being the sorted word and value being a vector of strings of all instances of that anagram. Iterate through input array, filling the hash map. Then iterate through the hash map adding the map … Continue reading
Leetcode 48: Rotate Image
The idea with my solution is that you work your way through layers of the box, starting with the outside layer and then moving inwards. For each layer we step through four points at a time, starting at the corners … Continue reading
Leetcode 46: Permutations
The obvious way to solve this is to repeatedly call next_permutation() from the standard library. Another way to solve it is to implement what next_permutation() does and I have done that in a previous solution:https://www.adamk.org/leetcode-31-next-permutation/ Here is my recursive solution … Continue reading
Leetcode 44: Wildcard Matching
The obvious solution is to use a recursive function. If we find a wildcard then we recurse every position in s from that point onwards. The problem with this is that it is very slow if there are a lot … Continue reading
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