Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Contributor.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
44 changes: 44 additions & 0 deletions dsa_in_java/dynamic programming/1D and 2D DP/CoinChange.java
Original file line number Diff line number Diff line change
@@ -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/
*/
42 changes: 42 additions & 0 deletions dsa_in_java/dynamic programming/1D and 2D DP/CoinChangeII.java
Original file line number Diff line number Diff line change
@@ -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/
*/
Original file line number Diff line number Diff line change
@@ -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/
*/
48 changes: 48 additions & 0 deletions dsa_in_java/dynamic programming/1D and 2D DP/MinimumPathSum.java
Original file line number Diff line number Diff line change
@@ -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/
*/
45 changes: 45 additions & 0 deletions dsa_in_java/dynamic programming/1D and 2D DP/UniquePaths.java
Original file line number Diff line number Diff line change
@@ -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/
*/
53 changes: 53 additions & 0 deletions dsa_in_java/dynamic programming/1D and 2D DP/UniquePathsII.java
Original file line number Diff line number Diff line change
@@ -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/
*/
50 changes: 50 additions & 0 deletions dsa_in_java/dynamic programming/Advanced DP/EditDistance.java
Original file line number Diff line number Diff line change
@@ -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/
*/
47 changes: 47 additions & 0 deletions dsa_in_java/dynamic programming/Advanced DP/Knapsack01.java
Original file line number Diff line number Diff line change
@@ -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/
*/
Loading