Add Profitable Schemes dynamic programming solutions in R#206
Add Profitable Schemes dynamic programming solutions in R#206Siddhram wants to merge 1 commit intoTheAlgorithms:masterfrom Siddhram:add-profitable_schemes
Conversation
| - [Xgboost](https://github.com/TheAlgorithms/R/blob/HEAD/classification_algorithms/xgboost.r) | ||
|
|
||
| ## Clustering Algorithms | ||
| * [Dbscan Clustering](https://github.com/TheAlgorithms/R/blob/HEAD/clustering_algorithms/dbscan_clustering.r) |
There was a problem hiding this comment.
Same question as in a different PR, don't change formatting
There was a problem hiding this comment.
Pull Request Overview
This PR adds a comprehensive implementation of the Profitable Schemes dynamic programming problem (LeetCode 879) to the R algorithms repository. The implementation includes both top-down memoized and bottom-up DP approaches for counting profitable schemes using multi-dimensional states and modular arithmetic.
Key Changes
- Implements two dynamic programming solutions with memoization and bottom-up approaches
- Adds comprehensive documentation including complexity analysis and algorithm explanation
- Includes multiple test examples demonstrating various scenarios and edge cases
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| dynamic_programming/profitable_schemes.r | New algorithm implementation with dual DP approaches and examples |
| DIRECTORY.md | Updated to include the new Profitable Schemes entry in the Dynamic Programming section |
| # Try all choices from start_i to end (subset enumeration by advancing index) | ||
| for (take in start_i:(m - 1L)) { | ||
| newGain <- gain + profit[take + 1L] | ||
| if (newGain > maxGain) newGain <- maxGain | ||
| poss <- (poss + dp(n_remain - group[take + 1L], take + 1L, newGain)) %% MOD | ||
| } |
There was a problem hiding this comment.
The loop logic doesn't properly implement subset enumeration. This attempts to take all items from start_i onwards rather than considering each item as a take/skip decision. The loop should either iterate through all items with take/skip choices or implement a different recursive pattern.
| # Try all choices from start_i to end (subset enumeration by advancing index) | |
| for (take in start_i:(m - 1L)) { | |
| newGain <- gain + profit[take + 1L] | |
| if (newGain > maxGain) newGain <- maxGain | |
| poss <- (poss + dp(n_remain - group[take + 1L], take + 1L, newGain)) %% MOD | |
| } | |
| # Subset enumeration: take/skip current crime at start_i | |
| # Skip current crime | |
| poss <- (poss + dp(n_remain, start_i + 1L, gain)) %% MOD | |
| # Take current crime | |
| newGain <- gain + profit[start_i + 1L] | |
| if (newGain > maxGain) newGain <- maxGain | |
| poss <- (poss + dp(n_remain - group[start_i + 1L], start_i + 1L, newGain)) %% MOD |
| for (i in seq_len(m)) { | ||
| g <- group[i] | ||
| pr <- profit[i] | ||
| for (people in n:0) { |
There was a problem hiding this comment.
The loop iterates from n to 0, but R sequences like n:0 when n=0 will create c(0, -1, -2, ..., 0) which is incorrect. Use seq(n, 0, by=-1) or add a condition to handle the n=0 case properly.
| for (people in n:0) { | |
| for (people in seq(n, 0, by = -1)) { |
| cols <- (minProfit:maxGain) + 1L | ||
| total <- sum(dp[, cols, drop = FALSE]) %% MOD |
There was a problem hiding this comment.
When minProfit > maxGain (100), this creates an invalid range that will cause an error. The code should handle cases where minProfit exceeds the capped gain limit by returning 0.
|
We do not add Leetcode problems |
This PR introduces a complete and well-documented implementation of the Profitable Schemes problem (LeetCode 879) in R.
Overview
The implementation provides solutions for counting profitable schemes using dynamic programming. Both top-down memoized and bottom-up DP approaches are included, handling multi-dimensional states and modular arithmetic.
Features
Top-down DP with memoization:
Bottom-up DP:
Example demonstrations included:
Clear headers, complexity notes, and consistent style with other DP files.
Complexity
Time Complexity: O(n · minProfit · m)
Space Complexity:
Demonstration
Run the script to execute built-in examples:
Windows:
Rscript "R/dynamic_programming/profitable_schemes.r"