Skip to content

[BUG] - Problem 901: Incorrect example for Online Stock Span #31788

@llukito

Description

@llukito

LeetCode Username

llukito

Problem Number, Title, and Link

  1. Online Stock Span https://leetcode.com/problems/online-stock-span/

Bug Category

Problem examples

Bug Description

In Problem 901. Online Stock Span, the description includes an incorrect example:

"If the prices of the stock in the last four days is [7,2,1,2] and the price of the stock today is 2, then the span of today is 4."

This is wrong, because according to the definition, the calculated span with those inputs is not 4.

Language Used for Code

Python/Python3

Code used for Submit/Run operation

class StockSpanner:

    def __init__(self):
        self.stack = []

    def next(self, price: int) -> int:
        span = 1
        while self.stack and self.stack[-1][0] <= price:
            span += self.stack.pop()[1]
        self.stack.append((price, span))
        return span


# Your StockSpanner object will be instantiated and called as such:
# obj = StockSpanner()
# param_1 = obj.next(price)

Expected behavior

The problem statement contains an incorrect example.

Excerpt from the description:
"If the prices of the stock in the last four days is [7,2,1,2] and the price of the stock today is 2, then the span of today is 4."

By the problem definition, the span is the number of consecutive days (including today) going backward for which the price was <= today's price (2). Counting backwards:

  • today: 2 <= 2 → count = 1
  • previous: 1 <= 2 → count = 2
  • previous: 2 <= 2 → count = 3
  • previous: 7 <= 2 → stop

So the correct span for that example is 3, not 4. This appears to be a typo in the example. Suggested fixes are below.

Screenshots

No response

Additional context

No response

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions