Documente Academic
Documente Profesional
Documente Cultură
1) count all the possible paths from top left to bottom right of a mXn matrix with the constraints
that from each cell you can either move only to right or down
/ Returns count of possible paths to reach cell at row number m and column
// number n from the topmost leftmost cell (cell at 1, 1)
int numberOfPaths(int m, int n)
{
// Create a 2D table to store results of subproblems
int count[m][n];
}
return count[m-1][n-1];
}
b) http://betterexplained.com/articles/navigate-a-grid-using-
combinations-and-permutations/
std::map<int,int> mp;
int i;
for(i=0;i<n;i++){
if(mp[arr[i]]==1){
return arr[i];
}
else{
mp[arr[i]]=1;
return 0;
int sum=0,i;
for(i=0;i<n;i++){
sum=sum+arr[i];
sum=sum-rep;
int sum2=n*(n+1)/2;
int mis=sum2-sum;
return 1;
http://www.geeksforgeeks.org/sort-array-wave-form-2/
if(close == n)
{
printf("%s \n", str);
return;
}
else
{
if(open > close) {
str[pos] = '}';
_printParenthesis(pos+1, n, open, close+1);
}
if(open < n) {
str[pos] = '{';
_printParenthesis(pos+1, n, open+1, close);
}
}
}
int i;
if(l==r){
for(i=0;i<vec.size();i++){
cout<<vec[i]<<" ";
cout<<"\n";
else{
for(i=l;i<=r;i++){
// swapvec(vec,l,i);
wordpermute(vec,l+1,r);
int i,j=0;
for(i=start;i<n;i++){
sub.push_back(vec[i]);
j++;
cout<<"sub is\n";
for(i=0;i<sub.size();i++){
cout<<sub[i]<<"\n";
return sub;
int i,j,f;
map<vector<string>,int> mp;
for(i=0;i<n;i++){
for(j=0;j<=i;j++){
vector<string> sub=getsub(vec,j,i+1);
auto search=mp.find(sub);
if(search==mp.end()){
mp[sub]++;
printVecStr(sub);
}
A head to tail ordering of strings is the one in which the last
letter of the previous word is the same as the first letter of the
following word. For eg. ‘Geekforgeeks’can be followed by
‘ software’.
map<vector<string>,int> wordpermute(map<vector<string>,int>
mp,vector<string> vec,int l,int r){
int i;
if(l==r){
for(i=0;i<vec.size();i++){
cout<<vec[i]<<" ";
}
cout<<"\n";
mp[vec]=1;
}
else{
for(i=l;i<=r;i++){
// swapvec(vec,l,i);
iter_swap(vec.begin() + l, vec.begin() + i);
mp=wordpermute(mp,vec,l+1,r);
iter_swap(vec.begin() + l, vec.begin() + i);
}
}
return mp;
}
int checkheadtail(vector<string> vec){
int i,k=0;
for(i=0;i<vec.size()-1;i++){
string str=vec[i];
string str2=vec[i+1];
int l=str.length();
if(str[l-1]==str2[0]){
k=1;
}
else{
k=0;
return 0;
}
}
return k;
}
9) Professor Midas drives an automobile from Newark to Reno along Interstate 80. His car’s gas
tank, when full, holds enough gas to travel ‘d’ miles, and his map gives the distances
between gas stations on his route. The professor wishes to make as few stops as possible
along the way. The problem is to find an efficient method by which Professor Midas can
determine at which gas stations he should stop, and prove that the strategy adopted yields
an optimal solution. [2]
http://people.cis.ksu.edu/~subbu/Papers/Minimum%20Stops.pdf
i ¬1 distance_to_travel ¬ Dist [i] while (Gi+1 != Gn) { distance_to_travel ¬ Dist [i] /*
Greedy loop */ while ((distance_to_travel £ d) AND (Gi+2 exists)) { destination =
Gi+1 distance_to_travel ¬ distance_to_travel + Dist [i+1] i ¬i + 1 } Put ‘destination’ in
Solution set S {} }