Maximum Linear Stock Score - Problem

Given a 1-indexed integer array prices, where prices[i] is the price of a particular stock on the ith day, your task is to select some of the elements of prices such that your selection is linear.

A selection indexes, where indexes is a 1-indexed integer array of length k which is a subsequence of the array [1, 2, ..., n], is linear if:

For every 1 < j <= k, prices[indexes[j]] - prices[indexes[j - 1]] == indexes[j] - indexes[j - 1].

A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.

The score of a selection indexes is equal to the sum of the following array: [prices[indexes[1]], prices[indexes[2]], ..., prices[indexes[k]]].

Return the maximum score that a linear selection can have.

Input & Output

Example 1 — Linear Sequence
$ Input: prices = [1,3,5,7,9]
Output: 25
💡 Note: The entire array forms a linear sequence with slope 0: (3-1)-(2-1)=1, (5-3)-(3-2)=1, etc. All differences equal the index differences. Sum = 1+3+5+7+9 = 25.
Example 2 — Partial Linear
$ Input: prices = [1,5,3,7,9]
Output: 16
💡 Note: Best linear subsequence is [1,3,7] at indices [1,3,4]. Check: (3-1)-(3-1)=-0, (7-3)-(4-3)=3. Not linear. Actually [5,7,9] at indices [2,4,5]: (7-5)-(4-2)=0, (9-7)-(5-4)=1. Not linear either. The best is [1,5,9] = 15 or single element sequences. Actually [3,7] has slope (7-3)-(4-3)=3, extending gives [3,7,11] but 11 not in array.
Example 3 — Single Element
$ Input: prices = [10]
Output: 10
💡 Note: Only one element, so the maximum score is 10.

Constraints

  • 1 ≤ prices.length ≤ 1000
  • -106 ≤ prices[i] ≤ 106
  • prices is 1-indexed in the problem description

Visualization

Tap to expand
Maximum Linear Stock Score INPUT prices array (1-indexed) i=1 i=2 i=3 i=4 i=5 1 3 5 7 9 Linear Condition: prices[j] - prices[j-1] == indexes[j] - indexes[j-1] (same slope = 1) prices[i] - i = constant index i price ALGORITHM STEPS 1 Key Transformation Compute key = prices[i] - i 2 Use Hash Map Group by key, sum prices Hash Map (key: sum) i price key sum 1 1 0 1 2 3 1 3 3 5 2 5 4 7 3 7 5 9 4 9 3 All keys unique Each element forms own group 4 Find Maximum Sum All elements are linear! max = 1+3+5+7+9 = 25 FINAL RESULT All elements form linear selection 1 3 5 7 9 + + + + 1 + 3 + 5 + 7 + 9 Output: 25 OK - Verified! Maximum linear score = 25 Key Insight: The linear condition prices[j] - prices[j-1] == j - (j-1) means the difference is always 1. Transform: key = prices[i] - i. Elements with same key form a valid linear selection. Use Hash Map to group by key, sum prices for each group. Return maximum sum. Time: O(n), Space: O(n) TutorialsPoint - Maximum Linear Stock Score | Hash Map Optimization
Asked in
Google 15 Amazon 12 Microsoft 8
8.9K Views
Medium Frequency
~35 min Avg. Time
245 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen