Skip to main content

Algorithm Discovery - The Designer's Brain

Phase 1: The Naive Realization

First instinct: "Let me actually BUILD the subsequences!"

Try to make [1,2,3], then [3,4,5]...
Wait, how do I know where to split?
Which 3 goes where?

Problem: Too many decisions. Exponential possibilities.

Key Insight #1:

"I don't need to BUILD subsequences, I need to PROVE they exist."


Phase 2: Understanding Through Examples

Raw problem reaction: "WTF does this even mean?"

Step 1: Understand through examples

[1,2,3,3,4,5]

What are valid splits?
- [1,2,3] and [3,4,5] ✓
- [1,2,3,4,5] and... wait, no second subsequence
- [1,2] and [3,3,4,5]? No! [1,2] has length < 3 ✗

Requirements:
• Consecutive numbers
• Each group ≥ 3 length

Step 2: The brute force awakening

Naive thought: "Let me try ALL possible ways to split!"

For each number, decide: which subsequence does it go to?

[1,2,3,3,4,5]
↓ ↓ ↓ ↓ ↓ ↓
A A A B B B → try this split
A A A A B B → try this split
...

How many ways? EXPONENTIAL!

Realization: "This is insane. There must be a pattern."


Phase 3: The Crucial Observation

Look at the problem differently:

Instead of "assign numbers to groups," think:

"Each number has a ROLE: either continue someone's chain or start a new chain"

Why think this way?

Because the numbers are sorted and we need consecutive integers!

[1,2,3,3,4,5]
^
1 is here. What's 1's purpose?

Either:
- 1 is the START of some chain
- 1 is the MIDDLE of some chain (extends from 0)
→ but we don't have 0, so 1 MUST start a chain

^
2 is here. What's 2's purpose?

Either:
- 2 starts a NEW chain [2,3,4,...]
- 2 extends an existing chain that has 1 [...,1,2,...]

AHA MOMENT:

"Every number is either a STARTER or an EXTENDER!"


Phase 4: The Greedy Intuition Emerges

Now you have two choices for each number. The question becomes:

"When BOTH options are available, which should I choose?"

Experiment mentally:

[1,2,3,3,4,5]

When I reach the second 3:
Option A: Extend [1,2] → [1,2,3]
Option B: Start new [3,4,5]

If I choose B first:
- Start [3,4,5] ✓
- But now [1,2] is stuck! No 3 left to complete it ✗

If I choose A first:
- Extend [1,2] → [1,2,3] ✓
- Then use the other 3 to start [3,4,5] ✓
- Both work!

The pattern emerges:

"Incomplete chains (length < 3) are LIABILITIES. Complete them ASAP!"

Why prefer extending? Because an incomplete subsequence (length < 3) is a ticking time bomb. We MUST complete it eventually. Better to complete it now while we can.

Key Insight #2:

"Greedy = always extend first, start only if you can't extend"


Phase 5: The Tracking Problem

Now the question: "How do I know if I can extend?"

Failed attempt:

Keep a list of all open subsequences?
[1,2], [3,4], [1,2,3,4,5,6]...
Too messy! How do I find which one ends at x-1?

Breakthrough thought:

"I don't care about WHICH subsequence needs x. I only care about HOW MANY subsequences need x."

If 2 subsequences end at ...x-1
Then 2 subsequences NEED x to continue

Key Insight #3:

"I only need COUNTS, not actual subsequences!"

This gives birth to the need map:

  • need[x] = 3 means "3 subsequences are waiting for x to continue"

Phase 6: The State Machine

The Two Maps

freq[x] = how many x's are still UNUSED (supply)
need[x] = how many subsequences NEED x next (demand)

For each number x:

╔══════════════════════════════════════╗
║ Is there DEMAND for x? ║
║ (Does some subsequence need x?) ║
╚══════════════════════════════════════╝

├── YES (need[x] > 0)
│ │
│ └─→ EXTEND (Priority 1)
│ • Use one x
│ • Satisfy one needer
│ • That chain now needs x+1

└── NO

╔════════════════════════════╗
║ Can I START a new chain? ║
║ (Do x+1 and x+2 exist?) ║
╚════════════════════════════╝

├── YES
│ └─→ START (Priority 2)
│ • Reserve x, x+1, x+2
│ • Create need for x+3

└── NO → FAIL
(x is stuck, can't use it)

Mental Model: Factory Assembly Line

Think of it as a factory assembly line:

Input: Stream of numbers (sorted)
Workers: Subsequences being built

Each number asks:

  1. "Is anyone waiting for me?" → EXTEND that worker's chain
  2. "No? Can I hire 2 friends to start a new team of 3?" → START new chain
  3. "No? I'm useless!" → FAIL

The need map is the waiting list at the factory gate.

The freq map is the inventory of unused parts.