Skip to content

Commit 7ea8094

Browse files
authoredOct 14, 2023
feat: add rust solution to lc problem: No.2898 (#1804)
No.2898.Maximum Linear Stock Score
1 parent 7ec7a3f commit 7ea8094

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed
 

‎solution/2800-2899/2898.Maximum Linear Stock Score/README.md

+20
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,26 @@ func max(a, b int64) int64 {
147147
}
148148
```
149149

150+
### **Rust**
151+
152+
```rust
153+
use std::collections::HashMap;
154+
155+
impl Solution {
156+
pub fn max_score(prices: Vec<i32>) -> i64 {
157+
let mut cnt: HashMap<i32, i64> = HashMap::new();
158+
159+
for (i, x) in prices.iter().enumerate() {
160+
let key = (*x) as i32 - (i as i32);
161+
let count = cnt.entry(key).or_insert(0);
162+
*count += *x as i64;
163+
}
164+
165+
*cnt.values().max().unwrap_or(&0)
166+
}
167+
}
168+
```
169+
150170
### **TypeScript**
151171

152172
```ts

‎solution/2800-2899/2898.Maximum Linear Stock Score/README_EN.md

+20
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,26 @@ func max(a, b int64) int64 {
139139
}
140140
```
141141

142+
### **Rust**
143+
144+
```rust
145+
use std::collections::HashMap;
146+
147+
impl Solution {
148+
pub fn max_score(prices: Vec<i32>) -> i64 {
149+
let mut cnt: HashMap<i32, i64> = HashMap::new();
150+
151+
for (i, x) in prices.iter().enumerate() {
152+
let key = (*x) as i32 - (i as i32);
153+
let count = cnt.entry(key).or_insert(0);
154+
*count += *x as i64;
155+
}
156+
157+
*cnt.values().max().unwrap_or(&0)
158+
}
159+
}
160+
```
161+
142162
### **TypeScript**
143163

144164
```ts
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use std::collections::HashMap;
2+
3+
impl Solution {
4+
pub fn max_score(prices: Vec<i32>) -> i64 {
5+
let mut cnt: HashMap<i32, i64> = HashMap::new();
6+
7+
for (i, x) in prices.iter().enumerate() {
8+
let key = (*x) as i32 - (i as i32);
9+
let count = cnt.entry(key).or_insert(0);
10+
*count += *x as i64;
11+
}
12+
13+
*cnt.values().max().unwrap_or(&0)
14+
}
15+
}

0 commit comments

Comments
 (0)
Please sign in to comment.