Slope Trick

DP with absolute value costs creates piecewise linear functions. Slope Trick tracks only the slopes.

47 lessons
156 min
Codeforces: 2300-2900LeetCode: 2400-2800

Lessons

1. Intro

Why Slope Trick exists

3m

2. Piecewise Linear Functions

Shape of costs

3m

3. Quiz: Piecewise Linear

Understanding the structure

3m1 problems

4. Vocabulary - Slope-Change Points

Critical breakpoints

3m

5. Heap Representation

Tracking breakpoints

3m

6. Heap Representation - Walkthrough

Storing breakpoints efficiently

4m

7. Quiz: Slope-Change Points

Knowledge check

3m1 problems

8. Quiz: Why Two Heaps

Understanding the structure

3m1 problems

9. Codeforces 713C Sonya and Problem Without a Legend - Problem Statement

CF 713C

3m1 problems

10. Sonya - Transform Trick

Making it non-decreasing

3m

11. Sonya - DP Formulation

Convex function

3m

12. Sonya - Heap Operations

Double-push trick

3m

13. Sonya - Implementation

Full solution

3m1 problems

14. Sonya - Walkthrough

Tracing the heap operations

4m

15. Challenge: Strictly Increasing

Variant with strict inequality

4m

16. Codeforces 865D Buy Low Sell High - Problem Statement

CF 865D

3m1 problems

17. Codeforces 865D Buy Low Sell High - The Idea

Retroactive decisions

3m

18. Codeforces 865D Buy Low Sell High - Why It Works

Slope function analog

3m

19. Codeforces 865D Buy Low Sell High - Implementation

Greedy with heap

3m1 problems

20. Codeforces 865D Buy Low Sell High - Walkthrough

Tracing the greedy insight

4m

21. Quiz: Regret Trick

Understanding the regret mechanism

3m1 problems

22. CSES Increasing Array II - Problem Statement

CSES 2132

3m1 problems

23. CSES - DP Function Shape

Convexity preservation

3m

24. CSES - Heap Mechanics

Maintaining breakpoints

3m

25. CSES - Implementation

Max-heap solution

3m1 problems

26. CSES - Walkthrough

Increasing array variant

4m

27. Prefix Min Operation

Advanced technique

3m

28. Prefix Min Operation - Walkthrough

How prefix min affects the function

4m

29. K-Increasing - Problem Statement

Generalized version

3m

30. Jobs and Deadlines - Problem Statement

Scheduling variant

3m

31. When to Use Slope Trick

Pattern recognition

3m

32. Quiz: When Slope Trick

Recognizing the pattern

3m1 problems

33. Common Mistakes

What to avoid

3m

34. Common Mistakes - Walkthrough

Avoiding mistakes

4m

35. Complexity Analysis

Why it's fast

3m

36. Complexity Analysis - Walkthrough

Understanding the efficiency

4m

37. Making Array Beautiful - Problem Statement

Another variant

3m

38. Challenge: Three Heaps

Non-convex extensions

4m

39. Lazy Propagation

Efficient global shifts

4m

40. Potions - Problem Statement

IOI variant

3m

41. Quiz: Slope Trick vs CHT

Comparing techniques

3m1 problems

42. Practice Strategy

How to improve

3m

43. Pattern - Slope Trick Recognition

Identifying applicable problems

4m

44. Implementation Template

Reusable structure

5m

45. Challenge: Online Queries

Query the function at points

4m

46. What's Next

Preview of Broken Profile DP

4m

47. Section Recap

What we learned

3m

Practice Problems

1.

Covered with full walkthrough in this section.

2.

Covered with full walkthrough in this section.

3.

Covered with full walkthrough in this section.

4.

THE canonical slope trick problem. Make array non-decreasing with minimum cost, using piecewise linear DP.

5.
Buy Low Sell HighCodeforceshard

Stock trading with slope trick. Maintains optimal profit function as piecewise linear.

6.
A New BeginningCodeforceshard

2D slope trick variant. Grid movement problem reduced to piecewise linear optimization.

7.
Moving PointsCodeforceshard

Geometry + inversion counting. Foundation for understanding coordinate transformations in slope trick.

8.
Array PartitionCodeforceshard

Slope trick for partition optimization. Maintaining optimal prefix/suffix with piecewise costs.

9.

Binary search + absolute value. Foundation for slope trick piecewise linear intuition.

10.

Difference array technique. Related slope analysis for piecewise cost functions.

11.

Greedy with heap. Similar heap operations to slope trick slope maintenance.

12.
Meeting Rooms IIILeetCodehard

Heap-based simulation. Understanding priority queue operations helps with slope trick heaps.

Ready to start learning?

Access all 47 lessons with interactive content and progress tracking.