The stock span for a given day is defined as the number of consecutive days immediately before the current day (including the current day itself) where the stock price was less than or equal to the current day price. For example, if prices are [100, 80, 60, 70, 60, 75, 85], the spans are [1, 1, 1, 2, 1, 4, 6]. Return null for null input and an empty list for an empty list.
Examples:
Input: prices = [100, 80, 60, 70, 60, 75, 85]
Output: [1, 1, 1, 2, 1, 4, 6]
Explanation: Day 1 (100) has span 1. Day 2 (80) has span 1. Day 3 (60) has span 1.
Day 4 (70) has span 2 because day 3 (60) is <= 70. Day 5 (60) has span 1.
Day 6 (75) has span 4 because days 3,4,5 are all <= 75. Day 7 (85) has span 6.
Input: prices = [10, 20, 30]
Output: [1, 2, 3]
Explanation: Each day price is higher than all previous days
Input: prices = [30, 20, 10]
Output: [1, 1, 1]
Explanation: Each day price is lower than the previous, so each span is 1
Input: prices = null
Output: null
Explanation: Null input returns null
Input: prices = []
Output: []
Explanation: Empty list returns empty list
Apex Code Editor
Welcome to Lightning Challenge!
Create an Account
Sign up to track your progress, earn points, and compete with others. Your solutions will be saved automatically.
Create accountHow It Works
- • Write your solution in the code editor
- • Connect your Salesforce org to test
- • Submit to check if your solution passes
- • Use hints if you get stuck
Contest Alert
🏆 #CodeEveryDay Mar/April 2026
Contest runs March 3 - April 30. Complete challenges to climb the leaderboard!
Points are calculated for challenges completed during the contest period.