Last active
          December 12, 2022 06:45 
        
      - 
      
- 
        Save kuznetsov-m/b4ed85df8c46c98b3ec6b50e44a9ba43 to your computer and use it in GitHub Desktop. 
Revisions
- 
        kuznetsov-m revised this gist Dec 12, 2022 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -18,7 +18,7 @@ vector<string> split(const string &); * The function accepts 2D_INTEGER_ARRAY mat as parameter. */ void backtrack(const vector<vector<int>> &mat, int energy, int i, int j, int &maxi) { energy -= mat[i][j]; if (energy < maxi) { 
- 
        kuznetsov-m revised this gist Dec 11, 2022 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,7 @@ #include <iostream> #include <vector> #include <string> #include <limits> // <----- did not have time to fix here using namespace std; @@ -38,7 +38,7 @@ void backtrack(vector<vector<int>> mat, int energy, int i, int j, int &maxi) { } int maxEnergy(vector<vector<int>> mat) { int maxi = std::numeric_limits<int>::min(); // <----- did not have time to fix here for (auto j = 0; j < mat[0].size(); ++j) { auto energy = 100; backtrack(mat, energy, 0, j, maxi); 
- 
        kuznetsov-m created this gist Dec 11, 2022 .There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,64 @@ #include <iostream> #include <vector> #include <string> #include <limits> using namespace std; string ltrim(const string &); string rtrim(const string &); vector<string> split(const string &); /* * Complete the 'maxEnergy' function below. * * The function is expected to return an INTEGER. * The function accepts 2D_INTEGER_ARRAY mat as parameter. */ void backtrack(vector<vector<int>> mat, int energy, int i, int j, int &maxi) { energy -= mat[i][j]; if (energy < maxi) { return; } if (i == 3) { maxi = std::max(energy, maxi); return; } auto j_min = std::max(j - 1, 0); auto j_max = std::min(j + 1, 3); for (auto _j = j_min; _j <= j_max; ++_j) { backtrack(mat, energy, i + 1, _j, maxi); } } int maxEnergy(vector<vector<int>> mat) { int maxi = std::numeric_limits<int>::min(); for (auto j = 0; j < mat[0].size(); ++j) { auto energy = 100; backtrack(mat, energy, 0, j, maxi); } return maxi; } int main() { { vector<vector<int>> mat = { {99, 99, 0, 99}, {99, 99, 0, 99}, {99, 99, 0, 99}, {99, 199, 116, 199} }; assert(maxEnergy(mat) == -16); } return 0; }