Recently I’ve got to solve a problem on LeetCode, and decided to write this article because, even though it was not extremely hard, the whole complexity was in the precomputation of some data. Let’s have a look:
A good meal is a meal that contains exactly two different food items with a sum of deliciousness equal to a power of two.
You can pick any two different foods to make a good meal.
Given an array of integers
deliciousness[i]is the deliciousness of the
ith item of food, return the number of different good meals you can make from this list modulo
109 + 7. …
I’ve recently come across one of the thousands of problems that can be solved with dynamic programming techniques. I remember that, when I was trying to get my head around this topic, one of the most frustrating aspects was the ever-green Fibonacci Numbers example. It was everywhere, but I had the impression that reading over and over the same stories about overlapping subproblems and optimal substructures, always with the same example and the same images in from of my eyes wasn’t really paying off the effort. …
In this article, we are going to study and solve the medium difficulty LeetCode challenge Stone Game VII. It’s a sort of extension of the previous Stone Game VI, already analyzed and solved in this previous article. Let’s get started with the problem description, the examples, the analysis of the problem, and eventually the code.
First off, let me ctrl-c ctrl-v the problem description:
Alice and Bob take turns playing a game, with Alice starting first.
nstones arranged in a row. On each player's turn, they can remove either the leftmost stone or the rightmost stone from the row and receive points equal to the sum of the remaining stones' values in the row. …
Throughout this article, we are going to be analyzing an interesting coding challenge that I’ve recently happened to solve. Those of you who love problem visualization as I do might end up being disappointed because this time I didn’t manage to come up with a satisfying visualization of the problem, however, I’ll try my best to run you through the thoughts that led me to the solution.
Alice and Bob take turns playing a game, with Alice starting first. …
In this article, we will be going through the solution to a coding challenge that I’ve recently had the pleasure to solve taking one of the LeetCode contests. Other than the solution itself, I’ll be providing an easy visualization that hopefully will make everything really simple to grasp.
I’m not going to spend much time here, let me just copy-paste the description of this problem titled “Sum of Absolute Differences in a Sorted Array”
You are given an integer array
numssorted in non-decreasing order.
Build and return an integer array
resultwith the same length as
result[i]is equal to the summation of absolute differences between
nums[i]and all the other elements in the array. …
Recently I’ve got to spend some hours thinking of a useful visualization for a problem whose key point is a pretty straightforward intuition. I am a stronger believer in visualization to help make a concept stick. That being said, I have got to be honest, am not sure if I pulled it off this time, but eventually came up with something that I liked. Let me know your thoughts.
You are given an array
tasks[i] = [actuali…
In this article, we will be solving and discussing the solution to one of the problems that I have recently got to work on. I reckon that it deserves some attention, not for its complexity, but because it requires the developer to do a little analysis and precomputation on the input data to pave the way for an easy solution.
I have recently come across this problem while brushing up my algorithmic skills by solving a coding contest on LeetCode. It is titled “Ways to Make a Fair Array” and the description states:
You are given an integer array
nums. You can choose exactly one index (0-indexed) and remove the element. Notice that the index of the elements may change after the removal. …
In this article, we are going to be discussing an algorithm that lends itself to visualization. We will get started from a concrete problem, a code challenge that I’ve recently got to solve, trying to figure out a general approach that can be reused in many other circumstances.
This is a medium difficulty LeetCode problem called “Split a String Into the Max Number of Unique Substrings”. The description is the following:
Given a string
s, return the maximum number of unique substrings that the given string can be split into.
You can split string
sinto any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique. …
In this article, I’ll be trying to describe a rather common Dynamic Programming technique used to solve an interesting coding challenge taken from one of the contests that you can find on LeetCode.
As DP (Dynamic Programming) can be a tricky concept, sometimes hard to grasp, I’ve endeavored to produce some pictures which will help to get the point across.
You are the manager of a basketball team. For the upcoming tournament, you want to choose the team with the highest overall score. The score of the team is the sum of scores of all the players in the team. …
In this article, we are going to analyze part of the fascinating Binary Tree world. The applications of this data structure are limitless, and even though it might be a bit scary at first, I promise that with the help of some pictures and visualizations, we will be getting acquainted with it, and hopefully learning something new.
As usual, I like to start from a practical problem, why not the coding challenge which got me to write this post? So let’s kick this off.