category-wise-problems

contains category wise problems(data structures, competitive) of popular platforms.

View the Project on GitHub mayankdutta/category-wise-problems

Fruit Feast, Medium

iterative approach
Code sample ```cpp void solve() { int n, a, b; cin >> n >> a >> b; vvi memo = vvi(2, vi(5'000'010, 0)); memo[0][0] = 1; for (int i = 0; i <= n; i++) { if (i >= a) memo[0][i] |= memo[0][i - a]; if (i >= b) memo[0][i] |= memo[0][i - b]; memo[1][i / 2] |= memo[0][i]; } for (int i = 0; i <= n; i++) { if (i >= a) memo[1][i] |= memo[1][i - a]; if (i >= b) memo[1][i] |= memo[1][i - b]; } int ans = 0; for (int i = 0; i <= n; i++) { if (memo[0][i]) ans = max(ans, i); if (memo[1][i]) ans = max(ans, i); } cout << ans << '\n'; } ```
Memoization approach
Code sample ```cpp #define MAXN 5000000 int T, A, B; int memo[MAXN][2]; int recur(int fullness, bool used) { if (memo[fullness][used] > 0) return memo[fullness][used]; if (fullness > T) return 0; int mx = fullness; mx = max(mx, recur(fullness + A, used)); mx = max(mx, recur(fullness + B, used)); if (!used) { mx = max(mx, recur(fullness / 2, true)); } memo[fullness][used] = mx; return mx; } ```