Skip to content

Commit 5c2e538

Browse files
feat: add OptimalBinarySearchTree algorithm
1 parent af047b0 commit 5c2e538

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

src/main/java/com/thealgorithms/stacks/StockSpanProblem.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
* While processing each day, pop smaller or equal prices because they are part of the current
1313
* span. After popping, the nearest greater price left on the stack tells us where the span stops.
1414
*
15+
* <p>Time complexity is O(n) because each index is pushed onto the stack once and popped at most
16+
* once, so the total number of stack operations grows linearly with the number of prices. This
17+
* makes the stack approach efficient because it avoids rechecking earlier days repeatedly, unlike
18+
* a naive nested-loop solution that can take O(n^2) time.
19+
*
1520
* <p>Example: for prices [100, 80, 60, 70, 60, 75, 85], the spans are
1621
* [1, 1, 1, 2, 1, 4, 6].
1722
*/

0 commit comments

Comments
 (0)