diff --git a/DIRECTORY.md b/DIRECTORY.md index 477a6ebe..3621a9be 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -1,133 +1,147 @@ - ## Association Algorithms - * [Apriori](https://github.com/TheAlgorithms/R/blob/HEAD/association_algorithms/apriori.r) + +- [Apriori](https://github.com/TheAlgorithms/R/blob/HEAD/association_algorithms/apriori.r) ## Classification Algorithms - * [Decision Tree](https://github.com/TheAlgorithms/R/blob/HEAD/classification_algorithms/decision_tree.r) - * [Gradient Boosting Algorithms](https://github.com/TheAlgorithms/R/blob/HEAD/classification_algorithms/gradient_boosting_algorithms.r) - * [Knn](https://github.com/TheAlgorithms/R/blob/HEAD/classification_algorithms/knn.r) - * [Light Gbm](https://github.com/TheAlgorithms/R/blob/HEAD/classification_algorithms/light_gbm.r) - * [Logistic Regression](https://github.com/TheAlgorithms/R/blob/HEAD/classification_algorithms/logistic_regression.r) - * [Logistic Regression 2](https://github.com/TheAlgorithms/R/blob/HEAD/classification_algorithms/logistic_regression_2.r) - * [Naive Bayes](https://github.com/TheAlgorithms/R/blob/HEAD/classification_algorithms/naive_bayes.r) - * [Random Forest](https://github.com/TheAlgorithms/R/blob/HEAD/classification_algorithms/random_forest.r) - * [Svm](https://github.com/TheAlgorithms/R/blob/HEAD/classification_algorithms/svm.r) - * [Xgboost](https://github.com/TheAlgorithms/R/blob/HEAD/classification_algorithms/xgboost.r) + +- [Decision Tree](https://github.com/TheAlgorithms/R/blob/HEAD/classification_algorithms/decision_tree.r) +- [Gradient Boosting Algorithms](https://github.com/TheAlgorithms/R/blob/HEAD/classification_algorithms/gradient_boosting_algorithms.r) +- [Knn](https://github.com/TheAlgorithms/R/blob/HEAD/classification_algorithms/knn.r) +- [Light Gbm](https://github.com/TheAlgorithms/R/blob/HEAD/classification_algorithms/light_gbm.r) +- [Logistic Regression](https://github.com/TheAlgorithms/R/blob/HEAD/classification_algorithms/logistic_regression.r) +- [Logistic Regression 2](https://github.com/TheAlgorithms/R/blob/HEAD/classification_algorithms/logistic_regression_2.r) +- [Naive Bayes](https://github.com/TheAlgorithms/R/blob/HEAD/classification_algorithms/naive_bayes.r) +- [Random Forest](https://github.com/TheAlgorithms/R/blob/HEAD/classification_algorithms/random_forest.r) +- [Svm](https://github.com/TheAlgorithms/R/blob/HEAD/classification_algorithms/svm.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) - * [Gmm](https://github.com/TheAlgorithms/R/blob/HEAD/clustering_algorithms/gmm.r) - * [Heirarchical Clustering](https://github.com/TheAlgorithms/R/blob/HEAD/clustering_algorithms/heirarchical_clustering.r) - * [K Means](https://github.com/TheAlgorithms/R/blob/HEAD/clustering_algorithms/k_means.r) - * [Kmeans Clustering](https://github.com/TheAlgorithms/R/blob/HEAD/clustering_algorithms/kmeans_clustering.r) - * [Kmeans Raw R](https://github.com/TheAlgorithms/R/blob/HEAD/clustering_algorithms/kmeans_raw_r.r) - * [Pam](https://github.com/TheAlgorithms/R/blob/HEAD/clustering_algorithms/pam.r) + +- [Dbscan Clustering](https://github.com/TheAlgorithms/R/blob/HEAD/clustering_algorithms/dbscan_clustering.r) +- [Gmm](https://github.com/TheAlgorithms/R/blob/HEAD/clustering_algorithms/gmm.r) +- [Heirarchical Clustering](https://github.com/TheAlgorithms/R/blob/HEAD/clustering_algorithms/heirarchical_clustering.r) +- [K Means](https://github.com/TheAlgorithms/R/blob/HEAD/clustering_algorithms/k_means.r) +- [Kmeans Clustering](https://github.com/TheAlgorithms/R/blob/HEAD/clustering_algorithms/kmeans_clustering.r) +- [Kmeans Raw R](https://github.com/TheAlgorithms/R/blob/HEAD/clustering_algorithms/kmeans_raw_r.r) +- [Pam](https://github.com/TheAlgorithms/R/blob/HEAD/clustering_algorithms/pam.r) ## Data Manipulation - * [Label Encode](https://github.com/TheAlgorithms/R/blob/HEAD/data_manipulation/label_encode.r) - * [One Hot Encode](https://github.com/TheAlgorithms/R/blob/HEAD/data_manipulation/one_hot_encode.r) - * [Shorten.Vector](https://github.com/TheAlgorithms/R/blob/HEAD/data_manipulation/shorten.vector.r) + +- [Label Encode](https://github.com/TheAlgorithms/R/blob/HEAD/data_manipulation/label_encode.r) +- [One Hot Encode](https://github.com/TheAlgorithms/R/blob/HEAD/data_manipulation/one_hot_encode.r) +- [Shorten.Vector](https://github.com/TheAlgorithms/R/blob/HEAD/data_manipulation/shorten.vector.r) ## Data Preprocessing - * [Data Normalization Standardization](https://github.com/TheAlgorithms/R/blob/HEAD/data_preprocessing/data_normalization_standardization.r) - * [Data Processing](https://github.com/TheAlgorithms/R/blob/HEAD/data_preprocessing/data_processing.r) - * [Dimensionality Reduction Algorithms](https://github.com/TheAlgorithms/R/blob/HEAD/data_preprocessing/dimensionality_reduction_algorithms.r) - * [K Folds](https://github.com/TheAlgorithms/R/blob/HEAD/data_preprocessing/k_folds.r) - * [Lasso](https://github.com/TheAlgorithms/R/blob/HEAD/data_preprocessing/lasso.r) + +- [Data Normalization Standardization](https://github.com/TheAlgorithms/R/blob/HEAD/data_preprocessing/data_normalization_standardization.r) +- [Data Processing](https://github.com/TheAlgorithms/R/blob/HEAD/data_preprocessing/data_processing.r) +- [Dimensionality Reduction Algorithms](https://github.com/TheAlgorithms/R/blob/HEAD/data_preprocessing/dimensionality_reduction_algorithms.r) +- [K Folds](https://github.com/TheAlgorithms/R/blob/HEAD/data_preprocessing/k_folds.r) +- [Lasso](https://github.com/TheAlgorithms/R/blob/HEAD/data_preprocessing/lasso.r) ## Data Structures - * [Binary Search Tree](https://github.com/TheAlgorithms/R/blob/HEAD/data_structures/binary_search_tree.r) + +- [Binary Search Tree](https://github.com/TheAlgorithms/R/blob/HEAD/data_structures/binary_search_tree.r) ## Dynamic Programming - * 0 - * 0 - * [1 Knapsack Problem](https://github.com/TheAlgorithms/R/blob/HEAD/dynamic_programming/0/0/1_knapsack_problem.r) - * [1 Knapsack Problem](https://github.com/TheAlgorithms/R/blob/HEAD/dynamic_programming/0/1_knapsack_problem.r) - * [Coin Change](https://github.com/TheAlgorithms/R/blob/HEAD/dynamic_programming/coin_change.r) - * [Longest Common Subsequence](https://github.com/TheAlgorithms/R/blob/HEAD/dynamic_programming/longest_common_subsequence.r) - * [Longest Increasing Subsequence](https://github.com/TheAlgorithms/R/blob/HEAD/dynamic_programming/longest_increasing_subsequence.r) + +- 0 + - 0 + - [1 Knapsack Problem](https://github.com/TheAlgorithms/R/blob/HEAD/dynamic_programming/0/0/1_knapsack_problem.r) + - [1 Knapsack Problem](https://github.com/TheAlgorithms/R/blob/HEAD/dynamic_programming/0/1_knapsack_problem.r) +- [Coin Change](https://github.com/TheAlgorithms/R/blob/HEAD/dynamic_programming/coin_change.r) +- [Profitable Schemes](https://github.com/TheAlgorithms/R/blob/HEAD/dynamic_programming/profitable_schemes.r) +- [Longest Common Subsequence](https://github.com/TheAlgorithms/R/blob/HEAD/dynamic_programming/longest_common_subsequence.r) +- [Longest Increasing Subsequence](https://github.com/TheAlgorithms/R/blob/HEAD/dynamic_programming/longest_increasing_subsequence.r) ## Graph Algorithms - * [Breadth First Search](https://github.com/TheAlgorithms/R/blob/HEAD/graph_algorithms/breadth_first_search.r) - * [Depth First Search](https://github.com/TheAlgorithms/R/blob/HEAD/graph_algorithms/depth_first_search.r) - * [Dijkstra Shortest Path](https://github.com/TheAlgorithms/R/blob/HEAD/graph_algorithms/dijkstra_shortest_path.r) - * [Bellman-Ford Shortest Path](https://github.com/TheAlgorithms/R/blob/HEAD/graph_algorithms/bellman_ford_shortest_path.r) + +- [Breadth First Search](https://github.com/TheAlgorithms/R/blob/HEAD/graph_algorithms/breadth_first_search.r) +- [Depth First Search](https://github.com/TheAlgorithms/R/blob/HEAD/graph_algorithms/depth_first_search.r) +- [Dijkstra Shortest Path](https://github.com/TheAlgorithms/R/blob/HEAD/graph_algorithms/dijkstra_shortest_path.r) +- [Bellman-Ford Shortest Path](https://github.com/TheAlgorithms/R/blob/HEAD/graph_algorithms/bellman_ford_shortest_path.r) ## Mathematics - * [Amicable Numbers](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/amicable_numbers.r) - * [Armstrong Number](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/armstrong_number.r) - * [Bisection Method](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/bisection_method.r) - * [Euclidean Distance](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/euclidean_distance.r) - * [Extended Euclidean Algorithm](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/extended_euclidean_algorithm.r) - * [Factorial](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/factorial.r) - * [Fibonacci](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/fibonacci.r) - * [First N Fibonacci](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/first_n_fibonacci.r) - * [Greatest Common Divisor](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/greatest_common_divisor.r) - * [Josephus Problem](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/josephus_problem.r) - * [Least Common Multiple](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/least_common_multiple.r) - * [Perfect Number](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/perfect_number.r) - * [Perfect Square](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/perfect_square.r) - * [Permutation Calculation](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/permutation_calculation.r) - * [Pi Monte Carlo](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/pi_monte_carlo.r) - * [Prime](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/prime.r) - * [Sieve Of Eratosthenes](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/sieve_of_eratosthenes.r) + +- [Amicable Numbers](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/amicable_numbers.r) +- [Armstrong Number](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/armstrong_number.r) +- [Bisection Method](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/bisection_method.r) +- [Euclidean Distance](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/euclidean_distance.r) +- [Extended Euclidean Algorithm](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/extended_euclidean_algorithm.r) +- [Factorial](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/factorial.r) +- [Fibonacci](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/fibonacci.r) +- [First N Fibonacci](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/first_n_fibonacci.r) +- [Greatest Common Divisor](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/greatest_common_divisor.r) +- [Josephus Problem](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/josephus_problem.r) +- [Least Common Multiple](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/least_common_multiple.r) +- [Perfect Number](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/perfect_number.r) +- [Perfect Square](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/perfect_square.r) +- [Permutation Calculation](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/permutation_calculation.r) +- [Pi Monte Carlo](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/pi_monte_carlo.r) +- [Prime](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/prime.r) +- [Sieve Of Eratosthenes](https://github.com/TheAlgorithms/R/blob/HEAD/mathematics/sieve_of_eratosthenes.r) ## Quantitative Finance - * [Kalman Filter](https://github.com/TheAlgorithms/R/blob/HEAD/quantitative_finance/kalman_filter.r) - * [Markowitz Portfolio Optimization](https://github.com/TheAlgorithms/R/blob/HEAD/quantitative_finance/markowitz_portfolio_optimization.r) - * [Monte Carlo Simulation](https://github.com/TheAlgorithms/R/blob/HEAD/quantitative_finance/monte_carlo_simulation.r) + +- [Kalman Filter](https://github.com/TheAlgorithms/R/blob/HEAD/quantitative_finance/kalman_filter.r) +- [Markowitz Portfolio Optimization](https://github.com/TheAlgorithms/R/blob/HEAD/quantitative_finance/markowitz_portfolio_optimization.r) +- [Monte Carlo Simulation](https://github.com/TheAlgorithms/R/blob/HEAD/quantitative_finance/monte_carlo_simulation.r) ## Regression Algorithms - * [Ann](https://github.com/TheAlgorithms/R/blob/HEAD/regression_algorithms/ann.r) - * [Linear Regression](https://github.com/TheAlgorithms/R/blob/HEAD/regression_algorithms/linear_regression.r) - * [Linearregressionrawr](https://github.com/TheAlgorithms/R/blob/HEAD/regression_algorithms/linearregressionrawr.r) - * [Multiple Linear Regression](https://github.com/TheAlgorithms/R/blob/HEAD/regression_algorithms/multiple_linear_regression.r) + +- [Ann](https://github.com/TheAlgorithms/R/blob/HEAD/regression_algorithms/ann.r) +- [Linear Regression](https://github.com/TheAlgorithms/R/blob/HEAD/regression_algorithms/linear_regression.r) +- [Linearregressionrawr](https://github.com/TheAlgorithms/R/blob/HEAD/regression_algorithms/linearregressionrawr.r) +- [Multiple Linear Regression](https://github.com/TheAlgorithms/R/blob/HEAD/regression_algorithms/multiple_linear_regression.r) ## Searches - * [Binary Search](https://github.com/TheAlgorithms/R/blob/HEAD/searches/binary_search.r) - * [Linear Search](https://github.com/TheAlgorithms/R/blob/HEAD/searches/linear_search.r) - * [Rabin.Karp.String.Search](https://github.com/TheAlgorithms/R/blob/HEAD/searches/rabin.karp.string.search.r) - * [Z.Algorithm.Search](https://github.com/TheAlgorithms/R/blob/HEAD/searches/z.algorithm.search.r) + +- [Binary Search](https://github.com/TheAlgorithms/R/blob/HEAD/searches/binary_search.r) +- [Linear Search](https://github.com/TheAlgorithms/R/blob/HEAD/searches/linear_search.r) +- [Rabin.Karp.String.Search](https://github.com/TheAlgorithms/R/blob/HEAD/searches/rabin.karp.string.search.r) +- [Z.Algorithm.Search](https://github.com/TheAlgorithms/R/blob/HEAD/searches/z.algorithm.search.r) ## Sorting Algorithms - * [Binary Insertion Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/binary_insertion_sort.r) - * [Bubble Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/bubble_sort.r) - * [Bucket Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/bucket_sort.r) - * [Cocktail Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/cocktail_sort.r) - * [Comb Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/comb_sort.r) - * [Counting Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/counting_sort.r) - * [Cycle Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/cycle_sort.r) - * [Gnome Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/gnome_sort.r) - * [Heap Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/heap_sort.r) - * [Insertion Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/insertion_sort.r) - * [Merge Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/merge_sort.r) - * [Odd Even Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/odd_even_sort.r) - * [Pancake Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/pancake_sort.r) - * [Patience Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/patience_sort.r) - * [Quick Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/quick_sort.r) - * [Radix Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/radix_sort.r) - * [Selection Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/selection_sort.r) - * [Shell Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/shell_sort.r) - * [Stooge Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/stooge_sort.r) - * [Strand Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/strand_sort.r) - * [Tim Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/tim_sort.r) - * [Topological Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/topological_sort.r) - * [Wiggle Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/wiggle_sort.r) + +- [Binary Insertion Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/binary_insertion_sort.r) +- [Bubble Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/bubble_sort.r) +- [Bucket Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/bucket_sort.r) +- [Cocktail Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/cocktail_sort.r) +- [Comb Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/comb_sort.r) +- [Counting Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/counting_sort.r) +- [Cycle Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/cycle_sort.r) +- [Gnome Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/gnome_sort.r) +- [Heap Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/heap_sort.r) +- [Insertion Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/insertion_sort.r) +- [Merge Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/merge_sort.r) +- [Odd Even Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/odd_even_sort.r) +- [Pancake Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/pancake_sort.r) +- [Patience Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/patience_sort.r) +- [Quick Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/quick_sort.r) +- [Radix Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/radix_sort.r) +- [Selection Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/selection_sort.r) +- [Shell Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/shell_sort.r) +- [Stooge Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/stooge_sort.r) +- [Strand Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/strand_sort.r) +- [Tim Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/tim_sort.r) +- [Topological Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/topological_sort.r) +- [Wiggle Sort](https://github.com/TheAlgorithms/R/blob/HEAD/sorting_algorithms/wiggle_sort.r) ## String Manipulation - * [Burrows](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/burrows.r) - * [Findpalindrome](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/findpalindrome.r) - * [Hamming Distance](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/hamming_distance.r) - * [Is.Anagram](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/is.anagram.r) - * [Is.Lower](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/is.lower.r) - * [Is.Uppercase](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/is.uppercase.r) - * [Levenshtein](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/levenshtein.r) - * [Longest.Palindromic.Subsequence](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/longest.palindromic.subsequence.r) - * [Longest.Substring.No.Repeat](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/longest.substring.no.repeat.r) - * [Manacher.Longest.Palindrome](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/manacher.longest.palindrome.r) - * [Maskwords](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/maskwords.r) - * [Min.Palindromic.Insert](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/min.palindromic.insert.r) - * [Minimum.Window.Substring](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/minimum.window.substring.r) - * [Rearrangeways](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/rearrangeways.r) - * [Shortest.Common.Supersequence](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/shortest.common.supersequence.r) - * [Unique.Letters.Count](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/unique.letters.count.r) + +- [Burrows](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/burrows.r) +- [Findpalindrome](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/findpalindrome.r) +- [Hamming Distance](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/hamming_distance.r) +- [Is.Anagram](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/is.anagram.r) +- [Is.Lower](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/is.lower.r) +- [Is.Uppercase](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/is.uppercase.r) +- [Levenshtein](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/levenshtein.r) +- [Longest.Palindromic.Subsequence](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/longest.palindromic.subsequence.r) +- [Longest.Substring.No.Repeat](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/longest.substring.no.repeat.r) +- [Manacher.Longest.Palindrome](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/manacher.longest.palindrome.r) +- [Maskwords](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/maskwords.r) +- [Min.Palindromic.Insert](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/min.palindromic.insert.r) +- [Minimum.Window.Substring](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/minimum.window.substring.r) +- [Rearrangeways](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/rearrangeways.r) +- [Shortest.Common.Supersequence](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/shortest.common.supersequence.r) +- [Unique.Letters.Count](https://github.com/TheAlgorithms/R/blob/HEAD/string_manipulation/unique.letters.count.r) diff --git a/dynamic_programming/profitable_schemes.r b/dynamic_programming/profitable_schemes.r new file mode 100644 index 00000000..fac6f58d --- /dev/null +++ b/dynamic_programming/profitable_schemes.r @@ -0,0 +1,128 @@ +# Profitable Schemes (LeetCode 879) - Dynamic Programming in R +# +# Problem: +# Given n members, an array group[] where group[i] is the members needed for crime i, +# and an array profit[] where profit[i] is the profit for crime i, count the number +# of schemes such that the total members used <= n and total profit >= minProfit. +# Return the count modulo 1e9+7. +# +# Intuition +# Three-state DP: remaining members (n_remain), starting index (i), and current gain +# (capped at 100). We use memoization to avoid recomputation. A bottom-up version is +# also included for completeness. +# +# Complexity +# Time Complexity: O(n * minProfit * m) where m = length(group) +# Space Complexity: O(n * minProfit * m) for memo table or O(n * minProfit) for bottom-up +# +# Notes +# - We cap gain at 100 because minProfit <= 100 (common constraint). +# - Use modulo 1e9+7 to handle large counts. + +MOD <- as.integer(1e9 + 7) + +# ------------------------------ +# Top-down DP with memoization (mirrors user-provided Java approach) +# ------------------------------ +profitable_schemes_topdown <- function(n, minProfit, group, profit) { + stopifnot(length(group) == length(profit)) + + m <- length(group) + maxGain <- 100L + + # 3D memo: dims [n_remain + 1][m + 1][maxGain + 1] + # Initialize to -1 (uncomputed) + memo <- array(-1L, dim = c(n + 1L, m + 1L, maxGain + 1L)) + + dp <- function(n_remain, start_i, gain) { + if (n_remain < 0L) return(0L) + + # Use min(gain, 100) to keep within bounds + if (gain > maxGain) gain <- maxGain + + cached <- memo[n_remain + 1L, start_i + 1L, gain + 1L] + if (cached != -1L) return(cached) + + poss <- if (gain >= minProfit) 1L else 0L + + if (start_i == m) { + memo[n_remain + 1L, start_i + 1L, gain + 1L] <<- poss + return(poss) + } + + # 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 + } + + memo[n_remain + 1L, start_i + 1L, gain + 1L] <<- poss + poss + } + + dp(n_remain = n, start_i = 0L, gain = 0L) +} + +# ------------------------------ +# Bottom-up DP (classic 2D DP over members and profit) +# dp[people][p] = number of ways to achieve profit p using some subset of crimes +# Iterate crimes and update dp in reverse on people to avoid reuse +# ------------------------------ +profitable_schemes_bottomup <- function(n, minProfit, group, profit) { + stopifnot(length(group) == length(profit)) + + m <- length(group) + maxGain <- 100L + + dp <- matrix(0L, nrow = n + 1L, ncol = maxGain + 1L) + dp[1:(n + 1L), 1L] <- 1L # profit 0 has 1 way (choose nothing) + + for (i in seq_len(m)) { + g <- group[i] + pr <- profit[i] + for (people in n:0) { + newPeople <- people - g + if (newPeople < 0) next + for (p in 0:maxGain) { + newP <- p + pr + if (newP > maxGain) newP <- maxGain + # dp indices are 1-based in R + dp[newPeople + 1L, newP + 1L] <- (dp[newPeople + 1L, newP + 1L] + dp[people + 1L, p + 1L]) %% MOD + } + } + } + + # Sum all dp[people][p] for p >= minProfit + cols <- (minProfit:maxGain) + 1L + total <- sum(dp[, cols, drop = FALSE]) %% MOD + as.integer(total) +} + +# ------------------------------ +# Examples / Demonstration +# ------------------------------ +cat("=== Profitable Schemes (Dynamic Programming) ===\n\n") + +# Example 1: Simple +n1 <- 5L +minProfit1 <- 3L +group1 <- c(2L, 2L) +profit1 <- c(2L, 3L) +cat("Example 1 (Top-down): ", profitable_schemes_topdown(n1, minProfit1, group1, profit1), "\n") +cat("Example 1 (Bottom-up):", profitable_schemes_bottomup(n1, minProfit1, group1, profit1), "\n\n") + +# Example 2: Larger arrays +n2 <- 10L +minProfit2 <- 5L +group2 <- c(2L, 3L, 5L) +profit2 <- c(6L, 7L, 8L) +cat("Example 2 (Top-down): ", profitable_schemes_topdown(n2, minProfit2, group2, profit2), "\n") +cat("Example 2 (Bottom-up):", profitable_schemes_bottomup(n2, minProfit2, group2, profit2), "\n\n") + +# Example 3: Edge cases +cat("Example 3 (Edge) zero minProfit -> counts all subsets within members\n") +cat("Top-down: ", profitable_schemes_topdown(1L, 0L, c(1L), c(1L)), "\n") +cat("Bottom-up: ", profitable_schemes_bottomup(1L, 0L, c(1L), c(1L)), "\n\n") + +cat("Note: For large instances, counts are returned modulo 1e9+7.\n\n")