RECURSION
CODING QUESTIONS
Flood Filling
An image is represented by an m x n integer grid image where image[i]
[j] represents the pixel value of the image.
You are also given three integers sr, sc, and color. You should perform
a ood ll on the image starting from the pixel image[sr][sc].
To perform a ood ll, consider the starting pixel, plus any pixels
connected 4-directionally to the starting pixel of the same color as the
starting pixel, plus any pixels connected 4-directionally to those pixels
(also with the same color), and so on. Replace the color of all of the
aforementioned pixels with color.
Return the modi ed image after performing the ood ll.
fl
fi
fl
fi
fi
fl
fi
Flood Filling
Combination Sum
Given an array of distinct integers candidates of size N, and a target
integer target, return a list of all unique combinations of candidates where
the chosen numbers sum to target. You may return the combinations
in any order.
The same number may be chosen from candidates an unlimited number
of times. Two combinations are unique if the frequency of at least one of
the chosen numbers is di erent.
Input: Output:
4 [[2, 2, 3], [7]]
2367
7
ff
Special Keyboard
Imagine you have a special keyboard with the following keys:
Key 1: Prints 'A' on screen
Key 2: (Ctrl-A): Select screen
Key 3: (Ctrl-C): Copy selection to bu er
Key 4: (Ctrl-V): Print bu er on screen appending it after what has already
been printed.
Find maximum numbers of A's that can be produced by pressing keys on
the special keyboard N times.
Input: Output:
3 3
ff
ff
Count all possible paths from top
left to bottom right of a mXn matrix
The problem is to count all the possible paths from the top left to the
bottom right of a M X N matrix with the constraints that from each cell
you can either move only to the right or down
Input: Output:
23 3
Josephus Problem
There are N people standing in a circle waiting to be executed. The
counting out begins at some point in the circle and proceeds around the
circle in a xed direction. In each step, a certain number of people are
skipped and the next person is executed. The elimination proceeds around
the circle (which is becoming smaller and smaller as the executed people
are removed), until only the last person remains, who is given freedom.
Given the total number of persons N and a number k which indicates
that k-1 persons are skipped and the kth person is killed in a circle. The
task is to choose the person in the initial circle that survives.
Input: Output:
52 2
fi