Advertisement
Guest User

Untitled

a guest
Aug 20th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. public class StockSingleSellBruteForce {
  2.  
  3. public static void maxProfit(int [] prices){
  4. int profit = -1;
  5. int buyDateIndex = prices[0];
  6. int sellDateIndex = prices[0];
  7. for (int i = 0; i <prices.length ; i++) {
  8. for (int j = i; j <prices.length ; j++) {
  9. if(prices[j]>prices[i] && (prices[j]-prices[i]>profit)) {
  10. profit = prices[j] - prices[i];
  11. buyDateIndex = i;
  12. sellDateIndex = j;
  13. }
  14. }
  15. }
  16. System.out.println("Maximum Profit: " + profit + ", buy date index: " + buyDateIndex +
  17. ", sell date index: " + sellDateIndex);
  18. }
  19.  
  20. public static void main(String[] args) {
  21. int [] prices = { 200, 500, 1000, 700, 30, 400, 900, 400, 50};
  22. maxProfit(prices);
  23. }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement