From e810c80d2ffb77d928fd89441405f3db83434323 Mon Sep 17 00:00:00 2001 From: Kartikeyagupta5 Date: Thu, 30 Oct 2025 22:18:26 +0530 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9C=A8=20Added=20Dynamic=20Programming?= =?UTF-8?q?=20problems=20(Basic,=201D/2D,=20Advanced)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1D and 2D DP/CoinChange.java | 44 ++++++++++++++ .../1D and 2D DP/CoinChangeII.java | 42 ++++++++++++++ .../LongestIncreasingSubsequence.java | 45 +++++++++++++++ .../1D and 2D DP/MinimumPathSum.java | 48 ++++++++++++++++ .../1D and 2D DP/UniquePaths.java | 45 +++++++++++++++ .../1D and 2D DP/UniquePathsII.java | 53 +++++++++++++++++ .../Advanced DP/EditDistance.java | 50 ++++++++++++++++ .../Advanced DP/Knapsack01.java | 47 +++++++++++++++ .../Advanced DP/LongestCommonSubsequence.java | 41 +++++++++++++ .../LongestPalindromicSubsequence.java | 43 ++++++++++++++ .../Advanced DP/PartitionEqualSubsetSum.java | 46 +++++++++++++++ .../Advanced DP/SubsetSum.java | 47 +++++++++++++++ .../basic DP/ClimbingStairs.java | 39 +++++++++++++ .../basic DP/FibonacciDP.java | 57 +++++++++++++++++++ .../basic DP/HouseRobber.java | 38 +++++++++++++ .../basic DP/HouseRobberII.java | 38 +++++++++++++ .../basic DP/MinCostClimbingStairs.java | 36 ++++++++++++ 17 files changed, 759 insertions(+) create mode 100644 dsa_in_java/dynamic programming/1D and 2D DP/CoinChange.java create mode 100644 dsa_in_java/dynamic programming/1D and 2D DP/CoinChangeII.java create mode 100644 dsa_in_java/dynamic programming/1D and 2D DP/LongestIncreasingSubsequence.java create mode 100644 dsa_in_java/dynamic programming/1D and 2D DP/MinimumPathSum.java create mode 100644 dsa_in_java/dynamic programming/1D and 2D DP/UniquePaths.java create mode 100644 dsa_in_java/dynamic programming/1D and 2D DP/UniquePathsII.java create mode 100644 dsa_in_java/dynamic programming/Advanced DP/EditDistance.java create mode 100644 dsa_in_java/dynamic programming/Advanced DP/Knapsack01.java create mode 100644 dsa_in_java/dynamic programming/Advanced DP/LongestCommonSubsequence.java create mode 100644 dsa_in_java/dynamic programming/Advanced DP/LongestPalindromicSubsequence.java create mode 100644 dsa_in_java/dynamic programming/Advanced DP/PartitionEqualSubsetSum.java create mode 100644 dsa_in_java/dynamic programming/Advanced DP/SubsetSum.java create mode 100644 dsa_in_java/dynamic programming/basic DP/ClimbingStairs.java create mode 100644 dsa_in_java/dynamic programming/basic DP/FibonacciDP.java create mode 100644 dsa_in_java/dynamic programming/basic DP/HouseRobber.java create mode 100644 dsa_in_java/dynamic programming/basic DP/HouseRobberII.java create mode 100644 dsa_in_java/dynamic programming/basic DP/MinCostClimbingStairs.java diff --git a/dsa_in_java/dynamic programming/1D and 2D DP/CoinChange.java b/dsa_in_java/dynamic programming/1D and 2D DP/CoinChange.java new file mode 100644 index 0000000..5f7907e --- /dev/null +++ b/dsa_in_java/dynamic programming/1D and 2D DP/CoinChange.java @@ -0,0 +1,44 @@ +/* + * Topic: Coin Change + * Language: Java + * + * Description: + * You are given coins of different denominations and an amount. + * Find the fewest number of coins needed to make up that amount. + * + * Example: + * Input: coins = [1,2,5], amount = 11 + * Output: 3 (11 = 5 + 5 + 1) + */ + +import java.util.*; + +public class CoinChange { + + public static int coinChange(int[] coins, int amount) { + int[] dp = new int[amount + 1]; + Arrays.fill(dp, amount + 1); + dp[0] = 0; + + for (int coin : coins) + for (int i = coin; i <= amount; i++) + dp[i] = Math.min(dp[i], dp[i - coin] + 1); + + return dp[amount] > amount ? -1 : dp[amount]; + } + + public static void main(String[] args) { + int[] coins = {1, 2, 5}; + int amount = 11; + System.out.println("Fewest coins: " + coinChange(coins, amount)); + } +} + +/* + * Output: + * Fewest coins: 3 + * + * Time: O(n*amount) + * Space: O(amount) + * LeetCode: https://leetcode.com/problems/coin-change/ + */ diff --git a/dsa_in_java/dynamic programming/1D and 2D DP/CoinChangeII.java b/dsa_in_java/dynamic programming/1D and 2D DP/CoinChangeII.java new file mode 100644 index 0000000..0d8ae03 --- /dev/null +++ b/dsa_in_java/dynamic programming/1D and 2D DP/CoinChangeII.java @@ -0,0 +1,42 @@ +/* + * Topic: Coin Change II + * Language: Java + * + * Description: + * Count the number of combinations that make up the amount. + * You can use each coin unlimited times. + * + * Example: + * Input: coins = [1,2,5], amount = 5 + * Output: 4 + * Explanation: [5], [2,2,1], [2,1,1,1], [1,1,1,1,1] + */ + +public class CoinChangeII { + + public static int change(int amount, int[] coins) { + int[] dp = new int[amount + 1]; + dp[0] = 1; + + for (int coin : coins) + for (int i = coin; i <= amount; i++) + dp[i] += dp[i - coin]; + + return dp[amount]; + } + + public static void main(String[] args) { + int[] coins = {1,2,5}; + int amount = 5; + System.out.println("Combinations: " + change(amount, coins)); + } +} + +/* + * Output: + * Combinations: 4 + * + * Time: O(n*amount) + * Space: O(amount) + * LeetCode: https://leetcode.com/problems/coin-change-ii/ + */ diff --git a/dsa_in_java/dynamic programming/1D and 2D DP/LongestIncreasingSubsequence.java b/dsa_in_java/dynamic programming/1D and 2D DP/LongestIncreasingSubsequence.java new file mode 100644 index 0000000..839a2b9 --- /dev/null +++ b/dsa_in_java/dynamic programming/1D and 2D DP/LongestIncreasingSubsequence.java @@ -0,0 +1,45 @@ +/* + * Topic: Longest Increasing Subsequence (LIS) + * Language: Java + * + * Description: + * Find the length of the longest increasing subsequence in an array. + * + * Example: + * Input: [10,9,2,5,3,7,101,18] + * Output: 4 (The LIS is [2,3,7,101]) + */ + +import java.util.*; + +public class LongestIncreasingSubsequence { + + public static int lengthOfLIS(int[] nums) { + int n = nums.length; + int[] dp = new int[n]; + Arrays.fill(dp, 1); + int max = 1; + + for (int i = 1; i < n; i++) + for (int j = 0; j < i; j++) + if (nums[i] > nums[j]) + dp[i] = Math.max(dp[i], dp[j] + 1); + + for (int val : dp) max = Math.max(max, val); + return max; + } + + public static void main(String[] args) { + int[] nums = {10,9,2,5,3,7,101,18}; + System.out.println("LIS Length: " + lengthOfLIS(nums)); + } +} + +/* + * Output: + * LIS Length: 4 + * + * Time: O(n^2) + * Space: O(n) + * LeetCode: https://leetcode.com/problems/longest-increasing-subsequence/ + */ diff --git a/dsa_in_java/dynamic programming/1D and 2D DP/MinimumPathSum.java b/dsa_in_java/dynamic programming/1D and 2D DP/MinimumPathSum.java new file mode 100644 index 0000000..162085d --- /dev/null +++ b/dsa_in_java/dynamic programming/1D and 2D DP/MinimumPathSum.java @@ -0,0 +1,48 @@ +/* + * Topic: Minimum Path Sum + * Language: Java + * + * Description: + * Given a grid filled with non-negative numbers, find a path from top-left to bottom-right + * which minimizes the sum of all numbers along its path. + * You can only move right or down. + * + * Example: + * Input: [[1,3,1],[1,5,1],[4,2,1]] + * Output: 7 + */ + +public class MinimumPathSum { + + public static int minPathSum(int[][] grid) { + int m = grid.length, n = grid[0].length; + + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (i == 0 && j == 0) continue; + else if (i == 0) grid[i][j] += grid[i][j - 1]; + else if (j == 0) grid[i][j] += grid[i - 1][j]; + else grid[i][j] += Math.min(grid[i - 1][j], grid[i][j - 1]); + } + } + return grid[m - 1][n - 1]; + } + + public static void main(String[] args) { + int[][] grid = { + {1,3,1}, + {1,5,1}, + {4,2,1} + }; + System.out.println("Min Path Sum: " + minPathSum(grid)); + } +} + +/* + * Output: + * Min Path Sum: 7 + * + * Time: O(m*n) + * Space: O(1) + * LeetCode: https://leetcode.com/problems/minimum-path-sum/ + */ diff --git a/dsa_in_java/dynamic programming/1D and 2D DP/UniquePaths.java b/dsa_in_java/dynamic programming/1D and 2D DP/UniquePaths.java new file mode 100644 index 0000000..abd6214 --- /dev/null +++ b/dsa_in_java/dynamic programming/1D and 2D DP/UniquePaths.java @@ -0,0 +1,45 @@ +/* + * Topic: Unique Paths + * Language: Java + * + * Description: + * A robot is located at the top-left corner of an m x n grid. + * It can move only down or right at any point in time. + * Find the number of possible unique paths to reach the bottom-right corner. + * + * Example: + * Input: m = 3, n = 7 + * Output: 28 + */ + +public class UniquePaths { + + public static int uniquePaths(int m, int n) { + int[][] dp = new int[m][n]; + + // Fill the first row and first column with 1 + for (int i = 0; i < m; i++) dp[i][0] = 1; + for (int j = 0; j < n; j++) dp[0][j] = 1; + + // Fill the DP table + for (int i = 1; i < m; i++) + for (int j = 1; j < n; j++) + dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; + + return dp[m - 1][n - 1]; + } + + public static void main(String[] args) { + int m = 3, n = 7; + System.out.println("Unique Paths: " + uniquePaths(m, n)); + } +} + +/* + * Output: + * Unique Paths: 28 + * + * Time: O(m*n) + * Space: O(m*n) + * LeetCode: https://leetcode.com/problems/unique-paths/ + */ diff --git a/dsa_in_java/dynamic programming/1D and 2D DP/UniquePathsII.java b/dsa_in_java/dynamic programming/1D and 2D DP/UniquePathsII.java new file mode 100644 index 0000000..73535da --- /dev/null +++ b/dsa_in_java/dynamic programming/1D and 2D DP/UniquePathsII.java @@ -0,0 +1,53 @@ +/* + * Topic: Unique Paths II (with obstacles) + * Language: Java + * + * Description: + * Same as Unique Paths, but some cells are blocked (1 = obstacle). + * Find the number of unique paths avoiding obstacles. + * + * Example: + * Input: [[0,0,0],[0,1,0],[0,0,0]] + * Output: 2 + */ + +public class UniquePathsII { + + public static int uniquePathsWithObstacles(int[][] grid) { + int m = grid.length, n = grid[0].length; + if (grid[0][0] == 1) return 0; + + int[][] dp = new int[m][n]; + dp[0][0] = 1; + + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (grid[i][j] == 1) { + dp[i][j] = 0; + } else { + if (i > 0) dp[i][j] += dp[i - 1][j]; + if (j > 0) dp[i][j] += dp[i][j - 1]; + } + } + } + return dp[m - 1][n - 1]; + } + + public static void main(String[] args) { + int[][] grid = { + {0,0,0}, + {0,1,0}, + {0,0,0} + }; + System.out.println("Unique Paths with Obstacles: " + uniquePathsWithObstacles(grid)); + } +} + +/* + * Output: + * Unique Paths with Obstacles: 2 + * + * Time: O(m*n) + * Space: O(m*n) + * LeetCode: https://leetcode.com/problems/unique-paths-ii/ + */ diff --git a/dsa_in_java/dynamic programming/Advanced DP/EditDistance.java b/dsa_in_java/dynamic programming/Advanced DP/EditDistance.java new file mode 100644 index 0000000..7eddce0 --- /dev/null +++ b/dsa_in_java/dynamic programming/Advanced DP/EditDistance.java @@ -0,0 +1,50 @@ +/* + * Topic: Edit Distance + * Language: Java + * + * Description: + * Given two strings word1 and word2, return the minimum number of operations + * required to convert word1 → word2 (Insert, Delete, Replace). + * + * Example: + * Input: word1 = "horse", word2 = "ros" + * Output: 3 + * Explanation: + * horse → rorse (replace 'h' → 'r') + * rorse → rose (remove 'r') + * rose → ros (remove 'e') + */ + +public class EditDistance { + + public static int minDistance(String word1, String word2) { + int m = word1.length(), n = word2.length(); + int[][] dp = new int[m + 1][n + 1]; + + for (int i = 0; i <= m; i++) dp[i][0] = i; + for (int j = 0; j <= n; j++) dp[0][j] = j; + + for (int i = 1; i <= m; i++) + for (int j = 1; j <= n; j++) + if (word1.charAt(i - 1) == word2.charAt(j - 1)) + dp[i][j] = dp[i - 1][j - 1]; + else + dp[i][j] = 1 + Math.min(dp[i - 1][j - 1], + Math.min(dp[i - 1][j], dp[i][j - 1])); + + return dp[m][n]; + } + + public static void main(String[] args) { + System.out.println("Edit Distance: " + minDistance("horse", "ros")); + } +} + +/* + * Output: + * Edit Distance: 3 + * + * Time: O(m*n) + * Space: O(m*n) + * LeetCode: https://leetcode.com/problems/edit-distance/ + */ diff --git a/dsa_in_java/dynamic programming/Advanced DP/Knapsack01.java b/dsa_in_java/dynamic programming/Advanced DP/Knapsack01.java new file mode 100644 index 0000000..da675bc --- /dev/null +++ b/dsa_in_java/dynamic programming/Advanced DP/Knapsack01.java @@ -0,0 +1,47 @@ +/* + * Topic: 0/1 Knapsack Problem + * Language: Java + * + * Description: + * Given weights and values of n items, put these items in a knapsack of capacity W + * to get the maximum total value in the knapsack. + * Each item can be included at most once. + * + * Example: + * Input: values = [60, 100, 120], weights = [10, 20, 30], W = 50 + * Output: 220 + * Explanation: Choose items with weight 20 and 30 → total value = 220 + */ + +public class Knapsack01 { + + public static int knapSack(int W, int[] wt, int[] val, int n) { + int[][] dp = new int[n + 1][W + 1]; + + for (int i = 1; i <= n; i++) { + for (int w = 1; w <= W; w++) { + if (wt[i - 1] <= w) + dp[i][w] = Math.max(val[i - 1] + dp[i - 1][w - wt[i - 1]], dp[i - 1][w]); + else + dp[i][w] = dp[i - 1][w]; + } + } + return dp[n][W]; + } + + public static void main(String[] args) { + int[] val = {60, 100, 120}; + int[] wt = {10, 20, 30}; + int W = 50; + System.out.println("Max Value: " + knapSack(W, wt, val, val.length)); + } +} + +/* + * Output: + * Max Value: 220 + * + * Time: O(n*W) + * Space: O(n*W) + * LeetCode: https://leetcode.com/problems/knapsack-problem/ + */ diff --git a/dsa_in_java/dynamic programming/Advanced DP/LongestCommonSubsequence.java b/dsa_in_java/dynamic programming/Advanced DP/LongestCommonSubsequence.java new file mode 100644 index 0000000..7f5e9a2 --- /dev/null +++ b/dsa_in_java/dynamic programming/Advanced DP/LongestCommonSubsequence.java @@ -0,0 +1,41 @@ +/* + * Topic: Longest Common Subsequence (LCS) + * Language: Java + * + * Description: + * Given two strings, find the length of their longest common subsequence. + * + * Example: + * Input: text1 = "abcde", text2 = "ace" + * Output: 3 (LCS is "ace") + */ + +public class LongestCommonSubsequence { + + public static int longestCommonSubsequence(String text1, String text2) { + int m = text1.length(), n = text2.length(); + int[][] dp = new int[m + 1][n + 1]; + + for (int i = 1; i <= m; i++) + for (int j = 1; j <= n; j++) + if (text1.charAt(i - 1) == text2.charAt(j - 1)) + dp[i][j] = 1 + dp[i - 1][j - 1]; + else + dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]); + + return dp[m][n]; + } + + public static void main(String[] args) { + System.out.println("LCS Length: " + longestCommonSubsequence("abcde", "ace")); + } +} + +/* + * Output: + * LCS Length: 3 + * + * Time: O(m*n) + * Space: O(m*n) + * LeetCode: https://leetcode.com/problems/longest-common-subsequence/ + */ diff --git a/dsa_in_java/dynamic programming/Advanced DP/LongestPalindromicSubsequence.java b/dsa_in_java/dynamic programming/Advanced DP/LongestPalindromicSubsequence.java new file mode 100644 index 0000000..663d4aa --- /dev/null +++ b/dsa_in_java/dynamic programming/Advanced DP/LongestPalindromicSubsequence.java @@ -0,0 +1,43 @@ +/* + * Topic: Longest Palindromic Subsequence + * Language: Java + * + * Description: + * Find the longest palindromic subsequence in a string. + * + * Example: + * Input: "bbbab" + * Output: 4 ("bbbb") + */ + +public class LongestPalindromicSubsequence { + + public static int longestPalindromeSubseq(String s) { + int n = s.length(); + int[][] dp = new int[n][n]; + + for (int i = n - 1; i >= 0; i--) { + dp[i][i] = 1; + for (int j = i + 1; j < n; j++) { + if (s.charAt(i) == s.charAt(j)) + dp[i][j] = 2 + dp[i + 1][j - 1]; + else + dp[i][j] = Math.max(dp[i + 1][j], dp[i][j - 1]); + } + } + return dp[0][n - 1]; + } + + public static void main(String[] args) { + System.out.println("Longest Palindromic Subsequence: " + longestPalindromeSubseq("bbbab")); + } +} + +/* + * Output: + * Longest Palindromic Subsequence: 4 + * + * Time: O(n^2) + * Space: O(n^2) + * LeetCode: https://leetcode.com/problems/longest-palindromic-subsequence/ + */ diff --git a/dsa_in_java/dynamic programming/Advanced DP/PartitionEqualSubsetSum.java b/dsa_in_java/dynamic programming/Advanced DP/PartitionEqualSubsetSum.java new file mode 100644 index 0000000..f593636 --- /dev/null +++ b/dsa_in_java/dynamic programming/Advanced DP/PartitionEqualSubsetSum.java @@ -0,0 +1,46 @@ +/* + * Topic: Partition Equal Subset Sum + * Language: Java + * + * Description: + * Determine if the array can be partitioned into two subsets + * with equal sum. + * + * Example: + * Input: [1,5,11,5] + * Output: true + */ + +public class PartitionEqualSubsetSum { + + public static boolean canPartition(int[] nums) { + int sum = 0; + for (int num : nums) sum += num; + + if (sum % 2 != 0) return false; + int target = sum / 2; + + boolean[] dp = new boolean[target + 1]; + dp[0] = true; + + for (int num : nums) + for (int j = target; j >= num; j--) + dp[j] = dp[j] || dp[j - num]; + + return dp[target]; + } + + public static void main(String[] args) { + int[] nums = {1,5,11,5}; + System.out.println("Can Partition: " + canPartition(nums)); + } +} + +/* + * Output: + * Can Partition: true + * + * Time: O(n*sum) + * Space: O(sum) + * LeetCode: https://leetcode.com/problems/partition-equal-subset-sum/ + */ diff --git a/dsa_in_java/dynamic programming/Advanced DP/SubsetSum.java b/dsa_in_java/dynamic programming/Advanced DP/SubsetSum.java new file mode 100644 index 0000000..e254701 --- /dev/null +++ b/dsa_in_java/dynamic programming/Advanced DP/SubsetSum.java @@ -0,0 +1,47 @@ +/* + * Topic: Subset Sum Problem + * Language: Java + * + * Description: + * Given an array of non-negative integers and a sum, + * determine if there exists a subset with a given sum. + * + * Example: + * Input: nums = [3, 34, 4, 12, 5, 2], sum = 9 + * Output: true (subset [4,5] or [3,2,4]) + */ + +public class SubsetSum { + + public static boolean isSubsetSum(int[] nums, int sum) { + int n = nums.length; + boolean[][] dp = new boolean[n + 1][sum + 1]; + + for (int i = 0; i <= n; i++) dp[i][0] = true; + + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= sum; j++) { + if (nums[i - 1] <= j) + dp[i][j] = dp[i - 1][j] || dp[i - 1][j - nums[i - 1]]; + else + dp[i][j] = dp[i - 1][j]; + } + } + return dp[n][sum]; + } + + public static void main(String[] args) { + int[] nums = {3, 34, 4, 12, 5, 2}; + int sum = 9; + System.out.println("Subset exists: " + isSubsetSum(nums, sum)); + } +} + +/* + * Output: + * Subset exists: true + * + * Time: O(n*sum) + * Space: O(n*sum) + * LeetCode: https://leetcode.com/problems/partition-equal-subset-sum/ + */ diff --git a/dsa_in_java/dynamic programming/basic DP/ClimbingStairs.java b/dsa_in_java/dynamic programming/basic DP/ClimbingStairs.java new file mode 100644 index 0000000..fc8c36e --- /dev/null +++ b/dsa_in_java/dynamic programming/basic DP/ClimbingStairs.java @@ -0,0 +1,39 @@ +/* + * Topic: Climbing Stairs + * Language: Java + * + * Description: + * You are climbing a staircase with n steps. + * You can climb 1 or 2 steps at a time. + * Find how many distinct ways to reach the top. + * + * Example: + * Input: n = 3 + * Output: 3 + * Explanation: (1+1+1), (1+2), (2+1) + */ + +public class ClimbingStairs { + + public static int climbStairs(int n) { + if (n <= 2) return n; + int[] dp = new int[n + 1]; + dp[1] = 1; + dp[2] = 2; + for (int i = 3; i <= n; i++) + dp[i] = dp[i - 1] + dp[i - 2]; + return dp[n]; + } + + public static void main(String[] args) { + int n = 5; + System.out.println("Distinct ways: " + climbStairs(n)); + } +} + +/* + * Output: + * Distinct ways: 8 + * Time: O(n), Space: O(n) + * LeetCode: https://leetcode.com/problems/climbing-stairs/ + */ diff --git a/dsa_in_java/dynamic programming/basic DP/FibonacciDP.java b/dsa_in_java/dynamic programming/basic DP/FibonacciDP.java new file mode 100644 index 0000000..c09e9c1 --- /dev/null +++ b/dsa_in_java/dynamic programming/basic DP/FibonacciDP.java @@ -0,0 +1,57 @@ +/* + * Topic: Fibonacci Number (Top-down & Bottom-up) + * Language: Java + * + * Description: + * Compute the nth Fibonacci number using both + * - Top-down (Recursion + Memoization) + * - Bottom-up (Tabulation) + * + * Example: + * Input: n = 6 + * Output: 8 + * + * Explanation: + * Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8 + * F(6) = 8 + */ + +import java.util.*; + +public class FibonacciDP { + + // Top-down approach (Memoization) + public static int fibTopDown(int n, int[] dp) { + if (n <= 1) return n; + if (dp[n] != -1) return dp[n]; + dp[n] = fibTopDown(n - 1, dp) + fibTopDown(n - 2, dp); + return dp[n]; + } + + // Bottom-up approach (Tabulation) + public static int fibBottomUp(int n) { + if (n <= 1) return n; + int[] dp = new int[n + 1]; + dp[0] = 0; dp[1] = 1; + for (int i = 2; i <= n; i++) dp[i] = dp[i - 1] + dp[i - 2]; + return dp[n]; + } + + public static void main(String[] args) { + int n = 6; + int[] dp = new int[n + 1]; + Arrays.fill(dp, -1); + System.out.println("Top-down: " + fibTopDown(n, dp)); + System.out.println("Bottom-up: " + fibBottomUp(n)); + } +} + +/* + * Output: + * Top-down: 8 + * Bottom-up: 8 + * + * Time Complexity: O(n) + * Space Complexity: O(n) + * LeetCode: https://leetcode.com/problems/fibonacci-number/ + */ diff --git a/dsa_in_java/dynamic programming/basic DP/HouseRobber.java b/dsa_in_java/dynamic programming/basic DP/HouseRobber.java new file mode 100644 index 0000000..2f38c51 --- /dev/null +++ b/dsa_in_java/dynamic programming/basic DP/HouseRobber.java @@ -0,0 +1,38 @@ +/* + * Topic: House Robber + * Language: Java + * + * Description: + * You cannot rob two adjacent houses. + * Find the maximum amount of money you can rob. + * + * Example: + * Input: [2,7,9,3,1] + * Output: 12 + * Explanation: Rob houses 2 and 9 = 12 + */ + +public class HouseRobber { + + public static int rob(int[] nums) { + int n = nums.length; + if (n == 1) return nums[0]; + int[] dp = new int[n]; + dp[0] = nums[0]; + dp[1] = Math.max(nums[0], nums[1]); + for (int i = 2; i < n; i++) + dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]); + return dp[n - 1]; + } + + public static void main(String[] args) { + int[] nums = {2,7,9,3,1}; + System.out.println("Max amount: " + rob(nums)); + } +} + +/* + * Output: Max amount: 12 + * Time: O(n), Space: O(n) + * LeetCode: https://leetcode.com/problems/house-robber/ + */ diff --git a/dsa_in_java/dynamic programming/basic DP/HouseRobberII.java b/dsa_in_java/dynamic programming/basic DP/HouseRobberII.java new file mode 100644 index 0000000..4892599 --- /dev/null +++ b/dsa_in_java/dynamic programming/basic DP/HouseRobberII.java @@ -0,0 +1,38 @@ +/* + * Topic: House Robber II + * Language: Java + * + * Description: + * Houses form a circle. You cannot rob adjacent houses. + * Solve by splitting into two linear subproblems. + */ + +public class HouseRobberII { + + public static int rob(int[] nums) { + int n = nums.length; + if (n == 1) return nums[0]; + return Math.max(robLinear(nums, 0, n - 2), robLinear(nums, 1, n - 1)); + } + + private static int robLinear(int[] nums, int start, int end) { + int prev1 = 0, prev2 = 0; + for (int i = start; i <= end; i++) { + int temp = Math.max(prev1, prev2 + nums[i]); + prev2 = prev1; + prev1 = temp; + } + return prev1; + } + + public static void main(String[] args) { + int[] nums = {2,3,2}; + System.out.println("Max amount (circular): " + rob(nums)); + } +} + +/* + * Output: Max amount (circular): 3 + * Time: O(n), Space: O(1) + * LeetCode: https://leetcode.com/problems/house-robber-ii/ + */ diff --git a/dsa_in_java/dynamic programming/basic DP/MinCostClimbingStairs.java b/dsa_in_java/dynamic programming/basic DP/MinCostClimbingStairs.java new file mode 100644 index 0000000..ea82f95 --- /dev/null +++ b/dsa_in_java/dynamic programming/basic DP/MinCostClimbingStairs.java @@ -0,0 +1,36 @@ +/* + * Topic: Min Cost Climbing Stairs + * Language: Java + * + * Description: + * You can climb either 1 or 2 steps. Each step has a cost. + * Return the minimum cost to reach the top. + * + * Example: + * Input: cost = [10,15,20] + * Output: 15 + */ + +public class MinCostClimbingStairs { + + public static int minCostClimbingStairs(int[] cost) { + int n = cost.length; + int[] dp = new int[n + 1]; + dp[0] = 0; + dp[1] = 0; + for (int i = 2; i <= n; i++) + dp[i] = Math.min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]); + return dp[n]; + } + + public static void main(String[] args) { + int[] cost = {10, 15, 20}; + System.out.println("Min cost: " + minCostClimbingStairs(cost)); + } +} + +/* + * Output: Min cost: 15 + * Time: O(n), Space: O(n) + * LeetCode: https://leetcode.com/problems/min-cost-climbing-stairs/ + */ From 90ddad12101237106b7b24cae1afcbfdc64402f4 Mon Sep 17 00:00:00 2001 From: Kartikeyagupta5 Date: Thu, 30 Oct 2025 22:21:48 +0530 Subject: [PATCH 2/2] Add in Contributer.md --- Contributor.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Contributor.md b/Contributor.md index 2dee167..b1bf8ef 100644 --- a/Contributor.md +++ b/Contributor.md @@ -229,6 +229,7 @@ Added solution for custom range slider - Added Queue Questions (Implement Stack using Queues, Sliding Window Maximum) from LeetCode. - Added String Questions (Valid Anagram, Longest Palindromic Substring, Group Anagrams) from LeetCode. - Added Hashing Questions (Two Sum, Group Anagrams, Longest Consecutive Sequence) from LeetCode. + - Added Dynamic Programming Questions (Climbing Stairs, House Robber, Coin Change) and topic like Basic DP, 1D & 2D DP and Advanced DP from LeetCode. 39. Rudra Pratap Singh | [LinkedIn Profile](https://www.linkedin.com/in/rudra-pratap-singh-0974a92b5/) | [GitHub Profile](https://github.com/Rudra2637) - Added new backtracking problems (Generate Parenthesis,Letter Combination,Rat in a maze problem) to the backtracking section under the dsa_in_cpp folder.