c++ - Max Sum Of Integers -


edit: solved! treating negative numbers test case 0, instead of having output negative well. help!

here challenge description: https://www.codeeval.com/open_challenges/17/
keep getting partially solved score. want know why. in eyes, code works. , believe o(n) time. looking!

here code:

    #include <iostream>     #include <fstream>     #include <cstdlib>     #include <vector>     #include <string>     #include <sstream>      using namespace std;      int max(int a, int b)     {         if (a > b)             return a;         else return b;     }       int maxsubarray(vector<int> values)     {         int max_so_far = values[0];         int curr_max = values[0];         for(int = 1; < values.size(); ++i)         {             curr_max = max(values[i], curr_max + values[i]);             max_so_far = max(max_so_far, curr_max);         }         return max_so_far;     }      int main(int argc, char *argv[])      {         std::vector<vector<int> > values; //to hold values of stock price change         ifstream file(argv[1]);         std::string line; //for txt file input         int value = 0; //for holding value of stock change          while (std::getline(file, line))          {             int pos = 0;             if(line.length() == 0)                 continue;             else             {                 std::istringstream iss(line);                 std::vector<int> list; // temporary list of values pushed 2-d vector                 while (iss >> value)                 {                        list.push_back(value);                   }                  values.push_back(list);             }         }          for(int = 0; < values.size(); ++i)         {             cout << maxsubarray(values[i]);             cout << endl;         }     /*           cout << " printing values : " << endl;         (int j = 0; j < values.size(); ++j)         {             (int k = 0; k < values[j].size(); ++k)                 cout << values[j][k] << " ";             cout << endl;         }     */         return 0;     }  

so swapped out code now. better score it's still partial.

#include <iostream> #include <fstream> #include <cstdlib> #include <vector>  using namespace std;  int max(int a, int b) {     if (a > b)         return a;     else return b; }   int maxsubarray(vector<int> values) {     int max_so_far = values[0];     int curr_max = values[0];     if (curr_max < 0)      {         curr_max = 0;                    max_so_far = 0;     }     for(int = 1; < values.size(); ++i)     {         curr_max = max(values[i], curr_max + values[i]);         curr_max = max(curr_max, 0);         max_so_far = max(max_so_far, curr_max);     }     return max_so_far; }  int main(int argc, char *argv[])  {     std::vector<vector<int> > values; //to hold values of stock price change     ifstream file(argv[1]);     std::string line; //for txt file input     std::string token; //for subtring converted char int     int value = 0; //for holding value of stock change     int count = 0;// holding how many total cases      while (!file.eof())      {         int pos = 0;         getline(file, line);         if(line.length() == 0)             continue;         else         {              std::vector<int> list; // temporary list of values pushed 2-d vector              while ((pos = line.find(",")) != std::string::npos )             {                 token = line.substr(0,pos);                 value = atoi(token.c_str());                 line.erase(0, pos + 1);                  list.push_back(value);               }             value = atoi(line.c_str());             list.push_back(value);              values.push_back(list);              ++count;         }     }      for(int = 0; < values.size(); ++i)     {         cout << maxsubarray(values[i]);         cout << endl;     }      cout << " printing values : " << endl;     (int j = 0; j < values.size(); ++j)     {         (int k = 0; k < values[j].size(); ++k)             cout << values[j][k] << " ";         cout << endl;     }      return 0; }  

Comments