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/State Machine DP (Stocks)

Technique

Divide & Conquer DP Optimisation

For dp[i][j] = min_k dp[i-1][k] + C(k, j) with opt(i, j) non-decreasing in j, compute each row by recursing on the middle j with a narrowed k range.

Learn first1D / Fibonacci-styleDivide and Conquer (merge-sort counting)
0%
mastery
0/2 solved
Practise nextSubarray Squares
IdeaPracticePitfalls

Recognise it

The signal

split an array into K groups minimising sum of group costs

  1. 1Brute force

    Try every k for every (i, j): O(K n2).

  2. 2The observation

    Scanning all split points: monotone opt bounds the search range.

  3. 3Why it works

    opt(i, j) <= opt(i, j+1).

Reach for it when

  • +cost satisfies the quadrangle inequality

Not when

  • ×opt not monotone
  • ×1D recurrence with linear cost (cht may be better)

Practice

Next

Subarray Squares

0/2 solved
  • Subarray SquaresCSESHard
  • Houses and SchoolsCSESHard

Pitfalls

Misconceptions

  • !assuming monotonicity without checking the quadrangle inequality
Go deeper1 combinations · resources

Combinations

  • Divide & Conquer DP Optimisation+Prefix Sum

    split array into k parts minimising sum of squared/segment costs

    Divide & Conquer DP Optimisation:
    monotone split points per layer
    Prefix Sum:
    O(1) segment cost

    Subarray Squares

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