[LeetCode] Maximum Linear Stock Score

2898. Maximum Linear Stock Score

1
>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

A selection indexes, where indexes``k which is a subsequence of the array [1, 2, ..., n]

  • 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.

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.

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
long long maxScore(vector<int>& prices) {
unordered_map<int, long long> best;
long long res = 0;
for(int i = 0; i < prices.size(); i++) {
res = max(res, (best[prices[i]-i] += prices[i]));
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/12/11/PS/LeetCode/maximum-linear-stock-score/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.