Skip to content

Commit 01eec88

Browse files
Add files via upload
1 parent bba960b commit 01eec88

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+2521
-0
lines changed

DynamicProgramming/ArrangeII.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
You are given a sequence of black and white horses, and a set of K stables numbered 1 to K. You have to accommodate the horses into the stables in such a way that the following conditions are satisfied:
3+
4+
You fill the horses into the stables preserving the relative order of horses. For instance, you cannot put horse 1 into stable 2 and horse 2 into stable 1. You have to preserve the ordering of the horses.
5+
No stable should be empty and no horse should be left unaccommodated.
6+
Take the product (number of white horses * number of black horses) for each stable and take the sum of all these products. This value should be the minimum among all possible accommodation arrangements
7+
Example:
8+
9+
10+
Input: {WWWB} , K = 2
11+
Output: 0
12+
13+
Explanation:
14+
We have 3 choices {W, WWB}, {WW, WB}, {WWW, B}
15+
for first choice we will get 1*0 + 2*1 = 2.
16+
for second choice we will get 2*0 + 1*1 = 1.
17+
for third choice we will get 3*0 + 0*1 = 0.
18+
19+
Of the 3 choices, the third choice is the best option.
20+
21+
If a solution is not possible, then return -1
22+
23+
LINK: https://www.interviewbit.com/problems/arrange-ii/
24+
*/
25+
26+
typedef long long int ll;
27+
28+
int Solution::arrange(string A, int B)
29+
{
30+
int n = A.length();
31+
32+
if(n<B)
33+
return -1;
34+
35+
int wh = 0, bh = 0;
36+
37+
ll dp[B][n];
38+
39+
for(int i=0;i<n;i++)
40+
{
41+
if(A[i]=='B')
42+
bh++;
43+
else
44+
wh++;
45+
dp[0][i] = bh*wh;
46+
}
47+
48+
for(int i=1;i<B;i++)
49+
{
50+
for(int j=0;j<n;j++)
51+
{
52+
dp[i][j] = INT_MAX;
53+
54+
if(i>j)
55+
continue;
56+
57+
bh = wh = 0;
58+
59+
for(int k=j;k>0;k--)
60+
{
61+
if(A[k]=='B')
62+
bh++;
63+
else
64+
wh++;
65+
dp[i][j] = min(dp[i][j], dp[i-1][k-1] + (bh*wh));
66+
}
67+
}
68+
}
69+
return dp[B-1][n-1];
70+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
Say you have an array for which the ith element is the price of a given stock on day i.
3+
4+
If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.
5+
6+
Example :
7+
8+
Input : [1 2]
9+
Return : 1
10+
11+
LINK: https://www.interviewbit.com/problems/best-time-to-buy-and-sell-stocks-i/
12+
*/
13+
14+
int Solution::maxProfit(const vector<int> &A)
15+
{
16+
int res = 0;
17+
int n = A.size();
18+
if(n==0)
19+
return res;
20+
21+
int min_ele = A[0];
22+
23+
for(int i=1;i<n;i++)
24+
{
25+
if(min_ele > A[i])
26+
min_ele = A[i];
27+
28+
res = max(res, A[i] - min_ele);
29+
}
30+
return res;
31+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
Say you have an array for which the ith element is the price of a given stock on day i.
3+
4+
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
5+
6+
Example :
7+
8+
Input : [1 2 3]
9+
Return : 2
10+
11+
LINK: https://www.interviewbit.com/problems/best-time-to-buy-and-sell-stocks-ii/
12+
*/
13+
14+
int Solution::maxProfit(const vector<int> &A)
15+
{
16+
int n = A.size();
17+
int res = 0;
18+
19+
int i = 0;
20+
21+
while(i<n-1)
22+
{
23+
while(i<n-1 && A[i+1]<=A[i])
24+
i++;
25+
26+
if(i==n-1)
27+
break;
28+
29+
int j = i;
30+
31+
while(i<n-1 && A[i+1]>=A[i])
32+
i++;
33+
34+
res += (A[i] - A[j]);
35+
i++;
36+
}
37+
return res;
38+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
Say you have an array for which the ith element is the price of a given stock on day i.
3+
4+
Design an algorithm to find the maximum profit. You may complete at most two transactions.
5+
6+
Note:
7+
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
8+
9+
Example :
10+
11+
Input : [1 2 1 2]
12+
Output : 2
13+
14+
Explanation :
15+
Day 1 : Buy
16+
Day 2 : Sell
17+
Day 3 : Buy
18+
Day 4 : Sell
19+
20+
LINK: https://www.interviewbit.com/problems/best-time-to-buy-and-sell-stocks-iii/
21+
*/
22+
23+
int Solution::maxProfit(const vector<int> &A)
24+
{
25+
int n = A.size();
26+
if(n==0)
27+
return 0;
28+
int dp[n];
29+
memset(dp,0,sizeof(dp));
30+
31+
int max_ele = A[n-1];
32+
33+
for(int i=n-2;i>=0;i--)
34+
{
35+
if(A[i] > max_ele)
36+
max_ele = A[i];
37+
38+
dp[i] = max(dp[i+1], max_ele - A[i]);
39+
}
40+
41+
int min_ele = A[0];
42+
43+
for(int i=1;i<n;i++)
44+
{
45+
if(A[i] < min_ele)
46+
min_ele = A[i];
47+
48+
dp[i] = max(dp[i-1], dp[i] + (A[i]-min_ele));
49+
}
50+
51+
return dp[n-1];
52+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
You are given a set of coins S. In how many ways can you make sum N assuming you have infinite amount of each coin in the set.
3+
4+
Note : Coins in set S will be unique. Expected space complexity of this problem is O(N).
5+
6+
Example :
7+
8+
Input :
9+
S = [1, 2, 3]
10+
N = 4
11+
12+
Return : 4
13+
14+
Explanation : The 4 possible ways are
15+
{1, 1, 1, 1}
16+
{1, 1, 2}
17+
{2, 2}
18+
{1, 3}
19+
Note that the answer can overflow. So, give us the answer % 1000007
20+
21+
LINK: https://www.interviewbit.com/problems/coin-sum-infinite/
22+
*/
23+
24+
#define MOD 1000007
25+
26+
int Solution::coinchange2(vector<int> &A, int B)
27+
{
28+
int n = A.size();
29+
30+
int dp[B+1];
31+
memset(dp,0,sizeof(dp));
32+
33+
dp[0] = 1;
34+
35+
for(int i=0;i<n;i++)
36+
{
37+
for(int j=A[i];j<=B;j++)
38+
dp[j] = (dp[j] + dp[j-A[i]])%MOD;
39+
}
40+
41+
return dp[B];
42+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
There are N coins (Assume N is even) in a line. Two players take turns to take a coin from one of the ends of the line until there are no more coins left. The player with the larger amount of money wins. Assume that you go first.
3+
4+
Write a program which computes the maximum amount of money you can win.
5+
6+
Example:
7+
8+
suppose that there are 4 coins which have value
9+
1 2 3 4
10+
now you are first so you pick 4
11+
then in next term
12+
next person picks 3 then
13+
you pick 2 and
14+
then next person picks 1
15+
so total of your money is 4 + 2 = 6
16+
next/opposite person will get 1 + 3 = 4
17+
so maximum amount of value you can get is 6
18+
19+
LINK: https://www.interviewbit.com/problems/coins-in-a-line/
20+
*/
21+
22+
typedef long long int ll;
23+
24+
vector<vector<ll> > dp;
25+
26+
ll solve(vector<int> &A, int i, int j)
27+
{
28+
if(i==j)
29+
return A[i];
30+
if(i+1==j)
31+
return dp[i][j] = max(A[i],A[j]);
32+
33+
if(dp[i][j]!=-1)
34+
return dp[i][j];
35+
36+
dp[i][j] = max(A[i] + min(solve(A,i+2,j), solve(A,i+1,j-1)), A[j] + min(solve(A,i,j-2), solve(A,i+1,j-1)));
37+
return dp[i][j];
38+
}
39+
40+
int Solution::maxcoin(vector<int> &A)
41+
{
42+
int n = A.size();
43+
44+
if(n==0)
45+
return 0;
46+
47+
dp.clear();
48+
dp.resize(n,vector<ll>(n,-1));
49+
50+
return solve(A,0,n-1);
51+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
Given two sequences S, T, count number of unique ways in sequence S, to form a subsequence that is identical to the sequence T.
3+
4+
Subsequence : A subsequence of a string is a new string which is formed from the original string by deleting some (can be none ) of the characters without disturbing the relative positions of the remaining characters. (ie, "ACE" is a subsequence of "ABCDE" while "AEC" is not).
5+
Example :
6+
7+
S = "rabbbit"
8+
T = "rabbit"
9+
Return 3. And the formations as follows:
10+
11+
S1= "ra_bbit"
12+
S2= "rab_bit"
13+
S3="rabb_it"
14+
"_" marks the removed character.
15+
16+
LINK: https://www.interviewbit.com/problems/distinct-subsequences/
17+
*/
18+
19+
int Solution::numDistinct(string A, string B)
20+
{
21+
int na = A.length();
22+
int nb = B.length();
23+
int dp[na+1][nb+1];
24+
memset(dp,0,sizeof(dp));
25+
26+
for(int i=0;i<na;i++)
27+
dp[i][0] = 1;
28+
29+
for(int i=1;i<=na;i++)
30+
{
31+
for(int j=1;j<=nb;j++)
32+
{
33+
dp[i][j] = dp[i-1][j];
34+
if(A[i-1]==B[j-1])
35+
dp[i][j] += dp[i-1][j-1];
36+
}
37+
}
38+
return dp[na][nb];
39+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of M x N rooms laid out in a 2D grid. Our valiant knight (K) was initially positioned in the top-left room and must fight his way through the dungeon to rescue the princess.
3+
4+
The knight has an initial health point represented by a positive integer. If at any point his health point drops to 0 or below, he dies immediately.
5+
6+
Some of the rooms are guarded by demons, so the knight loses health (negative integers) upon entering these rooms; other rooms are either empty (0’s) or contain magic orbs that increase the knight’s health (positive integers).
7+
8+
In order to reach the princess as quickly as possible, the knight decides to move only rightward or downward in each step.
9+
10+
Write a function to determine the knight’s minimum initial health so that he is able to rescue the princess.
11+
12+
For example, given the dungeon below, the initial health of the knight must be at least 7 if he follows the optimal path
13+
14+
RIGHT-> RIGHT -> DOWN -> DOWN.
15+
16+
Dungeon Princess: Example 1
17+
18+
19+
20+
Input arguments to function:
21+
Your function will get an M*N matrix (2-D array) as input which represents the 2D grid as described in the question. Your function should return an integer corresponding to the knight’s minimum initial health required.
22+
23+
24+
25+
Note:
26+
The knight’s health has no upper bound.
27+
Any room can contain threats or power-ups, even the first room the knight enters and the bottom-right room where the princess is imprisoned.
28+
29+
30+
LINK: https://www.interviewbit.com/problems/dungeon-princess/
31+
*/
32+
33+
int Solution::calculateMinimumHP(vector<vector<int> > &A)
34+
{
35+
int m = A.size();
36+
int n = A[0].size();
37+
38+
int dp[m][n];
39+
memset(dp,0,sizeof(dp));
40+
41+
dp[m-1][n-1] = (A[m-1][n-1] > 0) ? 1 : abs(A[m-1][n-1])+1;
42+
43+
for(int i=m-2;i>=0;i--)
44+
dp[i][n-1] = max(dp[i+1][n-1] - A[i][n-1], 1);
45+
for(int i=n-2;i>=0;i--)
46+
dp[m-1][i] = max(dp[m-1][i+1] - A[m-1][i], 1);
47+
48+
for(int i=m-2;i>=0;i--)
49+
{
50+
for(int j=n-1;j>=0;j--)
51+
{
52+
int val = min(dp[i+1][j], dp[i][j+1]);
53+
dp[i][j] = max(val - A[i][j], 1);
54+
}
55+
}
56+
57+
return dp[0][0];
58+
}

0 commit comments

Comments
 (0)