Accel Thinking

Python Learning Notes

Why Should You Write Brute Force First? Define the Complete Search Space

Define every candidate, estimate the full search space, build a correct baseline, and only then add pruning or memoization.

“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:

  1. define one candidate;
  2. generate every candidate systematically;
  3. 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

Once these five lines are precise, the algorithm is nearly written.

Common Misconceptions

  1. Brute force means random guessing. It does not; coverage and uniqueness require proof.
  2. Every exponential algorithm is too slow. The maximum exponent matters.
  3. Pruning should come first. A correct baseline makes pruning safer to design and test.
  4. Counting leaves is enough. The work performed at each leaf also matters.

Practice Questions

  1. How many binary strings of length n exist?
  2. Why are there O(n^2) subarrays?
  3. Why are there n! permutations of n distinct elements?
  4. Which is smaller, 2^20 or 10! ?
  5. Write one sentence defining a candidate in Chances of Winning.