Skip to content

Commit

Permalink
dp and some other stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
hhajela committed Mar 23, 2020
1 parent ad64dc1 commit e40e40e
Show file tree
Hide file tree
Showing 4 changed files with 220 additions and 0 deletions.
55 changes: 55 additions & 0 deletions 213_HouseRobberII.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <iostream>
#include <vector>

using namespace std;

int MaxRingLoot(const vector<int>& nums)
{
//take maximum of loot from 0 to n-2 and 1 to n-1

//case where 0 is picked

int lootMinusTwo =0;
int lootMinusOne =nums.front();

for(int i =1; i<nums.size()-1; i++)
{
int bestLoot = max(nums[i]+lootMinusTwo,lootMinusOne);
lootMinusTwo = lootMinusOne;
lootMinusOne = bestLoot;
}

int bestLoot = lootMinusOne;


//case where 0 is skipped
lootMinusOne = nums[1];
lootMinusTwo = 0;

for(int i = 2; i<nums.size(); i++)
{
int best = max(nums[i]+lootMinusTwo,lootMinusOne);
lootMinusTwo = lootMinusOne;
lootMinusOne = best;
}

bestLoot = max(bestLoot,lootMinusOne);

return bestLoot;

}

int main()
{
int nLength;
cin >> nLength;

vector<int> vecHouses(nLength);

for(int i =0; i<vecHouses.size(); i++)
cin >> vecHouses[i];

cout << "Max ring loot is " << MaxRingLoot(vecHouses) << endl;
return 0;

}
83 changes: 83 additions & 0 deletions 54_SpiralMatrix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include <iostream>
#include <vector>

using namespace std;

vector<int> SpiralTraversal(vector<vector<int>> matrix)
{
//sanity checks
if (matrix.empty()) return vector<int>();

//keep track of move direction and available steps
bool movHz = true; //moving horizontally
bool movRv = false; //moving vertically
int hzMaxSteps = matrix[0].size(); //total horizontal steps
int vtMaxSteps = matrix.size()-1; //total vertical steps
int i=0,j=0; //starting pos

vector<int> vecSpiral;

while(true)
{
//no more steps available
if( (movHz ? hzMaxSteps : vtMaxSteps) <= 0)
break;

//move ahead available number of steps in the correct direction
if (movHz)
for(int x =0; x<hzMaxSteps; x++)
vecSpiral.push_back(movRv ? matrix[i][j--] : matrix[i][j++]);
else
for(int x =0; x<vtMaxSteps; x++)
vecSpiral.push_back(movRv ? matrix[i--][j] : matrix[i++][j]);

//fix overshoot
if (movHz)
movRv ? j++ : j--;
else
movRv ? i++ : i--;

//update max steps
movHz ? hzMaxSteps-- : vtMaxSteps--;


//change direction
movHz = !movHz;

//change reverse or forward order
//if going from v to h
movRv = movHz ? !movRv : movRv;

//change indices to new starting position
if (movHz)
movRv ? j-- : j++;
else
movRv ? i-- : i++;
}

return vecSpiral;

}

int main()
{
int m,n;

cin >> m;
cin >> n;

vector<vector<int>> matrix(m,vector<int>(n));

for (int i =0; i<m; i++)
{
for(int j=0; j<n; j++)
cin >> matrix[i][j];
}

vector<int> vecResult = SpiralTraversal(matrix);

for(auto & num : vecResult)
cout << num << " ";
cout << endl;
return 0;
}
35 changes: 35 additions & 0 deletions 62_UniquePaths.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <vector>
#include <cstdlib>
#include <iostream>

using namespace std;

int CountUniqueWays(int m, int n)
{
//can either come from above or from left
//count both
vector<int> vecWays(m);
for(int i =0; i<m; i++)
vecWays[i] = 1; //exactly one way to get to anywhere in the top row

//calculate rest
for(int i =1; i<n; i++)
{
//take sum of total ways from top and left
//i.e. i-1,j and i,j-1
for(int j =0; j<m; j++)
vecWays[j] += (j>0?vecWays[j-1]:0);
}

return vecWays.back();
}

int main()
{
int m;
int n;
cin >> m;
cin >> n;
printf("number of ways of getting to %d,%d from %d,%d are %d\n",0,0,m-1,n-1,CountUniqueWays(m,n));
return 0;
}
47 changes: 47 additions & 0 deletions 91_DecodeWays.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <iostream>
#include <vector>


using namespace std;


int DecodeWays(const string & strMessage)
{
if (strMessage.empty()) return 0;

//can decode substrign upto ith position if there is a valid string decodign at i-1 or i-2
//and ith or ith+i-1th characters comprise valid encoded letters
//for empty string
int waysMinusOne = 1;
int waysMinusTwo = 0;

for(int i=0; i<strMessage.length(); i++)
{
int encodingValue = (strMessage[i]-'0') + (i>0?(strMessage[i-1]-'0')*10:0);

int currentWays = 0;
//no of ways if valid decoding at i-1 and this character is a valid letter
if (strMessage[i] <= '9' && strMessage[i] >= '1')
currentWays = waysMinusOne;

//no of ways if valid decoding at i-2 and this and the last character comprise of a valid two digit encoded letter
//only count the cases where two letter decoding
if (encodingValue >= 10 && encodingValue <= 26)
currentWays += waysMinusTwo;


waysMinusTwo = waysMinusOne;
waysMinusOne = currentWays;
}

return waysMinusOne;
}

int main()
{
string strMessage;
getline(cin, strMessage);

cout << "Number of possible ways of decoding string are " << DecodeWays(strMessage) << endl;
}

0 comments on commit e40e40e

Please sign in to comment.