DP
DP
**********************************1******************** //
word break is done but has confusion not aadded in this list solved in gfg
// "static void main" must be defined in a public class.
}
System.out.print(dp[n]);
}
}
*******************************2********************
coin change possible ways to to distribute 3 coins 1,2,3 to pay 4 amount
Input:
n = 4 , m = 3
S[] = {1,2,3}
Output: 4
Explanation: Four Possible ways are:
{1,1,1,1},{1,1,2},{2,2},{1,3}.
class Solution {
public long count(int S[], int coins, int amount) {
// code here.
long dp[]=new long[amount+1];
dp[0]=1;
for(int i=0;i<S.length;i++){
for(int j=S[i];j<dp.length;j++){
dp[j]+=dp[j-S[i]];
}
}
return dp[amount];
}
}
*****************************3*********************
leetcode buy stock and sell easy level
class Solution {
public int maxProfit(int[] A) {
int min_value=Integer.MAX_VALUE;
int least_value=0;
int max_profit=0;
for(int i=0;i<A.length;i++){
if(A[i]<min_value){
min_value=A[i];
}
least_value=A[i]-min_value;
if(least_value>max_profit){
max_profit=least_value;
}
}
return max_profit;
****************************4*****************************
Stickler thief geeks for geeks solution is bit simple... problem is bit
confusion you will understand by seeing the solution
class Solution
{
//Function to find the maximum money the thief can get.
public int FindMaxSum(int arr[], int n)
{
// Your code here
int b[]=new int[n];
if(n==0){
return 0;
}
if(n==1){
return arr[0];
}
if(n==2){
return Math.max(arr[0],arr[1]);
}
b[0]=arr[0];
b[1]=Math.max(arr[0],arr[1]);
for(int i=2;i<arr.length;i++){
b[i]=Math.max(arr[i]+b[i-2],b[i-1]);
}
return b[n-1];
}
}
***************************************************5***************** 0/1
knapsack with recursion doesn't pass the all test cases in gfg 1100/1200 but for
keep in mind
class Solution
{
//Function to return max value that can be put in knapsack of capacity W.
static int max(int a, int b)
{
return (a > b) ? a : b;
}
static int knapSack(int W, int wt[], int val[], int n) //Start
{
// your code here
if(n==0|| W==0){
return 0;
}
if(wt[n-1]<=W){
return max(val[n-1]+knapSack(W-wt[n-1],wt,val,n-1),knapSack(W,wt,val,n-
1));
}
else
return knapSack(W,wt,val,n-1);
}
}
class Solution
{
//Function to return max value that can be put in knapsack of capacity W.
static int max(int a, int b)
{
return (a > b) ? a : b;
}
static int knapSack(int W, int wt[], int val[], int n)
{
// your code here
int K[][]=new int[n+1][W+1];
for(int i =0;i<=n;i++){
// for(int w=0;w<=W;w++){
K[i][0]=1;
// }
}
for(int i=0;i<=n;i++)
{
for(int j=0;j<=W;j++)
{
if(i==0|| j==0){
K[i][j]=0;
}
else if(wt[i-1]>j){
K[i][j]=K[i-1][j];
}
else{
K[i][j]=max(val[i-1]+K[i-1][j-wt[i-1]],K[i-1][j]); for
unbounded knapsack everything is same except i-1 K[i][j]=max(val[i-1]+K[i][j-
wt[i-1]],K[i-1][j]);
}
}
}
****************************6***********************
fibonnci geeks for geeks why %100000007 do research but for
brief bcz value exceeds its limits that's why we are doing mod
**************************7***************
subset sum problem
class Solution{
boolean dp[][];
if(arr[i-1]<=j){
dp[i][j]=dp[i-1][j-arr[i-1]] || dp[i-1][j];
}
else{
dp[i][j]=dp[i-1][j];
}
}
}
return dp[N][sum];
}
************************8******************
Target sum leetcode still we can optimized O(2^n)
class Solution {
int count=0;
public int findTargetSumWays(int[] nums, int target) {
helper(nums,target,0,0);
return count;
}
void helper(int[] nums,int target,int start,int sum){
if(start==nums.length){
if(sum==target){
count++;
}
return;
}
helper(nums,target,start+1,sum+nums[start]);
helper(nums,target,start+1,sum-nums[start]);
//Remeber we are subtracting
}
}
road cutting
**********************9***********************
class Solution{
for(int i =0;i<=n;i++){
// for(int w=0;w<=W;w++){
K[i][0]=1;
// }
}
for(int i=0;i<=n;i++)
{
for(int j=0;j<=n;j++)
{
if(i==0|| j==0){
K[i][j]=0;
}
else if(length[i-1]>j){
K[i][j]=K[i-1][j];
}
else{
K[i][j]=max(price[i-1]+K[i][j-length[i-1]],K[i-1][j]); Everything
is same we change value with price array and added a new variable called length in
place of wt
}
}
}
return K[n][n];
}
}
**********************10*******************
Target sum dp soultuion
leetcode
class Solution {
int count=0;
public int findTargetSumWays(int[] arr, int sum) {
int n=arr.length;
int target=0;
for(int i=0;i<n;i++)
target+=arr[i];
if((target-sum)%2==1 || (sum>target))
return 0;
sum=(sum+target)/2;
if(sum<0)
return 0;
int t[][]=new int [n + 1][sum + 1]; // DP - matrix
// initialization
// here we are setting 1st row and 1st column
// i denotes the size of the array
// j denotes the target sum (subset sum)
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= sum; j++) {
if (i == 0) // when array(i) is empty than there is no meaning of
sum of elements so return count of subset as 0;
t[i][j] = 0;
if (j == 0) // when sum(j) is zero and there is always a chance
of empty subset so return count as 1;
t[i][j] = 1;
}
}
return t[n][sum];
}
}
****************************11******************************
Gold mine geeks for geeks very easy pepcoding solution
class Solution{
static int maxGold(int n, int m, int arr[][])
{
// code here
if(n==1)
{
int sum=0;
for(int i=m-1;i>=0;i--)
sum+=arr[0][i];
return sum;
}
for(int j=arr[0].length-1;j>=0;j--){
for(int i=arr.length-1;i>=0;i--){
if(j==arr[0].length-1){
dp[i][j]=arr[i][j];
}
else if(i==arr.length-1){
dp[i][j]=arr[i][j]+Math.max(dp[i][j+1],dp[i-1][j+1]);
} else if(i==0){
dp[i][j]=arr[i][j]+Math.max(dp[i][j+1],dp[i+1][j+1]);
}
else{
dp[i][j]=arr[i][j]+Math.max(dp[i][j+1],Math.max(dp[i+1]
[j+1],dp[i-1][j+1]));
}
}
}
int max=dp[0][0];
for(int i=1;i<dp.length;i++){
if( dp[i][0]>max){
max=dp[i][0];
}
}
return max;
}
}
***********************************12***************************
Longest Increasing Subsequence
Input:
N = 16
A[]={0,8,4,12,2,10,6,14,1,9,5
13,3,11,7,15}
Output: 6
Explanation:Longest increasing subsequence doubt
0 2 6 9 13 15, which has length 6
class Solution
{
//Function to find length of longest increasing subsequence.
static int longestSubsequence(int n, int a[])
{
// code here
int omax=0;
int dp[]=new int[n];
dp[0]=1;
for(int i=1;i<n;i++){
int max=0;
for(int j=0;j<i;j++){
if(a[i]>a[j]){
if(dp[j]>max){
max=dp[j];
}
}
}
dp[i]=max+1;
if(dp[i]>omax){
omax=dp[i];
}
}
return omax;
}
}
*******************13**************
Longest Common Substring
Input: text1 = "abcde", text2 = "ace"
Output: 3
Explanation: The longest common subsequence is "ace" and its length is 3.
class Solution {
int max(int a, int b)
{
return (a > b)? a : b;
}
if(S1.charAt(i-1)==S2.charAt(j-1)){
dp[i][j]=1+dp[i-1][j-1];
}
else{
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
}
}
}
return dp[n][m];
}
}
recursion approach
int lcs( char[] X, char[] Y, int m, int n )
{
if (m == 0 || n == 0)
return 0;
if (X[m-1] == Y[n-1])
return 1 + lcs(X, Y, m-1, n-1);
else
return max(lcs(X, Y, m, n-1), lcs(X, Y, m-1, n));
}
****************************14**************
Shortest Common Subsequene Note :- Similar to Longest common
subsequence just calculate substracting total length of string form lcs (M+N)-LSC
class Solution
{
//Function to find length of shortest common supersequence of two strings.
for(int i=1;i<m+1;i++){
for(int j=1;j<n+1;j++){
if(X.charAt(i-1)==Y.charAt(j-1)){
dp[i][j]=1+dp[i-1][j-1];
}
else{
dp[i][j]=Math.max(dp[i][j-1],dp[i-1][j]);
}
}
}
return dp[m][n];
}
public static int shortestCommonSupersequence(String X,String Y,int m,int n)
{
//Your code here
return m+n-LCS(X,Y,m,n);
}
}
**********************************15*******************
Minimum insertion and deletion
for(int i=0;i<n+1;i++){
for(int j=0;j<m+1;j++){
if(i==0|| j==0){
dp[i][j]=0;
}
}
}
for(int i=1;i<n+1;i++){
for(int j=1;j<m+1;j++){
if(str1.charAt(i-1)==str2.charAt(j-1)){
dp[i][j]=1+dp[i-1][j-1];
}
else{
dp[i][j]=Math.max(dp[i][j-1],dp[i-1][j]);
}
}
}
int sum=n-dp[n][m];
int sum2=m-dp[n][m];
return sum+sum2;
}
}
***************16***************
palindrom subsequnce leetcode solution easy will give you one string.......create
another string by reverse the string and then calculate LCS will give to
palindromic subsequence
class Solution {
public int longestPalindromeSubseq(String s) {
String s2="";
for(int i=s.length()-1;i>=0;i--){
s2=s2+s.charAt(i);
}
int n=s.length();
int m=s.length();
for(int i=0;i<n+1;i++){
for(int j=0;j<m+1;j++){
if(i==0|| j==0){
dp[i][j]=0;
}
}
}
for(int i=1;i<n+1;i++){
for(int j=1;j<m+1;j++){
if(s.charAt(i-1)==s2.charAt(j-1)){
dp[i][j]=1+dp[i-1][j-1];
}
else{
dp[i][j]=Math.max(dp[i][j-1],dp[i-1][j]);
}
}
}
return dp[n][m];
}
}
*********************17****************************
Minimum Insertion Steps to Make a String Palindrome leetcode similar to
above one no difference except last line
Example 1:
Input: s = "zzazz"
Output: 0
Explanation: The string "zzazz" is already palindrome we don't need any insertions.
Example 2:
Input: s = "mbadm"
Output: 2
Explanation: String can be "mbdadbm" or "mdbabdm".
class Solution {
public int minInsertions(String s) {
String s2="";
for(int i=s.length()-1;i>=0;i--){
s2=s2+s.charAt(i);
}
int n=s.length();
int m=s.length();
for(int i=0;i<n+1;i++){
for(int j=0;j<m+1;j++){
if(i==0|| j==0){
dp[i][j]=0;
}
}
}
for(int i=1;i<n+1;i++){
for(int j=1;j<m+1;j++){
if(s.charAt(i-1)==s2.charAt(j-1)){
dp[i][j]=1+dp[i-1][j-1];
}
else{
dp[i][j]=Math.max(dp[i][j-1],dp[i-1][j]);
}
}
}
return s.length()-dp[n][m];
}
}
************************************18*****************************
Maximize the cut segment it is easy take a look at it
class Solution
{
//Function to find the maximum number of cuts.
// if length of rod is 0
// then total cuts will
// be 0 so, initialize
// the dp[0] with 0
dp[0] = 0;
// if certain length
// is not possible
if (dp[i] == -1)
continue;
// if a segment of
// p is possible
if (i + p <= l)
dp[i + p] = Math.max(dp[i + p], dp[i] + 1); it will check
current and its previous value
// if a segment of
// q is possible
if (i + q <= l)
dp[i + q] = Math.max(dp[i + q], dp[i] + 1);
// if a segment of
// r is possible
if (i + r <= l)
dp[i + r] = Math.max(dp[i + r], dp[i] + 1);
}
}
}
return max_profit;
}
}
class Solution {
public int maxProfit(int[] prices) {
if(prices[i]>prices[i-1]){
profit+=prices[i]-prices[i-1];
}
}
return profit;
}
}
if(both values are same will check the previous value i-1,j-1)
for differnt values will compare three values.. and pick the small value with
addition 1
Input:
s = "geek", t = "gesek"
Output: 1
Explanation: One operation is required
inserting 's' between two 'e's of str1.
class Solution {
public int editDistance(String s, String t) {
// Code here
int n=s.length();
int m=t.length();
for(int i=0;i<n+1;i++){
for(int j=0;j<m+1;j++){
if(i==0){
dp[i][j]=j;
}
if(j==0){
dp[i][j]=i;
}
}
}
for(int i=0;i<n+1;i++){
for(int j=0;j<m+1;j++){
if(i==0){
dp[i][j]=j;
}
else if(j==0){
dp[i][j]=i;
}
else if(s.charAt(i-1)==t.charAt(j-1)){
dp[i][j]=dp[i-1][j-1];
}
else{
dp[i][j]=1+Math.min(dp[i][j-1],Math.min(dp[i-1][j-1],dp[i-1]
[j]));
}
}
}
return dp[n][m];
}
}
**************************************21***************************
Longest Repeating Subsequence variation of LCS only 1 difference index should not
be same gfg
class Solution
{
public int LongestRepeatingSubsequence(String str)
{
// code here
int n=str.length();
int m=str.length();
for(int i=0;i<n+1;i++){
for(int j=0;j<m+1;j++){
if(i==0|| j==0){
dp[i][j]=0;
}
}
}
for(int i=1;i<n+1;i++){
for(int j=1;j<m+1;j++){
return dp[n][m];
}
}
******************************22**************
Longest Common Substring
class Solution{
int max(int a, int b)
{
return (a > b)? a : b;
}
if(S1.charAt(i-1)==S2.charAt(j-1)){
dp[i][j]=1+dp[i-1][j-1];
ans=max(ans,dp[i][j]);
}
else{
dp[i][j]=0; Variation of LCS here is one line
difference
}
}
}
return ans;
}
}
******************23***************
************************24********************
Max rectangle
Input:
n = 4, m = 4
M[][] = {{0 1 1 0},
{1 1 1 1},
{1 1 1 1},
{1 1 0 0}}
Output: 8
Explanation: For the above test case the
matrix will look like
0 1 1 0
1 1 1 1
1 1 1 1
1 1 0 0
the max size rectangle is
1 1 1 1
1 1 1 1
and area is 4 *2 = 8.
class Solution {
int[] prevSmaller(int[] a){
int[] ps=new int[a.length];
Stack<Integer> stk = new Stack<>();
for(int i=0;i<a.length;i++){
while(!stk.isEmpty() && a[stk.peek()]>=a[i]){
stk.pop();
}
if(stk.isEmpty()){
ps[i]=-1;
}else{
ps[i]=stk.peek();
}
stk.push(i);
}
return ps;
}
}
}
**********************25*********************
class Solution {
int first=helper(arr,n-2,dp)+arr[n];
int second=helper(arr,n-1,dp);
dp[n]=Math.max(first,second);
return dp[n];
}
int findMaxSum(int arr[], int n) {
// code here
int dp[]=new int[n];
// return helper(arr,n-1,dp);
dp[0]=arr[0];
for(int i=1;i<n;i++){
int take=arr[i];
if(i>1) take+=dp[i-2];
int not=dp[i-1];
dp[i]=Math.max(take,not);
}
return dp[n-1];
}
}
**********************26**************** leetcode
Minimum Falling Path Sum Striver
class Solution {
public int minFallingPathSum(int[][] matrix) {
int n = matrix.length;
int m = matrix[0].length;
int dp[][] = new int[n][m];
}
}
return maxi;
}
}
*****************************27******************* very easy Minimum sum partition
same as subset sum only in the last 1 step is more ...... Striver
class Solution
{
for(int i=1;i<n+1;i++){
for(int j=1;j<sum+1;j++){
if(arr[i-1]>j){
dp[i][j]=dp[i-1][j];
}
else{
dp[i][j]=dp[i-1][j]||dp[i-1][j-arr[i-1]];
}
}
}
int min=Integer.MAX_VALUE;
for(int i=0;i<=sum/2;i++){
if(dp[n][i]==true){
min=Math.min(min,Math.abs(sum-2*i));
}
}
return min;
}
}
*******************************28****************
Min number of coins gfg
class Solution{
for(int i=0;i<M+1;i++){
for(int j=0;j<V+1;j++){
if(i == 0) dp[i][j] = 10000000;
if(j == 0) dp[i][j] = 0;
}
}
for(int i=1;i<M+1;i++){
for(int j=1;j<V+1;j++){
if(coins[i-1]>j){
dp[i][j]=dp[i-1][j];
}
else{
dp[i][j]=Math.min(dp[i-1][j],1+dp[i][j-coins[i-1]]);
}
}
}
if(dp[M][V] == 10000000) return -1;
return dp[M][V];
}
}