Skip to content

Commit 105aae5

Browse files
feat: add StockSpanProblem algorithm
1 parent 5e06b15 commit 105aae5

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.thealgorithms.stacks;
2+
3+
import java.util.Stack;
4+
5+
/**
6+
* Calculates the stock span for each day in a series of stock prices.
7+
*
8+
* <p>The span of a price on a given day is the number of consecutive days ending on that day
9+
* for which the price was less than or equal to the current day's price.
10+
*
11+
*/
12+
public final class StockSpanProblem {
13+
private StockSpanProblem() {
14+
}
15+
16+
/**
17+
* Calculates the stock span for each price in the input array.
18+
*
19+
* @param prices the stock prices
20+
* @return the span for each day
21+
* @throws IllegalArgumentException if the input array is null
22+
*/
23+
public static int[] calculateSpan(int[] prices) {
24+
if (prices == null) {
25+
throw new IllegalArgumentException("Input prices cannot be null");
26+
}
27+
28+
int[] spans = new int[prices.length];
29+
Stack<Integer> stack = new Stack<>();
30+
31+
for (int index = 0; index < prices.length; index++) {
32+
while (!stack.isEmpty() && prices[stack.peek()] <= prices[index]) {
33+
stack.pop();
34+
}
35+
36+
spans[index] = stack.isEmpty() ? index + 1 : index - stack.peek();
37+
stack.push(index);
38+
}
39+
40+
return spans;
41+
}
42+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.thealgorithms.stacks;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import java.util.stream.Stream;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.Arguments;
9+
import org.junit.jupiter.params.provider.MethodSource;
10+
11+
class StockSpanProblemTest {
12+
13+
@ParameterizedTest
14+
@MethodSource("validTestCases")
15+
void testCalculateSpan(int[] prices, int[] expectedSpans) {
16+
assertArrayEquals(expectedSpans, StockSpanProblem.calculateSpan(prices));
17+
}
18+
19+
private static Stream<Arguments> validTestCases() {
20+
return Stream.of(Arguments.of(new int[] {10, 4, 5, 90, 120, 80}, new int[] {1, 1, 2, 4, 5, 1}), Arguments.of(new int[] {100, 50, 60, 70, 80, 90}, new int[] {1, 1, 2, 3, 4, 5}), Arguments.of(new int[] {5, 4, 3, 2, 1}, new int[] {1, 1, 1, 1, 1}),
21+
Arguments.of(new int[] {1, 2, 3, 4, 5}, new int[] {1, 2, 3, 4, 5}), Arguments.of(new int[] {10, 20, 30, 40, 50}, new int[] {1, 2, 3, 4, 5}), Arguments.of(new int[] {100, 80, 60, 70, 60, 75, 85}, new int[] {1, 1, 1, 2, 1, 4, 6}),
22+
Arguments.of(new int[] {7, 7, 7, 7}, new int[] {1, 2, 3, 4}), Arguments.of(new int[] {}, new int[] {}), Arguments.of(new int[] {42}, new int[] {1}));
23+
}
24+
25+
@ParameterizedTest
26+
@MethodSource("invalidTestCases")
27+
void testCalculateSpanInvalidInput(int[] prices) {
28+
assertThrows(IllegalArgumentException.class, () -> StockSpanProblem.calculateSpan(prices));
29+
}
30+
31+
private static Stream<Arguments> invalidTestCases() {
32+
return Stream.of(Arguments.of((int[]) null));
33+
}
34+
}

0 commit comments

Comments
 (0)