Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fc10223

Browse files
feat: add solutions to lc problem: No.2898 (#1792)
No.2898.Maximum Linear Stock Score
1 parent 102e2c3 commit fc10223

File tree

11 files changed

+392
-0
lines changed

11 files changed

+392
-0
lines changed
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
# [2898. Maximum Linear Stock Score](https://leetcode.cn/problems/maximum-linear-stock-score)
2+
3+
[English Version](/solution/2800-2899/2898.Maximum%20Linear%20Stock%20Score/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>Given a <strong>1-indexed</strong> integer array <code>prices</code>, where <code>prices[i]</code> is the price of a particular stock on the <code>i<sup>th</sup></code> day, your task is to select some of the elements of <code>prices</code> such that your selection is <strong>linear</strong>.</p>
10+
11+
<p>A selection <code>indexes</code>, where <code>indexes</code> is a <strong>1-indexed</strong> integer array of length <code>k</code> which is a subsequence of the array <code>[1, 2, ..., n]</code>, is <strong>linear</strong> if:</p>
12+
13+
<ul>
14+
<li>For every <code>1 &lt; j &lt;= k</code>, <code>prices[indexes[j]] - prices[indexes[j - 1]] == indexes[j] - indexes[j - 1]</code>.</li>
15+
</ul>
16+
17+
<p>A <b>subsequence</b> is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.</p>
18+
19+
<p>The <strong>score</strong> of a selection <code>indexes</code>, is equal to the sum of the following array: <code>[prices[indexes[1]], prices[indexes[2]], ..., prices[indexes[k]]</code>.</p>
20+
21+
<p>Return <em>the <strong>maximum</strong> <strong>score</strong> that a linear selection can have</em>.</p>
22+
23+
<p>&nbsp;</p>
24+
<p><strong class="example">Example 1:</strong></p>
25+
26+
<pre>
27+
<strong>Input:</strong> prices = [1,5,3,7,8]
28+
<strong>Output:</strong> 20
29+
<strong>Explanation:</strong> We can select the indexes [2,4,5]. We show that our selection is linear:
30+
For j = 2, we have:
31+
indexes[2] - indexes[1] = 4 - 2 = 2.
32+
prices[4] - prices[2] = 7 - 5 = 2.
33+
For j = 3, we have:
34+
indexes[3] - indexes[2] = 5 - 4 = 1.
35+
prices[5] - prices[4] = 8 - 7 = 1.
36+
The sum of the elements is: prices[2] + prices[4] + prices[5] = 20.
37+
It can be shown that the maximum sum a linear selection can have is 20.
38+
</pre>
39+
40+
<p><strong class="example">Example 2:</strong></p>
41+
42+
<pre>
43+
<strong>Input:</strong> prices = [5,6,7,8,9]
44+
<strong>Output:</strong> 35
45+
<strong>Explanation:</strong> 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.
46+
The sum of all the elements is 35 which is the maximum possible some out of every selection.</pre>
47+
48+
<p>&nbsp;</p>
49+
<p><strong>Constraints:</strong></p>
50+
51+
<ul>
52+
<li><code>1 &lt;= prices.length &lt;= 10<sup>5</sup></code></li>
53+
<li><code>1 &lt;= prices[i] &lt;= 10<sup>9</sup></code></li>
54+
</ul>
55+
56+
## 解法
57+
58+
<!-- 这里可写通用的实现逻辑 -->
59+
60+
**方法一:哈希表**
61+
62+
我们可以将式子进行变换,得到:
63+
64+
$$
65+
prices[i] - i = prices[j] - j
66+
$$
67+
68+
题目实际上求的是相同的 $prices[i] - i$ 下,所有 $prices[i]$ 的和的最大值和。
69+
70+
因此,我们可以用一个哈希表 $cnt$ 来存储 $prices[i] - i$ 下,所有 $prices[i]$ 的和,最后取哈希表中的最大值即可。
71+
72+
时间复杂度 $O(n),ドル空间复杂度 $O(n)$。其中 $n$ 为数组 $prices$ 的长度。
73+
74+
<!-- tabs:start -->
75+
76+
### **Python3**
77+
78+
<!-- 这里可写当前语言的特殊实现逻辑 -->
79+
80+
```python
81+
class Solution:
82+
def maxScore(self, prices: List[int]) -> int:
83+
cnt = Counter()
84+
for i, x in enumerate(prices):
85+
cnt[x - i] += x
86+
return max(cnt.values())
87+
```
88+
89+
### **Java**
90+
91+
<!-- 这里可写当前语言的特殊实现逻辑 -->
92+
93+
```java
94+
class Solution {
95+
public long maxScore(int[] prices) {
96+
Map<Integer, Long> cnt = new HashMap<>();
97+
for (int i = 0; i < prices.length; ++i) {
98+
cnt.merge(prices[i] - i, (long) prices[i], Long::sum);
99+
}
100+
long ans = 0;
101+
for (long v : cnt.values()) {
102+
ans = Math.max(ans, v);
103+
}
104+
return ans;
105+
}
106+
}
107+
```
108+
109+
### **C++**
110+
111+
```cpp
112+
class Solution {
113+
public:
114+
long long maxScore(vector<int>& prices) {
115+
unordered_map<int, long long> cnt;
116+
for (int i = 0; i < prices.size(); ++i) {
117+
cnt[prices[i] - i] += prices[i];
118+
}
119+
long long ans = 0;
120+
for (auto& [_, v] : cnt) {
121+
ans = max(ans, v);
122+
}
123+
return ans;
124+
}
125+
};
126+
```
127+
128+
### **Go**
129+
130+
```go
131+
func maxScore(prices []int) (ans int64) {
132+
cnt := map[int]int{}
133+
for i, x := range prices {
134+
cnt[x-i] += x
135+
}
136+
for _, v := range cnt {
137+
ans = max(ans, int64(v))
138+
}
139+
return
140+
}
141+
142+
func max(a, b int64) int64 {
143+
if a > b {
144+
return a
145+
}
146+
return b
147+
}
148+
```
149+
150+
### **TypeScript**
151+
152+
```ts
153+
function maxScore(prices: number[]): number {
154+
const cnt: Map<number, number> = new Map();
155+
for (let i = 0; i < prices.length; ++i) {
156+
const j = prices[i] - i;
157+
cnt.set(j, (cnt.get(j) || 0) + prices[i]);
158+
}
159+
return Math.max(...cnt.values());
160+
}
161+
```
162+
163+
### **...**
164+
165+
```
166+
167+
```
168+
169+
<!-- tabs:end -->
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
# [2898. Maximum Linear Stock Score](https://leetcode.com/problems/maximum-linear-stock-score)
2+
3+
[中文文档](/solution/2800-2899/2898.Maximum%20Linear%20Stock%20Score/README.md)
4+
5+
## Description
6+
7+
<p>Given a <strong>1-indexed</strong> integer array <code>prices</code>, where <code>prices[i]</code> is the price of a particular stock on the <code>i<sup>th</sup></code> day, your task is to select some of the elements of <code>prices</code> such that your selection is <strong>linear</strong>.</p>
8+
9+
<p>A selection <code>indexes</code>, where <code>indexes</code> is a <strong>1-indexed</strong> integer array of length <code>k</code> which is a subsequence of the array <code>[1, 2, ..., n]</code>, is <strong>linear</strong> if:</p>
10+
11+
<ul>
12+
<li>For every <code>1 &lt; j &lt;= k</code>, <code>prices[indexes[j]] - prices[indexes[j - 1]] == indexes[j] - indexes[j - 1]</code>.</li>
13+
</ul>
14+
15+
<p>A <b>subsequence</b> is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.</p>
16+
17+
<p>The <strong>score</strong> of a selection <code>indexes</code>, is equal to the sum of the following array: <code>[prices[indexes[1]], prices[indexes[2]], ..., prices[indexes[k]]</code>.</p>
18+
19+
<p>Return <em>the <strong>maximum</strong> <strong>score</strong> that a linear selection can have</em>.</p>
20+
21+
<p>&nbsp;</p>
22+
<p><strong class="example">Example 1:</strong></p>
23+
24+
<pre>
25+
<strong>Input:</strong> prices = [1,5,3,7,8]
26+
<strong>Output:</strong> 20
27+
<strong>Explanation:</strong> We can select the indexes [2,4,5]. We show that our selection is linear:
28+
For j = 2, we have:
29+
indexes[2] - indexes[1] = 4 - 2 = 2.
30+
prices[4] - prices[2] = 7 - 5 = 2.
31+
For j = 3, we have:
32+
indexes[3] - indexes[2] = 5 - 4 = 1.
33+
prices[5] - prices[4] = 8 - 7 = 1.
34+
The sum of the elements is: prices[2] + prices[4] + prices[5] = 20.
35+
It can be shown that the maximum sum a linear selection can have is 20.
36+
</pre>
37+
38+
<p><strong class="example">Example 2:</strong></p>
39+
40+
<pre>
41+
<strong>Input:</strong> prices = [5,6,7,8,9]
42+
<strong>Output:</strong> 35
43+
<strong>Explanation:</strong> 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.
44+
The sum of all the elements is 35 which is the maximum possible some out of every selection.</pre>
45+
46+
<p>&nbsp;</p>
47+
<p><strong>Constraints:</strong></p>
48+
49+
<ul>
50+
<li><code>1 &lt;= prices.length &lt;= 10<sup>5</sup></code></li>
51+
<li><code>1 &lt;= prices[i] &lt;= 10<sup>9</sup></code></li>
52+
</ul>
53+
54+
## Solutions
55+
56+
**Solution 1: Hash Table**
57+
58+
We can transform the equation as follows:
59+
60+
$$
61+
prices[i] - i = prices[j] - j
62+
$$
63+
64+
In fact, the problem is to find the maximum sum of all $prices[i]$ under the same $prices[i] - i$.
65+
66+
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.
67+
68+
The time complexity is $O(n),ドル and the space complexity is $O(n),ドル where $n$ is the length of the $prices$ array.
69+
70+
<!-- tabs:start -->
71+
72+
### **Python3**
73+
74+
```python
75+
class Solution:
76+
def maxScore(self, prices: List[int]) -> int:
77+
cnt = Counter()
78+
for i, x in enumerate(prices):
79+
cnt[x - i] += x
80+
return max(cnt.values())
81+
```
82+
83+
### **Java**
84+
85+
```java
86+
class Solution {
87+
public long maxScore(int[] prices) {
88+
Map<Integer, Long> cnt = new HashMap<>();
89+
for (int i = 0; i < prices.length; ++i) {
90+
cnt.merge(prices[i] - i, (long) prices[i], Long::sum);
91+
}
92+
long ans = 0;
93+
for (long v : cnt.values()) {
94+
ans = Math.max(ans, v);
95+
}
96+
return ans;
97+
}
98+
}
99+
```
100+
101+
### **C++**
102+
103+
```cpp
104+
class Solution {
105+
public:
106+
long long maxScore(vector<int>& prices) {
107+
unordered_map<int, long long> cnt;
108+
for (int i = 0; i < prices.size(); ++i) {
109+
cnt[prices[i] - i] += prices[i];
110+
}
111+
long long ans = 0;
112+
for (auto& [_, v] : cnt) {
113+
ans = max(ans, v);
114+
}
115+
return ans;
116+
}
117+
};
118+
```
119+
120+
### **Go**
121+
122+
```go
123+
func maxScore(prices []int) (ans int64) {
124+
cnt := map[int]int{}
125+
for i, x := range prices {
126+
cnt[x-i] += x
127+
}
128+
for _, v := range cnt {
129+
ans = max(ans, int64(v))
130+
}
131+
return
132+
}
133+
134+
func max(a, b int64) int64 {
135+
if a > b {
136+
return a
137+
}
138+
return b
139+
}
140+
```
141+
142+
### **TypeScript**
143+
144+
```ts
145+
function maxScore(prices: number[]): number {
146+
const cnt: Map<number, number> = new Map();
147+
for (let i = 0; i < prices.length; ++i) {
148+
const j = prices[i] - i;
149+
cnt.set(j, (cnt.get(j) || 0) + prices[i]);
150+
}
151+
return Math.max(...cnt.values());
152+
}
153+
```
154+
155+
### **...**
156+
157+
```
158+
159+
```
160+
161+
<!-- tabs:end -->
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
long long maxScore(vector<int>& prices) {
4+
unordered_map<int, long long> cnt;
5+
for (int i = 0; i < prices.size(); ++i) {
6+
cnt[prices[i] - i] += prices[i];
7+
}
8+
long long ans = 0;
9+
for (auto& [_, v] : cnt) {
10+
ans = max(ans, v);
11+
}
12+
return ans;
13+
}
14+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
func maxScore(prices []int) (ans int64) {
2+
cnt := map[int]int{}
3+
for i, x := range prices {
4+
cnt[x-i] += x
5+
}
6+
for _, v := range cnt {
7+
ans = max(ans, int64(v))
8+
}
9+
return
10+
}
11+
12+
func max(a, b int64) int64 {
13+
if a > b {
14+
return a
15+
}
16+
return b
17+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public long maxScore(int[] prices) {
3+
Map<Integer, Long> cnt = new HashMap<>();
4+
for (int i = 0; i < prices.length; ++i) {
5+
cnt.merge(prices[i] - i, (long) prices[i], Long::sum);
6+
}
7+
long ans = 0;
8+
for (long v : cnt.values()) {
9+
ans = Math.max(ans, v);
10+
}
11+
return ans;
12+
}
13+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution:
2+
def maxScore(self, prices: List[int]) -> int:
3+
cnt = Counter()
4+
for i, x in enumerate(prices):
5+
cnt[x - i] += x
6+
return max(cnt.values())
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function maxScore(prices: number[]): number {
2+
const cnt: Map<number, number> = new Map();
3+
for (let i = 0; i < prices.length; ++i) {
4+
const j = prices[i] - i;
5+
cnt.set(j, (cnt.get(j) || 0) + prices[i]);
6+
}
7+
return Math.max(...cnt.values());
8+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /