Skip to content

Instantly share code, notes, and snippets.

@leosabbir
Last active May 27, 2018 03:39
Show Gist options
  • Save leosabbir/a636c9f13c2de60144495fc3b1ad0fe5 to your computer and use it in GitHub Desktop.
Save leosabbir/a636c9f13c2de60144495fc3b1ad0fe5 to your computer and use it in GitHub Desktop.
Compute maximum benefit possible by buying and selling an stock
/* File: StockBenefit.java
* Created: 2018-04-04
* Author: Sabbir Manandhar
*
* Copyright (c) 2018 Hogwart Inc.
*/
/**
* Compute potential max profit
*
* @author Sabbir Manandhar
* @version 1.0
*/
public class StockBenefit {
public static void main(String[] args) {
int[] stocks = new int[] {10, 7, 5, 8, 11, 9};
int profit = getMaxProfit(stocks);
int expectedProfit = 6;
verify(expectedProfit, profit);
stocks = new int[] {15, 5, 10, 25, 55, 1};
profit = getMaxProfit(stocks);
expectedProfit = 50;
verify(expectedProfit, profit);
stocks = new int[] {10, 9, 8, 7, 6, 5};
profit = getMaxProfit(stocks);
expectedProfit = -1;
verify(expectedProfit, profit);
stocks = new int[] {10, 10, 10, 10, 10, 10};
profit = getMaxProfit(stocks);
expectedProfit = 0;
verify(expectedProfit, profit);
stocks = new int[] {10, 7, 5, 8, 11, 9};
profit = getMaxProfit_2(stocks);
expectedProfit = 6;
verify(expectedProfit, profit);
stocks = new int[] {15, 5, 10, 25, 55, 1};
profit = getMaxProfit_2(stocks);
expectedProfit = 50;
verify(expectedProfit, profit);
stocks = new int[] {10, 9, 8, 7, 6, 5};
profit = getMaxProfit_2(stocks);
expectedProfit = -1;
verify(expectedProfit, profit);
stocks = new int[] {10, 10, 10, 10, 10, 10};
profit = getMaxProfit_2(stocks);
expectedProfit = 0;
verify(expectedProfit, profit);
} // main
//-------------------------------------------------------------------------------------
/**
* Assertion method to verify two values
* @param expected
* @param actual
*/
public static void verify(int expected, int actual) {
if (expected != actual) throw new AssertionError();
} // verify
//-------------------------------------------------------------------------------------
/**
* Brute Force Approach to calculate the profit
* @param stockPrices input stock prices
* @return max profit
*/
public static int getMaxProfit(int[] stockPrices) {
int maxProfit = Integer.MIN_VALUE;
for (int i = 0; i < stockPrices.length; i++) {
for (int j = i+1; j < stockPrices.length; j++) {
int profit = stockPrices[j] - stockPrices[i];
if (profit > maxProfit) {
maxProfit = profit;
}
}
}
return maxProfit;
} // getMaxProfit
//-------------------------------------------------------------------------------------
/**
* Linear Time solution to find the max profit
* @param stockPrices input stock prices
* @return max profit
*/
public static int getMaxProfit_2(int[] stockPrices) {
int maxProfit = Integer.MIN_VALUE;
int minPrice = stockPrices[0];
for (int i = 1; i < stockPrices.length; i++) {
int profit = stockPrices[i] - minPrice;
if (maxProfit < profit) {
maxProfit = profit;
}
if (minPrice > stockPrices[i]) {
minPrice = stockPrices[i];
}
}
return maxProfit;
} // getMaxProfit_2
} // StockBenefit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment