/** * Implements a brute force solution to the transformed subset sum problem * @author alchambers * */ public class BruteForceTransform implements StockIfc{ public StockInfo solve(int[] prices){ int numDays = prices.length; int[] change = new int[numDays-1]; // Transform the problem into an instance of the // Maximum subarray problem for(int i = 0; i < change.length; i++) { change[i] = prices[i+1]-prices[i]; } return bruteForce(change); } /** * Considers all possible combinations * @param array An array of integers * @return solution to the transformed Stock Market problem */ private StockInfo bruteForce(int[] array) { StockInfo s = new StockInfo(); int best = Integer.MIN_VALUE; for(int start = 0; start < array.length; start++){ int sum = 0; for(int i = start; i < array.length; i++){ sum += array[i]; if(sum > best){ best = sum; s.buy_index = start; s.sell_index = i; s.profit = sum; } } } return s; } }