Skip to content
Open
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
59 changes: 59 additions & 0 deletions CoinChange.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*You are given an amount denoted by value. You are also given an array of coins. The array contains the denominations
* of the give coins. You need to find the minimum number of coins to make the change for value using the coins of given
* denominations. Also, keep in mind that you have infinite supply of the coins.
* */
//Initial Template for Java

import java.io.*;
import java.util.*;
public class CoinChange {
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
int testcases=sc.nextInt();
while(testcases-->0)
{
int value=sc.nextInt();
int numberOfCoins=sc.nextInt();
int coins[]=new int[numberOfCoins];

for(int i=0;i<numberOfCoins;i++)
{
int x=sc.nextInt();
coins[i]=x;
}
Coin obj=new Coin();
long answer=obj.minimumNumberOfCoins(coins,numberOfCoins,value);
System.out.println(answer==-1?"Not Possible":answer);
}
}
}


// } Driver Code Ends





class Coin
{
//Complete this function
public long minimumNumberOfCoins(int coins[],int numberOfCoins,int amount)
{
int dp[]=new int[amount+1];
Arrays.fill(dp,amount+1);
dp[0]=0;
for(int i=1;i<=amount;i++){
for(int j=0;j<coins.length;j++){
if(coins[j]<=i)
dp[i]=Math.min(dp[i],dp[i-coins[j]]+1);
}
}
return dp[amount]>amount?-1:dp[amount];
}
}


// { Driver Code Starts.

// } Driver Code Ends
47 changes: 47 additions & 0 deletions CountNumberOfHops.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*A frog jumps either 1, 2, or 3 steps to go to the top. In how many ways can it reach the top. As the answer will be
* large find the answer modulo 1000000007.
*/
//Initial Template for Java

import java.util.*;
import java.io.*;
import java.lang.*;

public class CountNumberOfHops
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();

while(t-- > 0)
{
int n = sc.nextInt();
System.out.println(new Hops().countWays(n));

}
}
}
// } Driver Code Ends


//User function Template for Java


class Hops
{
static long countWays(int n)
{
if(n==0||n==1)
return 1;
long dp[]=new long[n+1];
dp[0]=1;
dp[1]=1;
dp[2]=2;
for(int i=3;i<=n;i++)
dp[i]=(dp[i-1]+dp[i-2]+dp[i-3])%1000000007;
return dp[n];
}

}

44 changes: 44 additions & 0 deletions DistinctOccurrences.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*Given two strings S and T of length n and m respectively. find count of distinct occurrences of T in S as a sub-sequence.
*/
import java.util.*;

public class DistinctOccurrences
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
sc.nextLine();
while(t>0)
{
String line = sc.nextLine();
String S = line.split(" ")[0];
String T = line.split(" ")[1];

Solution sol=new Solution();
System.out.println(sol.subsequenceCount(S,T));
t--;
}
}
}// } Driver Code Ends


/*You are required to complete this method*/
class Solution
{
int subsequenceCount(String s, String t)
{
int dp[][]=new int[t.length()+1][s.length()+1];
for(int j=0;j<=s.length();j++)
dp[0][j]=1;
for(int i=0;i<t.length();i++){
for(int j=0;j<s.length();j++){
if(t.charAt(i)==s.charAt(j))
dp[i+1][j+1]=(dp[i][j]+dp[i+1][j])%1000000007;
else
dp[i+1][j+1]=(dp[i+1][j])%1000000007;
}
}
return dp[t.length()][s.length()];
}
}