Skip to content
  • Dashboard
  • Review
  • Go: plan & chat
  • Roadmap
  • Patterns & lessons49
  • Sheets
  • Problems1.5k
  • Ascend
  • Toolkit
  • Core CS + SQL
  • System Design
  • Machine Coding
  • Interview hub
  • Mock interview
  • Companies & OA
  • Leaderboard
  • Battles
  • College Cup
  • People

Keep your progress

Sync across devices with a free account.

Sign upSign in
Patterns & lessons
Sign inSign up
Patterns/Dynamic Programming/1D / Fibonacci-style

Technique · CP 2400+

Connected-Component DP (insertion DP)

Process elements in sorted order, keeping only how many 'components' (open groups, or partial segments of the final permutation) exist. Each new element starts a component, extends one, or merges two, with counts for each choice.

Learn first1D / Fibonacci-styleCombinatorics mod p (nCr, Catalan, stars and bars)
0%
mastery
0/4 solved
Practise nextCoding Company
IdeaPracticePitfalls

Recognise it

The signal

count permutations where adjacent elements must satisfy a relative-size condition

  1. 1Brute force

    Enumerate permutations/partitions: n!.

  2. 2The observation

    The positions of earlier elements do not matter, only how many groups are still open.

  3. 3Why it works

    After the i smallest elements, the state (j open components, cost so far) decides the future. The cost of the open groups grows by j*(a_{i+1} - a_i).

Reach for it when

  • +partition sorted items into teams whose cost is max - min
  • +the final arrangement is built by inserting values in increasing order

Not when

  • ×the condition depends on exact positions, not relative order

Practice

Next

Coding Company

0/4 solved
  • Coding CompanyCSESMedium
  • Counting PermutationsCSESMedium
  • Phoenix and ComputersCodeforces2200
  • Ant ManCodeforces2500

Pitfalls

Misconceptions

  • !forgetting border components in permutation problems
Go deeperresources

Resources

  • DP lecture notesErrichto · video
  • AtCoder Educational DP ContestAtCoder · sheet
  • Introduction to DPUSACO Guide · course
  • Dynamic Programming PatternsLeetCode discuss · article
  • Dynamic Programming playlistAditya Verma · video
  • Bitmask DPUSACO Guide · course
  • Digit DPCodeforces · blog
TodayReviewPatternsProblems