Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problem: No.2898 #1792

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
169 changes: 169 additions & 0 deletions solution/2800-2899/2898.Maximum Linear Stock Score/README.md
Original file line number Diff line number Diff line change
@@ -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)

## 题目描述

<!-- 这里写题目描述 -->

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

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

<ul>
<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>
</ul>

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

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

<p>Return <em>the <strong>maximum</strong> <strong>score</strong> that a linear selection can have</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> prices = [1,5,3,7,8]
<strong>Output:</strong> 20
<strong>Explanation:</strong> 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.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> prices = [5,6,7,8,9]
<strong>Output:</strong> 35
<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.
The sum of all the elements is 35 which is the maximum possible some out of every selection.</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>1 &lt;= prices.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= prices[i] &lt;= 10<sup>9</sup></code></li>
</ul>

## 解法

<!-- 这里可写通用的实现逻辑 -->

**方法一:哈希表**

我们可以将式子进行变换,得到:

$$
prices[i] - i = prices[j] - j
$$

题目实际上求的是相同的 $prices[i] - i$ 下,所有 $prices[i]$ 的和的最大值和。

因此,我们可以用一个哈希表 $cnt$ 来存储 $prices[i] - i$ 下,所有 $prices[i]$ 的和,最后取哈希表中的最大值即可。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $prices$ 的长度。

<!-- tabs:start -->

### **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<Integer, Long> 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<int>& prices) {
unordered_map<int, long long> 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<number, number> = 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());
}
```

### **...**

```

```

<!-- tabs:end -->
161 changes: 161 additions & 0 deletions solution/2800-2899/2898.Maximum Linear Stock Score/README_EN.md
Original file line number Diff line number Diff line change
@@ -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

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

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

<ul>
<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>
</ul>

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

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

<p>Return <em>the <strong>maximum</strong> <strong>score</strong> that a linear selection can have</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> prices = [1,5,3,7,8]
<strong>Output:</strong> 20
<strong>Explanation:</strong> 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.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> prices = [5,6,7,8,9]
<strong>Output:</strong> 35
<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.
The sum of all the elements is 35 which is the maximum possible some out of every selection.</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>1 &lt;= prices.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= prices[i] &lt;= 10<sup>9</sup></code></li>
</ul>

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

<!-- tabs:start -->

### **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<Integer, Long> 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<int>& prices) {
unordered_map<int, long long> 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<number, number> = 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());
}
```

### **...**

```

```

<!-- tabs:end -->
14 changes: 14 additions & 0 deletions solution/2800-2899/2898.Maximum Linear Stock Score/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
public:
long long maxScore(vector<int>& prices) {
unordered_map<int, long long> 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;
}
};
17 changes: 17 additions & 0 deletions solution/2800-2899/2898.Maximum Linear Stock Score/Solution.go
Original file line number Diff line number Diff line change
@@ -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
}
13 changes: 13 additions & 0 deletions solution/2800-2899/2898.Maximum Linear Stock Score/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution {
public long maxScore(int[] prices) {
Map<Integer, Long> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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())
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function maxScore(prices: number[]): number {
const cnt: Map<number, number> = 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());
}
Loading