Posts
    158 posts
Plus One
For this problem, the approach is really straightforward, just add 1 to each of the digit and making sure that the carry is considered as well. ...
Binary Tree Level Order Traversal II
Today, we’re going to solve a variation of the Binary Tree Level Order Traversal. ...
Arranging Coins
For the month of July, I decided to get back onto "Leetcoding" after almost 2 weeks of hiatus from solving algorithmic problems. Every month since April this year, Leetcode organizes this 30-day challenge where each day, you have to solve 1 problem. For today, we are going to solve a problem called "Arranging Coins". ...
Making Safe Functions
When we write Kotlin functions, it’s important that we make sure that our code is stable and behave correctly. It’s also important to be clear as to what the function expects in order to behave correctly. ...
Good News
It’s been more than a week since I got the result of a 1-year long preparation and I can still feel the excitement, disbelief and nirvana all throughout my body :). I will share more details about it in the upcoming days. ...
Range Sum Query 2D
Here’s a hard LC problem which is actually just a variation of the Range Sum Query problem but instead, you have to work on a 2D data. ...
Filling Bookcase Shelves
Another medium LC problem in which we are asked to arrange the books in a bookshelf with certain width and try to minimize the total height of the books after the arrangement. ...
Largest Values From Labels
For this medium LC problem, I’m presenting two variations of my solution: Using array and another using PriorityQueue. ...
Shortest Distance to Target Color
The solution here for this medium LC problem might not be the fastest but I think it’s really clean and concise and easy to understand. ...
Alphabet Board Path
It took me about 20 minutes to think about the solution for this medium LC problem and just few minutes to actually implement the code. ...
Brace Expansion II
This is a follow-up question for the Brace Expansion problem earlier and the only difference is that now, the braces can be nested multiple times. ...
Brace Expansion
This medium LC problem asks us to return all the words formed by expanding the characters inside the braces and appending it to all the other characters present in the string. ...
Contain All Alphabet
Recently, somebody posted a problem that was asked in his Google Phone Interview. The first problem was easy and could be solved with an O(n) algorithm. 1. Find out if a string contains all alphabet characters in their respective order. ...
Redundant Connection
This medium LC problem is about finding the edge which causes the tree to have a cycle. ...
Connecting Cities With Minimum Cost
This medium LC problem is basically a minimum spanning tree problem therefore we can use Kruskal’s algorithm to solve it. ...
Network Delay Time
This medium LC question can be solved by using Djikstra algorithm. The goal is to find the individual distances of nodes from a given node. Then, the maximum distance will be the total network delay time. The time complexity for this algorithm is O(NlogN + E), N = number of nodes and E = number of edges. ...
Most Stones Removed With Same Row or Column
This medium LC question is really difficult to solve if you don’t have any background about Disjoint Sets. ...
Campus Bikes II
Here’s a medium (problem)[https://leetcode.com/problems/campus-bikes-ii/] which can be solved by using backtracking. Since the problem mentioned about minimizing the sum of distances for a possible combination of workers and bikes, we can use backtracking to go through all possible combinations of bikes and workers. We also keep track of the minimum sum and use that as the final answer. ...
Range Sum Query Immutable
An easy level question with an interesting solution. ...
Character Mapping
This isn’t a solution to a problem but this could be really handy when solving some problems on LC particularly when you need to create a histogram of characters in a particular string. For example, the word “leetcode” will have a histogram of: ...
Set Mismatch
To solve this problem, you need to know the arithmetic progression formula. ...
Invalid Transactions
Starting today, I will try to solve mostly medium level LC questions as I’ve solved about 75% of easy questions. This one seems to be quite easy as it only involves straightforward logic. ...
Word Pattern
The solution to this problem is very easy but I struggled a little bit because I was focusing more on finding a regex to represent the pattern. ...
1-Bit and 2-Bit Characters
The idea to solve this problem is to first, get the total number of bits then iterate through each bit and if the previous bit is 1, then deduct 2 from the total number of bits. If it’s a 0, then deduct only 1. In the end, if the total number of bits is equal to 1, then last character must be a 1-bit character. ...
Search in a Sorted Array of Unknown Size
Honestly, this should be an easy-level problem as it only involves binary search. ...
Binary Watch
As I’ve mentioned before, I really enjoy solving problems involving bit manipulation and this is one of the many problems that are fun to solve with. ...
Valid Word Square
This is a fun problem to solve and also relatively very easy. I was able to solve this with just one try. ...
Binary Tree Level Order Traversal II
The solution for this problem is the same as the other similarly named problem. Except at the end, we just have to reverse the list before returning it. ...
Minimum Depth of Binary Tree
Here’s another shot at a tree traversal problem. This solution uses Depth-First Traversal. ...
Valid Mountain Array
The idea for this problem is to first find the index of the peak. Peak is defined as A[i-1] < A[i] > A[i+1]. ...
Get Equal Substrings Within Budget
The approach for this problem is to use sliding window technique after getting an array of all the costs involved per character. ...
Reverse Bits
This problem does not support Kotlin yet so I have to do it in Java. ...
Minimum Index Sum of Two Lists
The solution for this problem is to loop through any of the list and each time a word exists in both lists, then get the sum of their indexes and use that as the key to store the word in a hashmap. Also, keep track of the lowest key of the map so that later on, you can use that to return the answer. ...
First Unique Character in a String
Most problems like this one can be solved by using the character count map. This approach is very effective as it can give you O(n) time complexity. ...
Majority Element
Writing code in Kotlin brings so much fun particularly when solving Leetcode problems such as this one. ...
Rotated Digits
For this problem, the idea is to swap each digit with their corresponding 180-degree values and use Integer.parseInt to verify if it’s still a valid number. If yes, check also if it’s not equal to the original number as this may also affect the validity of the number. ...
Occurrences After Bigram
Another easy one with a straightforward answer. ...
Merge Intervals
This is one of the commonly asked questions in LC as there were about 22 companies who’ve used this question in their interviews. ...
Minimum Absolute Difference
The idea to solve this problem is to sort the input array and then use a sliding window approach to find the minimum absolute difference between two consecutive elements. If we find a new low, then clear the result and add the current elements. Add more elements if the current diff is equal to the lowest diff so far. ...
How Many Apples Can You Put Into the Basket
Straightforward solution for this easy problem ...
Prime Palindrome
This is one tough medium problem and I had to look at the official solution to solve this. I can solve for smaller numbers but once it gets to 7+ digits then I always get the Time Limit Exceeded result. ...
Excel Sheet Column Number
One of the favorite problems in LC because the problem looks simple but the solution involves quite a bit of analysis. ...
Detect Capital
The easiest way to solve this problem is to count the number of uppercase character. We can easily do that by using map and Character.isUpperCase functions. ...
Meeting Rooms
I never expect that the solution for this problem would be this easy so here it goes. ...
Roman to Integer
I solved this using Java before so this is just a re-implementation in Kotlin language. ...
Two Sum IV - Input Is a BST
This is a fun problem as it really makes you think of how to improve your solution. There are multiple ways of solving this such as iterating through the tree and collecting all the values in a hash set. Then loop through the hash set and find if there are any entry equals to k - current element. ...
Contains-DuplicateII
This is a variation of the previous problem. Again, this is best solved using hashmap. ...
Contains-Duplicate
There are multiple ways to solve this problem. One could sort the arrays and check element by element if two consecutive items are equal. ...
Find the Difference
Just another easy problem. ...
Single Element in a Sorted Array
This medium problem can be solved easily by few lines of code with O(n) time complexity. ...