Skip to content

Conversation

@Siddhram
Copy link
Contributor

@Siddhram Siddhram commented Oct 13, 2025

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:

  • State: (n_remain, start_i, gain) with gain capped at 100
  • Modulo 1e9+7
    Bottom-up DP:
  • 2D DP over people and capped profit
  • Modulo 1e9+7
    Example demonstrations included:
  • Simple case
  • Larger arrays
  • Edge case with minProfit = 0
    Clear headers, complexity notes, and consistent style with other DP files.

Complexity
Time Complexity: O(n · minProfit · m)
Space Complexity:

  • Top-down: O(n · minProfit · m)
  • Bottom-up: O(n · minProfit)

Demonstration
Run the script to execute built-in examples:

Windows:

Rscript "R/dynamic_programming/profitable_schemes.r"

- [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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question as in a different PR, don't change formatting

@siriak siriak requested a review from Copilot October 14, 2025 08:04
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment on lines +53 to +58
# 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
}
Copy link

Copilot AI Oct 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
# 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

Copilot uses AI. Check for mistakes.
for (i in seq_len(m)) {
g <- group[i]
pr <- profit[i]
for (people in n:0) {
Copy link

Copilot AI Oct 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
for (people in n:0) {
for (people in seq(n, 0, by = -1)) {

Copilot uses AI. Check for mistakes.
Comment on lines +97 to +98
cols <- (minProfit:maxGain) + 1L
total <- sum(dp[, cols, drop = FALSE]) %% MOD
Copy link

Copilot AI Oct 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
@siriak
Copy link
Member

siriak commented Oct 14, 2025

We do not add Leetcode problems

@siriak siriak closed this Oct 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants