diff --git a/solution/2800-2899/2898.Maximum Linear Stock Score/README.md b/solution/2800-2899/2898.Maximum Linear Stock Score/README.md new file mode 100644 index 0000000000000..15113b1b7ed2d --- /dev/null +++ b/solution/2800-2899/2898.Maximum Linear Stock Score/README.md @@ -0,0 +1,169 @@ +# [2898. Maximum Linear Stock Score](https://leetcode.cn/problems/maximum-linear-stock-score) + +[English Version](/solution/2800-2899/2898.Maximum%20Linear%20Stock%20Score/README_EN.md) + +## 题目描述 + + + +

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:

+ + + +

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.

+ +

 

+

Example 1:

+ +
+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:
+For j = 2, we have:
+indexes[2] - indexes[1] = 4 - 2 = 2.
+prices[4] - prices[2] = 7 - 5 = 2.
+For j = 3, we have:
+indexes[3] - indexes[2] = 5 - 4 = 1.
+prices[5] - prices[4] = 8 - 7 = 1.
+The sum of the elements is: prices[2] + prices[4] + prices[5] = 20.
+It can be shown that the maximum sum a linear selection can have is 20.
+
+ +

Example 2:

+ +
+Input: prices = [5,6,7,8,9]
+Output: 35
+Explanation: We can select all of the indexes [1,2,3,4,5]. Since each element has a difference of exactly 1 from its previous element, our selection is linear.
+The sum of all the elements is 35 which is the maximum possible some out of every selection.
+ +

 

+

Constraints:

+ + + +## 解法 + + + +**方法一:哈希表** + +我们可以将式子进行变换,得到: + +$$ +prices[i] - i = prices[j] - j +$$ + +题目实际上求的是相同的 $prices[i] - i$ 下,所有 $prices[i]$ 的和的最大值和。 + +因此,我们可以用一个哈希表 $cnt$ 来存储 $prices[i] - i$ 下,所有 $prices[i]$ 的和,最后取哈希表中的最大值即可。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $prices$ 的长度。 + + + +### **Python3** + + + +```python +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()) +``` + +### **Java** + + + +```java +class Solution { + public long maxScore(int[] prices) { + Map cnt = new HashMap<>(); + for (int i = 0; i < prices.length; ++i) { + cnt.merge(prices[i] - i, (long) prices[i], Long::sum); + } + long ans = 0; + for (long v : cnt.values()) { + ans = Math.max(ans, v); + } + return ans; + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + long long maxScore(vector& prices) { + unordered_map cnt; + for (int i = 0; i < prices.size(); ++i) { + cnt[prices[i] - i] += prices[i]; + } + long long ans = 0; + for (auto& [_, v] : cnt) { + ans = max(ans, v); + } + return ans; + } +}; +``` + +### **Go** + +```go +func maxScore(prices []int) (ans int64) { + cnt := map[int]int{} + for i, x := range prices { + cnt[x-i] += x + } + for _, v := range cnt { + ans = max(ans, int64(v)) + } + return +} + +func max(a, b int64) int64 { + if a > b { + return a + } + return b +} +``` + +### **TypeScript** + +```ts +function maxScore(prices: number[]): number { + const cnt: Map = new Map(); + for (let i = 0; i < prices.length; ++i) { + const j = prices[i] - i; + cnt.set(j, (cnt.get(j) || 0) + prices[i]); + } + return Math.max(...cnt.values()); +} +``` + +### **...** + +``` + +``` + + diff --git a/solution/2800-2899/2898.Maximum Linear Stock Score/README_EN.md b/solution/2800-2899/2898.Maximum Linear Stock Score/README_EN.md new file mode 100644 index 0000000000000..bca0c1119fc44 --- /dev/null +++ b/solution/2800-2899/2898.Maximum Linear Stock Score/README_EN.md @@ -0,0 +1,161 @@ +# [2898. Maximum Linear Stock Score](https://leetcode.com/problems/maximum-linear-stock-score) + +[中文文档](/solution/2800-2899/2898.Maximum%20Linear%20Stock%20Score/README.md) + +## Description + +

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.

+ +

 

+

Example 1:

+ +
+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:
+For j = 2, we have:
+indexes[2] - indexes[1] = 4 - 2 = 2.
+prices[4] - prices[2] = 7 - 5 = 2.
+For j = 3, we have:
+indexes[3] - indexes[2] = 5 - 4 = 1.
+prices[5] - prices[4] = 8 - 7 = 1.
+The sum of the elements is: prices[2] + prices[4] + prices[5] = 20.
+It can be shown that the maximum sum a linear selection can have is 20.
+
+ +

Example 2:

+ +
+Input: prices = [5,6,7,8,9]
+Output: 35
+Explanation: We can select all of the indexes [1,2,3,4,5]. Since each element has a difference of exactly 1 from its previous element, our selection is linear.
+The sum of all the elements is 35 which is the maximum possible some out of every selection.
+ +

 

+

Constraints:

+ +
    +
  • 1 <= prices.length <= 105
  • +
  • 1 <= prices[i] <= 109
  • +
+ +## Solutions + +**Solution 1: Hash Table** + +We can transform the equation as follows: + +$$ +prices[i] - i = prices[j] - j +$$ + +In fact, the problem is to find the maximum sum of all $prices[i]$ under the same $prices[i] - i$. + +Therefore, we can use a hash table $cnt$ to store the sum of all $prices[i]$ under the same $prices[i] - i$, and finally take the maximum value in the hash table. + +The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the $prices$ array. + + + +### **Python3** + +```python +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()) +``` + +### **Java** + +```java +class Solution { + public long maxScore(int[] prices) { + Map cnt = new HashMap<>(); + for (int i = 0; i < prices.length; ++i) { + cnt.merge(prices[i] - i, (long) prices[i], Long::sum); + } + long ans = 0; + for (long v : cnt.values()) { + ans = Math.max(ans, v); + } + return ans; + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + long long maxScore(vector& prices) { + unordered_map cnt; + for (int i = 0; i < prices.size(); ++i) { + cnt[prices[i] - i] += prices[i]; + } + long long ans = 0; + for (auto& [_, v] : cnt) { + ans = max(ans, v); + } + return ans; + } +}; +``` + +### **Go** + +```go +func maxScore(prices []int) (ans int64) { + cnt := map[int]int{} + for i, x := range prices { + cnt[x-i] += x + } + for _, v := range cnt { + ans = max(ans, int64(v)) + } + return +} + +func max(a, b int64) int64 { + if a > b { + return a + } + return b +} +``` + +### **TypeScript** + +```ts +function maxScore(prices: number[]): number { + const cnt: Map = new Map(); + for (let i = 0; i < prices.length; ++i) { + const j = prices[i] - i; + cnt.set(j, (cnt.get(j) || 0) + prices[i]); + } + return Math.max(...cnt.values()); +} +``` + +### **...** + +``` + +``` + + diff --git a/solution/2800-2899/2898.Maximum Linear Stock Score/Solution.cpp b/solution/2800-2899/2898.Maximum Linear Stock Score/Solution.cpp new file mode 100644 index 0000000000000..4b1ebba38043b --- /dev/null +++ b/solution/2800-2899/2898.Maximum Linear Stock Score/Solution.cpp @@ -0,0 +1,14 @@ +class Solution { +public: + long long maxScore(vector& prices) { + unordered_map cnt; + for (int i = 0; i < prices.size(); ++i) { + cnt[prices[i] - i] += prices[i]; + } + long long ans = 0; + for (auto& [_, v] : cnt) { + ans = max(ans, v); + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/2800-2899/2898.Maximum Linear Stock Score/Solution.go b/solution/2800-2899/2898.Maximum Linear Stock Score/Solution.go new file mode 100644 index 0000000000000..cac88bd51b2a7 --- /dev/null +++ b/solution/2800-2899/2898.Maximum Linear Stock Score/Solution.go @@ -0,0 +1,17 @@ +func maxScore(prices []int) (ans int64) { + cnt := map[int]int{} + for i, x := range prices { + cnt[x-i] += x + } + for _, v := range cnt { + ans = max(ans, int64(v)) + } + return +} + +func max(a, b int64) int64 { + if a > b { + return a + } + return b +} \ No newline at end of file diff --git a/solution/2800-2899/2898.Maximum Linear Stock Score/Solution.java b/solution/2800-2899/2898.Maximum Linear Stock Score/Solution.java new file mode 100644 index 0000000000000..2870c78a2276d --- /dev/null +++ b/solution/2800-2899/2898.Maximum Linear Stock Score/Solution.java @@ -0,0 +1,13 @@ +class Solution { + public long maxScore(int[] prices) { + Map cnt = new HashMap<>(); + for (int i = 0; i < prices.length; ++i) { + cnt.merge(prices[i] - i, (long) prices[i], Long::sum); + } + long ans = 0; + for (long v : cnt.values()) { + ans = Math.max(ans, v); + } + return ans; + } +} \ No newline at end of file diff --git a/solution/2800-2899/2898.Maximum Linear Stock Score/Solution.py b/solution/2800-2899/2898.Maximum Linear Stock Score/Solution.py new file mode 100644 index 0000000000000..a9ed882e678d7 --- /dev/null +++ b/solution/2800-2899/2898.Maximum Linear Stock Score/Solution.py @@ -0,0 +1,6 @@ +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()) diff --git a/solution/2800-2899/2898.Maximum Linear Stock Score/Solution.ts b/solution/2800-2899/2898.Maximum Linear Stock Score/Solution.ts new file mode 100644 index 0000000000000..3ea22781874a1 --- /dev/null +++ b/solution/2800-2899/2898.Maximum Linear Stock Score/Solution.ts @@ -0,0 +1,8 @@ +function maxScore(prices: number[]): number { + const cnt: Map = new Map(); + for (let i = 0; i < prices.length; ++i) { + const j = prices[i] - i; + cnt.set(j, (cnt.get(j) || 0) + prices[i]); + } + return Math.max(...cnt.values()); +} diff --git a/solution/README.md b/solution/README.md index 88bad520d9390..3be591496a530 100644 --- a/solution/README.md +++ b/solution/README.md @@ -2908,6 +2908,7 @@ | 2895 | [最小处理时间](/solution/2800-2899/2895.Minimum%20Processing%20Time/README.md) | | 中等 | 第 366 场周赛 | | 2896 | [执行操作使两个字符串相等](/solution/2800-2899/2896.Apply%20Operations%20to%20Make%20Two%20Strings%20Equal/README.md) | | 中等 | 第 366 场周赛 | | 2897 | [对数组执行操作使平方和最大](/solution/2800-2899/2897.Apply%20Operations%20on%20Array%20to%20Maximize%20Sum%20of%20Squares/README.md) | | 困难 | 第 366 场周赛 | +| 2898 | [Maximum Linear Stock Score](/solution/2800-2899/2898.Maximum%20Linear%20Stock%20Score/README.md) | | 中等 | 🔒 | ## 版权 diff --git a/solution/README_EN.md b/solution/README_EN.md index 2cf2695793f69..f2b0816a18ef8 100644 --- a/solution/README_EN.md +++ b/solution/README_EN.md @@ -2906,6 +2906,7 @@ Press Control + F(or Command + F on | 2895 | [Minimum Processing Time](/solution/2800-2899/2895.Minimum%20Processing%20Time/README_EN.md) | | Medium | Weekly Contest 366 | | 2896 | [Apply Operations to Make Two Strings Equal](/solution/2800-2899/2896.Apply%20Operations%20to%20Make%20Two%20Strings%20Equal/README_EN.md) | | Medium | Weekly Contest 366 | | 2897 | [Apply Operations on Array to Maximize Sum of Squares](/solution/2800-2899/2897.Apply%20Operations%20on%20Array%20to%20Maximize%20Sum%20of%20Squares/README_EN.md) | | Hard | Weekly Contest 366 | +| 2898 | [Maximum Linear Stock Score](/solution/2800-2899/2898.Maximum%20Linear%20Stock%20Score/README_EN.md) | | Medium | 🔒 | ## Copyright diff --git a/solution/summary.md b/solution/summary.md index 2c9bb04e7288b..a211fac722fa4 100644 --- a/solution/summary.md +++ b/solution/summary.md @@ -2953,3 +2953,4 @@ - [2895.最小处理时间](/solution/2800-2899/2895.Minimum%20Processing%20Time/README.md) - [2896.执行操作使两个字符串相等](/solution/2800-2899/2896.Apply%20Operations%20to%20Make%20Two%20Strings%20Equal/README.md) - [2897.对数组执行操作使平方和最大](/solution/2800-2899/2897.Apply%20Operations%20on%20Array%20to%20Maximize%20Sum%20of%20Squares/README.md) + - [2898.Maximum Linear Stock Score](/solution/2800-2899/2898.Maximum%20Linear%20Stock%20Score/README.md) diff --git a/solution/summary_en.md b/solution/summary_en.md index fa7e7f91712ba..18568eff6bbd2 100644 --- a/solution/summary_en.md +++ b/solution/summary_en.md @@ -2953,3 +2953,4 @@ - [2895.Minimum Processing Time](/solution/2800-2899/2895.Minimum%20Processing%20Time/README_EN.md) - [2896.Apply Operations to Make Two Strings Equal](/solution/2800-2899/2896.Apply%20Operations%20to%20Make%20Two%20Strings%20Equal/README_EN.md) - [2897.Apply Operations on Array to Maximize Sum of Squares](/solution/2800-2899/2897.Apply%20Operations%20on%20Array%20to%20Maximize%20Sum%20of%20Squares/README_EN.md) + - [2898.Maximum Linear Stock Score](/solution/2800-2899/2898.Maximum%20Linear%20Stock%20Score/README_EN.md)