From 340586fd9fb81dbb32286135ffe97a63ab631306 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sat, 14 Oct 2023 22:06:38 +0800 Subject: [PATCH] feat: add rust solution to lc problem: No.2898 No.2898.Maximum Linear Stock Score --- .../2898.Maximum Linear Stock Score/README.md | 20 +++++++++++++++++++ .../README_EN.md | 20 +++++++++++++++++++ .../Solution.rs | 15 ++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 solution/2800-2899/2898.Maximum Linear Stock Score/Solution.rs diff --git a/solution/2800-2899/2898.Maximum Linear Stock Score/README.md b/solution/2800-2899/2898.Maximum Linear Stock Score/README.md index 15113b1b7ed2d..9c7ddfe596cf9 100644 --- a/solution/2800-2899/2898.Maximum Linear Stock Score/README.md +++ b/solution/2800-2899/2898.Maximum Linear Stock Score/README.md @@ -147,6 +147,26 @@ func max(a, b int64) int64 { } ``` +### **Rust** + +```rust +use std::collections::HashMap; + +impl Solution { + pub fn max_score(prices: Vec) -> i64 { + let mut cnt: HashMap = HashMap::new(); + + for (i, x) in prices.iter().enumerate() { + let key = (*x) as i32 - (i as i32); + let count = cnt.entry(key).or_insert(0); + *count += *x as i64; + } + + *cnt.values().max().unwrap_or(&0) + } +} +``` + ### **TypeScript** ```ts 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 index bca0c1119fc44..0424276eb8fa0 100644 --- a/solution/2800-2899/2898.Maximum Linear Stock Score/README_EN.md +++ b/solution/2800-2899/2898.Maximum Linear Stock Score/README_EN.md @@ -139,6 +139,26 @@ func max(a, b int64) int64 { } ``` +### **Rust** + +```rust +use std::collections::HashMap; + +impl Solution { + pub fn max_score(prices: Vec) -> i64 { + let mut cnt: HashMap = HashMap::new(); + + for (i, x) in prices.iter().enumerate() { + let key = (*x) as i32 - (i as i32); + let count = cnt.entry(key).or_insert(0); + *count += *x as i64; + } + + *cnt.values().max().unwrap_or(&0) + } +} +``` + ### **TypeScript** ```ts diff --git a/solution/2800-2899/2898.Maximum Linear Stock Score/Solution.rs b/solution/2800-2899/2898.Maximum Linear Stock Score/Solution.rs new file mode 100644 index 0000000000000..55ce7c70e2747 --- /dev/null +++ b/solution/2800-2899/2898.Maximum Linear Stock Score/Solution.rs @@ -0,0 +1,15 @@ +use std::collections::HashMap; + +impl Solution { + pub fn max_score(prices: Vec) -> i64 { + let mut cnt: HashMap = HashMap::new(); + + for (i, x) in prices.iter().enumerate() { + let key = (*x) as i32 - (i as i32); + let count = cnt.entry(key).or_insert(0); + *count += *x as i64; + } + + *cnt.values().max().unwrap_or(&0) + } +} \ No newline at end of file