Maximum Linear Stock Score - Problem
Maximum Linear Stock Score
You're a stock analyst looking for linear patterns in stock prices over time. Given stock prices for consecutive days, your goal is to find the maximum sum of prices from a subsequence that follows a perfectly linear trend.
What makes a selection "linear"?
A selection of days is linear if the price changes at exactly the same rate as time passes. Mathematically, for any three consecutive selected days i, j, k:
Your Task: Find the subsequence of days with maximum total price sum that maintains this linear relationship.
Input: Array
Output: Maximum possible sum of prices from any linear subsequence
You're a stock analyst looking for linear patterns in stock prices over time. Given stock prices for consecutive days, your goal is to find the maximum sum of prices from a subsequence that follows a perfectly linear trend.
What makes a selection "linear"?
A selection of days is linear if the price changes at exactly the same rate as time passes. Mathematically, for any three consecutive selected days i, j, k:
(price[j] - price[i]) / (j - i) = (price[k] - j) / (k - j)Your Task: Find the subsequence of days with maximum total price sum that maintains this linear relationship.
Input: Array
prices where prices[i] is the stock price on day i+1Output: Maximum possible sum of prices from any linear subsequence
Input & Output
example_1.py โ Basic Linear Sequence
$
Input:
prices = [1, 3, 5, 7, 9]
โบ
Output:
25
๐ก Note:
The entire array forms a perfect linear sequence with slope 2. All prices sum to 1+3+5+7+9 = 25.
example_2.py โ Multiple Linear Subsequences
$
Input:
prices = [1, 5, 3, 7, 9]
โบ
Output:
16
๐ก Note:
Best linear subsequence is [1, 5, 9] with slope 4. Sum = 1+5+9 = 15. Or [3, 7] with sum 10. Actually [5, 7, 9] gives slope 2 and sum 21... Wait, let me recalculate: [5,7,9] has indices [1,3,4], slopes are (7-5)/(3-1)=1 and (9-7)/(4-3)=2, not linear. [1,5,9] has indices [0,1,4], slopes (5-1)/(1-0)=4 and (9-5)/(4-1)=4/3, not linear either. The maximum is taking the best 2-element sequence or single element.
example_3.py โ Single Element Edge Case
$
Input:
prices = [42]
โบ
Output:
42
๐ก Note:
With only one element, the maximum score is that single element value.
Constraints
- 1 โค prices.length โค 1000
- 1 โค prices[i] โค 105
- All prices are positive integers
- Array is 0-indexed but problem description uses 1-indexed terminology
Visualization
Tap to expand
Understanding the Visualization
1
Identify Slopes
For each pair of days, calculate the rate of price change (slope)
2
Group by Pattern
Group sequences that maintain the same rate of change
3
Build Incrementally
Extend existing patterns or start new ones at each position
4
Track Maximum
Keep track of the highest total value among all valid patterns
Key Takeaway
๐ฏ Key Insight: Every pair of points defines exactly one possible linear pattern. Use dynamic programming to efficiently build and compare all possible patterns while tracking the maximum sum.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code