Skip to content

Commit

Permalink
#22 Still tle, hint says to follow house robber problem
Browse files Browse the repository at this point in the history
  • Loading branch information
debojyoti-majumder authored May 10, 2019
1 parent 37db2a9 commit babc3b8
Showing 1 changed file with 34 additions and 9 deletions.
43 changes: 34 additions & 9 deletions 2019Q1/cppWorkspace/leetcode740.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,33 @@ class Solution {
int deleteAndEarn(vector<int>& nums) {
// Building the map of numbers
auto numMap { buildMap(nums) };
if( numMap.size() == 0 ) return 0;

auto accCost { 0 };
auto prevNumber { 0 };

map<int,size_t> costMap;

for( const auto& numberItem : numMap ) {
if( numberItem.first == numMap.begin()->first ) {
prevNumber = numberItem.first;
costMap.insert(numberItem);
}
else if( costMap.size() == 3 ) {
accCost += getMaxPoint(costMap, costMap.begin());

costMap.clear();
costMap.insert(numberItem);
prevNumber = numberItem.first;
}
else if( prevNumber + 1 == ( numberItem.first ) ) {
prevNumber = numberItem.first;
costMap.insert(numberItem);
}
}

// Starting selection from the first item
return getMaxPoint(numMap, numMap.begin());
accCost += getMaxPoint(costMap, costMap.begin());
return accCost;
}
};

Expand All @@ -108,14 +132,15 @@ void testLeetcode740() {
vector<int> inp2{2,2,3,3,3,4};
cout << s.deleteAndEarn(inp2) << endl;

// // Should not get TLE, correct output 138
vector<int> tleCase { 1,8,5,9,6,9,4,1,7,3,3,6,3,3,8,2,6,3,2,2,1,2,9,8,
7,1,1,10,6,7,3,9,6,10,5,4,10,1,6,7,4,7,4,1,9,5,1,5,7,5};
vector<int> q{1,2,3,4,5,6};
cout << s.deleteAndEarn(q) << endl;

// Should output 138
vector<int> tleCase { 1,8,5,9,6,9,4,1,7,3,3,6,3,3,8,2,6,3,2,2,1,2,9,8, 7,1,1,10,6,7,3,9,6,10,5,4,10,1,6,7,4,7,4,1,9,5,1,5,7,5};
cout << s.deleteAndEarn(tleCase) << endl;

vector<int> tleCase2 { 12,32,93,17,100,72,40,71,37,92,58,34,29,78,11,84,77,90,
92,35,12,5,27,92,91,23,65,91,85,14,42,28,80,85,38,71,62,82,66,3,33,33,55,60,48,78,63,
11,20,51,78,42,37,21,100,13,60,57,91,53,49,15,45,19,51,2,96,22,32,2,46,62,58,11,29,6,74,
38,70,97,4,22,76,19,1,90,63,55,64,44,90,51,36,16,65,95,64,59,53,93};
vector<int> tleCase2 { 25,95,76,4,90,87,46,44,58,33,62,79,5,3,32,21,87,31,44,68,49,45,18,50,26,74,64,17,81,49,80,58,15,6,90,8,6,28,15,16,9,98,50,96,30,27,67,99,86,63,19,54,80,4,84,24,60,22,75,35,76,3,37,80,16,51,14,51,93,49,84,82,48,9,7,79,7,68,15,11,71,59,18,47,5,57,64,38,99,35,57,9,13,14,81,25,5,14,74,63,80,78,70,48,32,54,34,40,21,95,98,25,72,59,21,49,19,2,18,93,14,81,57,41,95,69,71,64,50,35,26,72,92,51,18,11,55,26,2,95,93,35,71,47,88,22,66,90,72,
66,61,11,76,10,95,24,35,75,15,95,24,76,78,58,28,23,75,73,40,40,84,18,31,91,7,97,13,96,39,17,22,85,28,79,61,73,
88,36,82,27,95,31,96,59,20,13,44,13,7,29};
cout << s.deleteAndEarn(tleCase2) << endl;
}

0 comments on commit babc3b8

Please sign in to comment.