Skip to content

Commit 175f194

Browse files
Add files via upload
1 parent f680993 commit 175f194

File tree

6 files changed

+229
-0
lines changed

6 files changed

+229
-0
lines changed

DP-1/COIN TOWER.txt

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
public class Solution {
2+
3+
public static String findWinner(int n, int x, int y) {
4+
//Your code goes here
5+
int[] dp = new int[n+1];
6+
// 0 - Whis wins ; 1 - Beerus wins
7+
dp[0]=0;
8+
dp[1]=1;
9+
10+
for (int i=2;i<=n;i++)
11+
{
12+
//Beerus wins if dp[i-1] or dp[i-x] or dp[i-y] is 0, i.e, Whis wins when the number of coins is (i-1), (i-x) or (i-y)
13+
//If none of them are 0, then Beerus cannot win => This means Whis wins for i number of coins
14+
if (dp[i-1]==0)
15+
{
16+
dp[i]=1;
17+
}
18+
else if ((i-x)>=0 && dp[i-x]==0)
19+
{
20+
dp[i]=1;
21+
}
22+
else if ((i-y)>=0 && dp[i-y]==0)
23+
{
24+
dp[i]=1;
25+
}
26+
else
27+
{
28+
dp[i]=0;
29+
}
30+
/*
31+
if (dp[i]==1)
32+
System.out.println("For n="+i+" coins, Winner is: Beerus");
33+
else
34+
System.out.println("For n="+i+" coins, Winner is: Whis");
35+
*/
36+
}
37+
38+
if (dp[n]==1)
39+
return "Beerus";
40+
else
41+
return "Whis";
42+
}
43+
44+
}

DP-1/LOOT HOUSES.txt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
public class Solution {
2+
3+
public static int maxMoneyLooted(int[] houses) {
4+
//Your code goes here
5+
//Handling the base cases where number of houses = 0,1,2
6+
if (houses.length==0)
7+
return 0;
8+
9+
if (houses.length==1)
10+
return houses[0];
11+
12+
if (houses.length==2)
13+
return Math.max(houses[0],houses[1]);
14+
15+
int n = houses.length;
16+
int[] dp = new int[n];
17+
dp[0]=houses[0];
18+
dp[1]=Math.max(houses[0],houses[1]);
19+
20+
for (int i=2;i<n;i++)
21+
{
22+
//For every house, we consider two cases
23+
//Case 1 - Current house is counted as part of the max value => This means the previous house cannot be counted
24+
int maxVal1=dp[i-2]+houses[i];
25+
26+
//Case 2 - Current house is not counted as part of the max value => This means previous house can be counted
27+
int maxVal2=dp[i-1];
28+
29+
//Max value till current house is maximum of the two possible max values till now
30+
dp[i]=Math.max(maxVal1,maxVal2);
31+
}
32+
33+
//Final element of dp stores max possible value for given number of houses and their respective amounts of loot
34+
return dp[n-1];
35+
36+
}
37+
38+
}

DP-1/MIN STEPS TO 1 USING DP.txt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
public class Solution {
2+
3+
public static int countMinStepsToOne(int n) {
4+
5+
if(n<=1)
6+
{
7+
return 0;
8+
}
9+
if(n==2)
10+
{
11+
return 1;
12+
}
13+
if(n==3)
14+
{
15+
return 1;
16+
}
17+
18+
int dp[] = new int[n+1];
19+
dp[0]=0;
20+
dp[1]=0;
21+
22+
for(int i =2;i<=n;i++)
23+
{
24+
int ans1=Integer.MAX_VALUE,ans2=Integer.MAX_VALUE,ans3=Integer.MAX_VALUE;
25+
ans1=dp[i-1];
26+
27+
if(i%2==0)
28+
{
29+
ans2=dp[i/2];
30+
}
31+
if(i%3==0)
32+
{
33+
ans3=dp[i/3];
34+
}
35+
36+
dp[i]=1+ Math.min(ans1,Math.min(ans2,ans3));
37+
}
38+
39+
return dp[n];
40+
}
41+
42+
}

DP-1/MIN STEPS TO ONE.txt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
public class Solution {
2+
3+
public static int countMinStepsToOne(int n) {
4+
//Your code goes here
5+
if(n<=1)
6+
{
7+
return 0;
8+
}
9+
10+
if(n==2)
11+
{
12+
return 1;
13+
}
14+
if(n==3)
15+
{
16+
return 1;
17+
}
18+
int a;
19+
int b= Integer.MAX_VALUE;
20+
int c=Integer.MAX_VALUE;
21+
a = countMinStepsToOne(n-1);
22+
if(n%2==0)
23+
{
24+
b = countMinStepsToOne(n/2);
25+
}
26+
if(n%3==0)
27+
{
28+
c=countMinStepsToOne(n/3);
29+
}
30+
31+
return 1+ Math.min(a,Math.min(b,c));
32+
}
33+
34+
public static void main(String[] args) {
35+
System.out.println(countMinStepsToOne(10));
36+
}
37+
38+
}

DP-1/MINIMUM NUMBER OF SQUARES.txt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
public class Solution {
2+
3+
public static int minCount(int n) {
4+
int dp[]=new int[n+1];
5+
for(int i =0;i<dp.length;i++)
6+
{
7+
dp[i]=-1;
8+
}
9+
10+
return count(n,dp);
11+
}
12+
13+
public static int count(int n,int dp[])
14+
{
15+
if(n==0)
16+
{
17+
return 0;
18+
}
19+
20+
int minans=Integer.MAX_VALUE;
21+
int ans;
22+
for(int i =1;i*i<=n;i++)
23+
{
24+
if(dp[n-(i*i)]==-1)
25+
{
26+
ans = count((n-(i*i)),dp);
27+
dp[n-(i*i)]=ans;
28+
}
29+
else
30+
{
31+
ans = dp[n-(i*i)];
32+
}
33+
34+
if(ans<minans)
35+
{
36+
minans=ans;
37+
}
38+
}
39+
return 1 + minans;
40+
}
41+
42+
}

DP-1/STAIR CASE.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
public class Solution {
2+
3+
public static long staircase(int n) {
4+
if(n<=1)
5+
{
6+
return 1;
7+
}
8+
if(n==2)
9+
{
10+
return 2;
11+
}
12+
13+
long[] dp = new long[n+1];
14+
dp[0]=1;
15+
dp[1]=1;
16+
dp[2]=2;
17+
for(int i =3;i<=n;i++)
18+
{
19+
dp[i]=dp[i-1]+dp[i-2]+dp[i-3];
20+
}
21+
22+
return dp[n];
23+
}
24+
25+
}

0 commit comments

Comments
 (0)