0% found this document useful (0 votes)
15 views4 pages

Backtracking Lecture 3

The document discusses three algorithms: 1) Lexicographic Power Set which generates all subsets of a string in lexicographic order. 2) Combination Sum which finds all combinations of numbers that sum to a target number. 3) Path with Maximum Gold which finds the maximum amount of gold that can be collected by traversing a 2D grid from top to bottom.

Uploaded by

Shivangi Rawat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views4 pages

Backtracking Lecture 3

The document discusses three algorithms: 1) Lexicographic Power Set which generates all subsets of a string in lexicographic order. 2) Combination Sum which finds all combinations of numbers that sum to a target number. 3) Path with Maximum Gold which finds the maximum amount of gold that can be collected by traversing a 2D grid from top to bottom.

Uploaded by

Shivangi Rawat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

Lexicographic Power Set

//User function Template for C++

void rec(int indx, string &str, string curr, int n)


{
if(indx==n)
{
return;
}
if(curr.size()>0)
{
cout<<curr<<" ";
}

for(int j=indx+1;j<n;j++)
{
char ch= str[j];

rec(j,str,curr+ch,n);
}
}
void powerSet(string str)
{
// code here
sort(str.begin(),str.end());

int n=str.size();

string curr="";
rec(-1,str,curr,n);

}
2. Combination Sum (Adobe, Amazon, Microsoft)

class Solution {
public:
void rec(int i, vector<int>&a, vector<int>&curr,
vector<vector<int>>&ans, int B)
{
if(B==0)
{
ans.push_back(curr);
return;
}

if(B<0 or i==a.size())
{
return;
}

if(i>0 and a[i-1]==a[i])


{
rec(i+1,a,curr,ans,B);
}

else
{

curr.push_back(a[i]);

rec(i,a,curr,ans,B-a[i]);

curr.pop_back();

rec(i+1,a,curr,ans,B);
}

}
vector<vector<int> > combinationSum(vector<int> &a, int B) {
// Your code here
vector<vector<int>>ans;
vector<int>curr;
int i=0;
sort(a.begin(),a.end());

rec(i,a,curr,ans,B);

return ans;
}
};

3. Path with Maximum Gold (Google, Amazon)

class Solution {
public:
int rec(int i, int j, int n, int m, vector<vector<int>>& grid)
{
if(i<0 || j<0 || j==m || i==n || grid[i][j]==0)
{
return 0;
}

int val= grid[i][j];

grid[i][j]=0;

int up= val + rec(i-1,j,n,m,grid);

int down = val + rec(i+1,j,n,m,grid);

int left= val+ rec(i,j-1,n,m,grid);

int right = val + rec(i,j+1,n,m,grid);

grid[i][j]= val; //backtracking step

return max({up,down,right,left});

}
int getMaximumGold(vector<vector<int>>& grid) {
int n = grid.size();
int m= grid[0].size();

int ans=0;

for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
int amount=0;
if(grid[i][j]!=0)
{
amount= rec(i,j,n,m,grid);
}

if(amount> ans)
{
ans=amount;
}
}
}
return ans;
}
};

You might also like