Leetcode List : https://leetcode.com/list/5v3kl3d1

Here is a list of backtracking problems that can be useful to practice to solve more problems during interview / contest. Backtracking algorithm is straightforward, but when it comes to real problems sometimes it is not obvious how we should tweak the algorithm.

Let's check the basic description and template of backtracking algorithm:

Wikipedia definition: Backtracking is a general algorithm for finding solutions to some computational problems, notably constraint satisfaction problems, that incrementally builds candidates to the solutions, and abandons a candidate ("backtracks") as soon as it determines that the candidate cannot possibly be completed to a valid solution.

void dfs(arguments) {
	if (condition == true) { // Condition when we should stop our exploration.
		result.push_back(current);
		return;
	}
	for (int i = num; i <= last; i++) {
		current.push_back(i); // Explore candidate.
		dfs(arguments);
		current.pop_back();   // Abandon candidate.
	}
}

One thing to remember before we can jump to some backtracking problems:

  1. Permutation: can be thought of number of ways to order some input.
  2. Combnation: can be thought as the number of ways of selecting from some input.
  3. Subset: can be thought as a selection of objects form the original set.

From now let's start to apply this algorithm to solve some backtracking problems.