The stock span problem is a financial problem where we have a series of n daily price quotes for a stock and we need to calculate span of stock’s price for all n days. The span Si of the stock’s price on a given day i is defined as the maximum number of consecutive days just before the given day, for which the price of the stock on the current day is less than or equal to its price on the given day. For example, if an array of 7 days prices is given as {100, 80, 60, 70, 60, 75, 85}, then the span values for corresponding 7 days are {1, 1, 1, 2, 1, 4, 6}
Looking for code review, optimization, and best practices.
public final class StockSpan {
private StockSpan() {}
public static int[] stockSpan(int[] stock) {
// we will use stack for indexes and not for "stock values"
final Stack<Integer> stack = new Stack<Integer>();
int[] span = new int[stock.length];
for (int i = 0; i < stock.length; i++) {
int index = 0;
span[i] = 1;
while (!stack.isEmpty() && stock[stack.peek()] <= stock[i]) {
index = stack.pop();
span[i] = i - index + span[index];
}
stack.push(i);
}
return span;
}
}
public class StockSpanTest {
@Test
public void test1() {
int price1[] = {100, 80, 60, 70, 60, 75, 85};
int expected1[] = {1, 1, 1, 2, 1, 4, 6};
assertTrue(Arrays.equals(expected1, StockSpan.stockSpan(price1)));
}
@Test
public void test2() {
int price2[] = {10, 4, 5, 90, 120, 80};
int expected2[] = {1, 1, 2, 4, 5, 1};
assertTrue(Arrays.equals(expected2, StockSpan.stockSpan(price2)));
}
}