Taming Open Source Swift (SR-7292)

In December 2015, Apple made Swift an open source language and made it available on Github. This piqued my interest, I dug around the code base for a little, cloned it and felt like I had achieved something great. Fast forward to Spring 2017, I made my first contribution to the swift-corelibs-foundation, two contributions to be precise. The way I …

Mohit AthwaniTaming Open Source Swift (SR-7292)

Leetcode – 64. Minimum Path Sum

There were two approaches that came to my mind when I first read this problem. This is a classic recursion problem and we can use a a modified DFS approach checking at each point if we can move left and down and then recurse from the new positions. The time complexity of this would be exponential of the order of …

Mohit AthwaniLeetcode – 64. Minimum Path Sum

Leetcode – 212. Word Search II

This problem builds on Word Search, the only difference being instead of searching for one particular word, we are now given a set of words. We could take our solution from the Word Search problem and run that algorithm for every word in the data set which would result in a lot of wasted computations as the nested for loops …

Mohit AthwaniLeetcode – 212. Word Search II

Leetcode – 79. Word Search

In this problem, we are given a two dimensional board of characters and we have to check if the word can be found in the board by checking adjacent cells only. A basic strategy to solve this problem would be to start walking down the board until the first character is found. Once the first character is found, look at …

Mohit AthwaniLeetcode – 79. Word Search

Leetcode – 863. All Nodes Distance K In Binary Tree

When I first saw this problem, I panicked! I knew that this some how had to be a Breadth First Search problem because BFS from any node gives you the shortest path to every other node that is connected to the tree. Since every node is represented as a TreeNode with only left and right pointers, I would need to …

Mohit AthwaniLeetcode – 863. All Nodes Distance K In Binary Tree

Leetcode – 33. Search In Rotated Sorted Array

This is straight up Binary Search problem with some modifications. When we find the mid point of the given array, one of two things will hold true: The array from start to mid is sorted. The array from mid to end is sorted. Once we know which is the sorted half, all we have to do is check if the …

Mohit AthwaniLeetcode – 33. Search In Rotated Sorted Array

Leetcode – 270. Closest Binary Search Tree Value

If the type of the target and they type of the value in the tree nodes were the same, and the problem was to find the node in the tree that is just smaller than the target, the problem becomes fairly simple. For example, given target = 17 and the graph:

the value we will return is 14. The …

Mohit AthwaniLeetcode – 270. Closest Binary Search Tree Value

Leetcode – 42. Trapping Rain Water

There’s one thing common between this problem and the Container With Most Water problem and that is the height of the shorter tower dictates the result. In this problem, the amount of water that can accumulate between two towers is limited by the height of the shorter tower.  Consider an array [4, 5, 9, 7 ,1, 0 ,4, 0 ,8 …

Mohit AthwaniLeetcode – 42. Trapping Rain Water

Leetcode – 23. Merge k Sorted Lists

There are so many ways to solve this problem. Let’s look at some of them: Iterate through all the nodes in all the k lists and dump the values in an array, then sort the array, iterate through the sorted array, generate the linked list and return the head. This obviously doesn’t sound like an efficient algorithm. We can use …

Mohit AthwaniLeetcode – 23. Merge k Sorted Lists

Leetcode – 22. Generate Parentheses

Before we start solving this problem, let’s do some math first. The problem statement clearly states that we have N pairs of parentheses to work with. That means for a string to be considered as part of the solution, it must be of size 2N. Once we have generated a string of size 2N, for it to actually be a …

Mohit AthwaniLeetcode – 22. Generate Parentheses