Maximum Linear Stock Score — LeetCode 2898 Python Solution
- Problem
- #2898
- Pattern
- Hash Map
- Reading time
- 2 min
- Source
- leetcode.com
The 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].
Example
- Input
- prices = [1,5,3,7,8]
- Output
- 20
- Explanation
- We can select the indexes [2,4,5]. We show that our selection is linear:
Python solution
class Solution:
def maxScore(self, prices: List[int]) -> int:
cnt = Counter()
for i, x in enumerate(prices):
cnt[x - i] += x
return max(cnt.values())Complexity
| Measure | Complexity |
|---|---|
| Time | O(n) |
| Space | O(n), where n is the length of the prices array auxiliary |
Pattern: Hash Map
Trade memory for time: remember what you have seen so the second pass never happens. LeetCode 2898. Maximum Linear Stock Score is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Hash Table.
The hash map guide has the Python template for the pattern and the 709 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 2898. Maximum Linear Stock Score?
- LeetCode 2898. Maximum Linear Stock Score is rated Medium on LeetCode.
- What is the time complexity of LeetCode 2898. Maximum Linear Stock Score?
- The Python solution on this page runs in O(n).
- What is the space complexity of LeetCode 2898. Maximum Linear Stock Score?
- The Python solution on this page uses O(n), where n is the length of the prices array auxiliary space.
- What topics does LeetCode 2898. Maximum Linear Stock Score cover?
- LeetCode 2898. Maximum Linear Stock Score is tagged Array and Hash Table on LeetCode.
- Is LeetCode 2898. Maximum Linear Stock Score a premium problem?
- Yes. LeetCode 2898. Maximum Linear Stock Score is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.