/** * Implements a brute force solution to the Stock Market problem. * @author americachambers * */ public class BruteForce implements StockIfc { public StockInfo solve(int[] prices) { int buy = 0; int sell = 0; int profit = Integer.MIN_VALUE; // Consider all possible combinations of (buy, sell) dates for(int i = 0; i < prices.length; i++) { for(int j = i; j < prices.length; j++){ if(prices[j]-prices[i] > profit) { profit = prices[j]-prices[i]; buy = i; sell = j; } } } // Create and populate a StockInfo object // with the solution found StockInfo answer = new StockInfo(); answer.buy_index = buy; answer.sell_index = sell; answer.profit = profit; return answer; } }