“Brute force” can sound like a lack of technique, but it is often the most important first step in algorithm design. A good brute-force solution does not try things randomly. It defines every candidate, generates each candidate exactly once, and uses the constraints to decide whether the full search is affordable.
The Core Idea
Write brute force first not because it must be the final solution, but because it exposes the candidate space, the success condition, and the complexity baseline.
What Brute Force Means
Complete search has three parts:
- define one candidate;
- generate every candidate systematically;
- test whether each candidate satisfies the problem.
In Chances of Winning, one candidate is a complete assignment of outcomes to all remaining games. If each game has three outcomes and R games remain, there are 3^R candidates.
Step 1: Describe the Search Space
Ask four questions:
| Question | Why it matters |
|---|---|
| What decisions form one candidate? | Defines the state |
| How many choices does each decision have? | Defines the branching factor |
| How many decisions are made? | Defines the depth |
| What makes a candidate successful? | Defines the terminal check |
With b choices at each of d levels, the direct leaf count is often b^d.
Step 2: Compare It with the Constraints
Exponential does not automatically mean impossible. 3^6 is only 729, while 2^60 is beyond direct enumeration. Substitute the actual maximum depth before rejecting a method.
Step 3: Build a Correct Baseline
A direct search often acts as a correctness oracle. It stays close to the statement, is easy to test on tiny cases, and gives an optimized version something trustworthy to compare against. A productive order is:
define candidates -> enumerate all -> verify small cases -> optimize
Loops or Recursion?
Use nested loops when the number and structure of choices are fixed, such as enumerating every pair. Use recursion when the number of decisions varies but each level follows the same pattern.
def search(i):
if i == len(decisions):
return check_complete_candidate()
total = 0
for choice in choices_for(decisions[i]):
apply(choice)
total += search(i + 1)
undo(choice)
return total
The shared-state contract is essential: when a call returns, the state must match its value at entry. That is why apply-recurse-undo must be exact.
Optimize Only After the Baseline Works
Three common improvements are pruning impossible partial candidates, memoizing repeated states, and choosing a better representation such as a frequency array or bitmask. Every optimization must preserve all successful candidates and must not merge states with different futures.
Worked Model: Chances of Winning
- Candidate: one complete assignment of remaining game outcomes.
- Branches: first team wins, second team wins, or tie.
- Depth:
R <= 6remaining games. - Leaves:
3^R <= 729. - Success: the favourite finishes strictly above all three opponents.
Once these five lines are precise, the algorithm is nearly written.
Common Misconceptions
- Brute force means random guessing. It does not; coverage and uniqueness require proof.
- Every exponential algorithm is too slow. The maximum exponent matters.
- Pruning should come first. A correct baseline makes pruning safer to design and test.
- Counting leaves is enough. The work performed at each leaf also matters.
Practice Questions
- How many binary strings of length
nexist? - Why are there
O(n^2)subarrays? - Why are there
n!permutations ofndistinct elements? - Which is smaller,
2^20or10!? - Write one sentence defining a candidate in Chances of Winning.
Questions or feedback?
Have a question about this article or want to suggest an improvement? Send us an email.
