SRM232 StockHistory

You have recently been given a one-time bonus at work, along with a pay raise. In the interest of putting your new found money to good use, you have decided to invest in the stock market. To learn about recent market history, you have acquired historical data on several stocks you might be interested in purchasing.

For experiment’s sake, you want to evaluate the potential performance of your selected stocks. What you are wondering is how much money you could have made if, at the beginning, you had an initial investment of int initialInvestment dollars, and each month thereafter you had an additional int monthlyContribution dollars. Assume that you may buy any number of shares of stocks at the beginning of each month (including fractional numbers of shares), and that at the end of the timeframe (at the beginning of the last month) represented in the data, you sell all of your holdings. You may not sell stocks in the intermediate period. (See examples for clarification.)

You are given a String[], stockPrices, in which each element lists the prices of each stock at the beginning of a month. Each element of stockPrices is in the form “stock0 stock1 …” (quotes added for clarity), where each stocki is a positive integer with no leading zeroes. The first element of stockPrices gives the initial prices, the second element gives the prices at the beginning of the first month after you start investing, and so on.

You are to return an int indicating the maximum earnings (profit) you could make by the end of the timeframe. You should round your result to the nearest integer.

#include <cmath>
#include <string>
#include <vector>
using namespace std;

class StockHistory {
    public:
        int maximumEarnings(int initialInvestment, int monthlyContribution, vector <string> stockPrices){
            int money = initialInvestment;
            int month = stockPrices.size();
            int corp = 1:
            char *s = (char *)stockPrices[0].c_str();
            while(*s++) if(*s==' ') corp++;

            int prices[50][999];
            double max = 0; profit = 0;
            double proportion[50] = {0};
            bool buy[50] = {false};

            for(int i = 0; i < month; i++){
                string s = stockPrices[i];
                for(int j = 0; j < corp; j++){
                    int pos=s.find(" ");
                    if(pos == string::npos){
                        prices[i][j] = atoi(s.c_str());
                    } else {
                        prices[i][j] = atoi(s.substr(0, pos).c_str());
                        s = s.substr(pos+1, s.size());
                    }
                }
            }

            for(int i = month - 2; 0 <= i; i--){
                for(int j = 0; j < corp; j++){
                    double p = 1.0 * prices[month - 1][j] / prices[i][j] - 1;
                    if(0 < p && max < p) {
                        buy[i] = true;
                        max = p;
                        proportion[i] = p;
                    }
                    for(int i = 0; i < month; i++){
                        if(buy[i]){
                            profit += money * proportion[i];
                            money = 0;
                        }
                        money += monthlyContribution;
                    }
                    return (int)Math.round(profit);
                }
            }
        }
}