Posts

Showing posts from 2025

Snakes and Ladders

Introduction Graph problems pervade computer science, and algorithms to working with them are fundamental to the field. Hundreds of interesting computational problems are couched in terms of graphs. Today let’s discuss graph algorithms. In fact, I like this problem so much because it reminds me of my childhood. We were playing this game during our school holidays with our friends and it was fun. Let us get into the details next. Problem Statement You are given an $n \times n$ integer matrix board where the cells are labeled from $1$ to $n^2$ in a Boustrophedon style starting from the bottom left of the board (i.e. $board[n - 1][0]$) and alternating direction each row. You start on square $1$ of the board. In each move, starting from square curr, do the following: Choose a destination square next with a label in the range $[curr + 1, min(curr + 6, n^2)]$ This choice simulates the result of a standard 6-sided die roll: i.e., there are always at most 6 destinations, regardless of the siz...

Rabbits in Forest

Image
Today, let’s go to the forest and conduct a survey. This would be an interesting task. We specifically choose rabbits and when we meet one, we ask him, hey How many rabbits of your color have you seen in this forest? In a hypothetical world where you know how to communicate with a rabbit, it would be an amazing activity. Now, let’s get into the problem first. Problem Statement There is a forest with an unknown number of rabbits. We asked $n$ rabbits "How many rabbits have the same color as you?" and collected the answers in an integer array $answers$ where $answers[i]$ is the answer of the $i^{th}$ rabbit. Given the array $answers$, return the minimum number of rabbits that could be in the forest. [1] Example 1: Input: answers = [1,1,2] Output: 5 Explanation: The two rabbits that answered "1" could both be the same color, say red. The rabbit that answered "2" can't be red or the answers would be inconsistent. Say the rabbit that answered "2" ...