Coin Change

Read the lesson → understand the pattern → then code. Tutor works offline (built-in teacher); optional Cursor/Gemini keys upgrade chat quality.

Topic refresher: Intro DP — Remember Subanswers

Reuse smaller answers via a table or rolling variables.

int a = 1, b = 1;
for (int i = 2; i <= n; i++) {
    int c = a + b;
    a = b; b = c;
}
return b;
LeetCode
3/3
MediumIntro Dynamic ProgrammingLeetCode #322

Coin Change

Fewest coins for amount. dp[x] = min over coin of dp[x-coin]+1.

Examples

Example 1

Input

3 11
1 2 5

Output

3
Example 2

Input

1 3
2

Output

-1

Stuck? Take a nudge

C++17