Posts
    158 posts
Game of Life
Here’s a medium problem of which I found out a weird behavior, probably a bug in LeetCode. ...
Shortest Completing Word
The idea to solve this problem is to first create a histogram of characters for the license plate. Then, for each word, and for each character of the word, check if it exists in our histogram and if it is, then reduce the count. If the total sum of the histogram is zero then that means the word completes our license plate and is a candidate for the answer. ...
Max Consecutive Ones
I’m starting to enjoy binary related problems particularly bit manipulation. But this time, it’s just plain loop that monitors max count of consecutive ones. Easy peasy. ...
Design HashSet
Another simple problem with simple solution :) ...
Verifying an Alien Dictionary
My answer to this problem is very verbose and I didn’t like it but it works so ¯_(ツ)_/¯. ...
Partition Array Into Three Parts With Equal Sum
Easy problem with easy solution. ...
Number of Days in a Month
This is rather easier if you can have access to a calendar to check if a certain year is a leap year, otherwise, you need to memorize this criteria to check if a year is a leap year: ...
Distance Between Bus Stops
Ok, I cheated on this one by looking at other people’s solution. I initially tried hard to compute the clockwise and counterclockwise sum without realizing that I can actually just compute the clockwise and subtract it from the total sum. Duh! But I reimplemented the idea using Kotlin’s way so give me credit even just for a bit :D ...
Reverse Linked List
This could be one of the most commonly asked questions in LC. This question has been asked in interviews for the following companies: Amazon, Microsoft, Google, Mathworks, Apple, Oracle, Uber etc. ...
Index Pairs of a String
I’ve used Brute-force to solve this problem. Other people used Trie to solve but I’m not yet familiar with it. ...
Binary Number With Alternating Bits
I’m starting to get comfortable with bit manipulation and here’s another problem that I’ve solved in just a few amount of time. ...
Goat Latin
Easy problem with a short solution. ...
Complement of Base 10 Integer
This is an interesting easy problem. ...
Flip-Game
This is another easy LC problem with easy solution. ...
Invert Binary Tree
I initially tried to solve this iteratively but I couldn’t figure out the solution. Recursive approach seems to be the best and easiest way. ...
Average of Levels in Binary Tree
Another problem that can be solved easily using recursion and with the help of Kotlin’s operator, everything becomes easy-peasy. ...
Binary Gap
This is a bit manipulation problem and the solution, although a bit verbose is really efficient beating 100% of all submission for Kotlin. ...
Sum of Root to Leaf Binary Numbers
I solved this problem initially using Java but here’s a similar implementation using Kotlin. ...
Fizz Buzz
Another elegant solution for easy LC problem. ...
Prime Number of Set Bits in Binary Representation
This solution for easy LC problem is very concise the easy to understand don’t you think? ...
Next Greater Element I
Another easy to understand solution for an easy-level LC problem. ...
Palindrome Permutation
I cheated on this easy problem a little bit by looking at the suggested hints. ...
Uncommon Words From Two Sentences
I really don’t like my solution for this one but it’s enough for now. Maybe one way to optimize this is not to use the map and filter operators and just use pure for-loops. ...
Single Number
This is an easy but very tricky problem specially if you aren’t too familiar with logical operations. ...
Baseball Game
Another problem with a very straighforward answer. ...
Island-Perimeter
This is definitely an easy one because all you have to do is to that if a block is filled up, check the four conditions: ...
Binary Tree Right Side View
Here’s one of my favorite questions in LC. I like this because it looks complicated as it may seem but the approach to the solution is very simple. ...
Valid Palindrome
Another easy problem and probably one of the most commonly asked questions in interviews. ...
Last Stone Weight
Is there any other way to solve this without using PriorityQueue? One could use a list and sort it always everytime a new non-zero value is created from smashing the rocks together but that won’t be that much different than using PriorityQueue which sorts the elements in every offer ...
Toeplitz Matrix
Not the cleanest solution but so far this is the only solution I can think of for this easy LC problem. ...
Sum of Even Numbers After Queries
My first solution to this easy problem passed but only faster than 6% of submissions which means we still have some room for improvements. ...
Compare Strings by Frequency of the Smallest Character
This easy problem looks intimidating at first because the description looks complicated but when you analyze it further, the solution is actually straightforward. ...
Number Complement
Here’s another easy problem which can be solved by applying logical operators. ...
Keyboard Row
This is another one of those problems that I was able to solve in less than 5 minutes. ...
Power of Two
Another beautiful solution to an easy LC problem. ...
Find Words That Can Be Formed by Characters
For this easy LC problem, the solution is to first, create a map of all the characters in chars and their corresponding count or occurences. We will use occurences later on to count the available characters that can still be used. ...
Happy Number
This easy problem looks simple at first but it gets very tricky specially when finding out if your loop already started in an endless loop. I did try a couple of ideas without using a list to keep track of previous sums but all didn’t work. Eventually I ended up using a mutableList to maintain a list of previous sums so I can look up and see if the new sum has been solved before. If it is, then it means we entered into an endless loop already. ...
Jewels and Stones
This probably is one the shortest solutions I’ve ever written to a problem in LC. ...
Reverse Integer
Another super easy problem using math operations. The problem asks us to reverse the digits of an integer. For example, 123 should return 321. You should also maintain the original sign of the number whether it’s positive or negative. This problem took me about 3 minutes to solve. ...
Best Time to Buy and Sell Stock
This easy LC problem is just a reword or a variation of a problem that finds a pair with the maximum difference. This can be solved in O(n) because we can calculate the mininum number and maximum difference at the same time as we go through each of the array elements. ...
Subsets
The idea to the solution for this medium LC problem is to use bit manipulation to iterate through all possible combination of bits and use this bits to generate the list of subsets. For example, a given array is [1,2,3], you could represent a subset of {1,2,3} as 111, {1,2} as 110, {1} as 100 and so on and so forth. ...
Maximum Subarray
Another easy LC problem. Most solutions will probably have O(n^2) or worst but here’s an another solution which has an O(n) time complexity. ...
Two Sum
This is one of the easiest problem on LC as this has the highest acceptance rate as of now. ...
Maximum Binary Tree
We can solve this medium LC problem by recursively finding the index of the highest value (let’s call it as maxIndex) then splitting the array into two subarrays starting from 0 to maxIndex - 1 and maxIndex + 1 to array’s size - 1. We construct the tree everytime we encounter the max value. ...
Binary Search Tree to Greater Sum Tree
The solution to this medium LC problem involves traversing the tree using inorder traversal. In-order traversal means the left node gets the priority, followed by the root node then right node. ...
Transpose Matrix
Another short solution for this easy LC problem. ...
Single-Row Keyboard
I solved this easy LC problem in less than 5 minutes. Solution is very easy and straightforward. I provided two solutions: One that uses the map operator and another using the good ‘ole for loop. ...
K Closest Points to Origin
To solve this medium LC problem, you must first define a function to calculate the Euclidian distance of two points. In this case, we have to find the distance between a point and the origin (0, 0). ...
Number of Lines to Write String
The solution to this easy LC problem is quite simple and I think the code is also self-explanatory so I won’t say that much about this solution. ...
Remove All Adjacent Duplicates in String
Not the fastest solution for this easy LC problem but it’s readable and easy to understand so I’m fine with this. ...