From 43b6f8411c9342495fc28328c80f4e8d03815a17 Mon Sep 17 00:00:00 2001 From: Ryan Li Date: Tue, 11 May 2021 21:15:24 +0800 Subject: [PATCH 1/3] feat: init and add tests --- Cargo.lock | 4 ++ Cargo.toml | 1 + .../Cargo.toml | 9 ++++ .../src/lib.rs | 50 +++++++++++++++++++ 4 files changed, 64 insertions(+) create mode 100644 maximum_points_you_can_obtain_from_cards/Cargo.toml create mode 100644 maximum_points_you_can_obtain_from_cards/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 326d429..cbd7203 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -48,6 +48,10 @@ version = "0.1.0" name = "longest_palindromic_substring" version = "0.1.0" +[[package]] +name = "maximum_points_you_can_obtain_from_cards" +version = "0.1.0" + [[package]] name = "merge_sorted_array" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index ce301d0..1790f05 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,4 +21,5 @@ members = [ "delete_operation_for_two_strings", "construct_target_array_with_multiple_sums", "count_primes", + "maximum_points_you_can_obtain_from_cards", ] diff --git a/maximum_points_you_can_obtain_from_cards/Cargo.toml b/maximum_points_you_can_obtain_from_cards/Cargo.toml new file mode 100644 index 0000000..cd9dda0 --- /dev/null +++ b/maximum_points_you_can_obtain_from_cards/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "maximum_points_you_can_obtain_from_cards" +version = "0.1.0" +authors = ["Ryan Li "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/maximum_points_you_can_obtain_from_cards/src/lib.rs b/maximum_points_you_can_obtain_from_cards/src/lib.rs new file mode 100644 index 0000000..d0cf67e --- /dev/null +++ b/maximum_points_you_can_obtain_from_cards/src/lib.rs @@ -0,0 +1,50 @@ +pub struct Solution {} + +impl Solution { + pub fn max_score(card_points: Vec, k: i32) -> i32 {} +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn example_1() { + let card_points = vec![1, 2, 3, 4, 5, 6, 1]; + let k = 3; + let expected = 12; + assert_eq!(Solution::max_score(card_points, k), expected); + } + + #[test] + fn example_2() { + let card_points = vec![2, 2, 2]; + let k = 2; + let expected = 4; + assert_eq!(Solution::max_score(card_points, k), expected); + } + + #[test] + fn example_3() { + let card_points = vec![9, 7, 7, 9, 7, 7, 9]; + let k = 7; + let expected = 55; + assert_eq!(Solution::max_score(card_points, k), expected); + } + + #[test] + fn example_4() { + let card_points = vec![1, 1000, 1]; + let k = 1; + let expected = 1; + assert_eq!(Solution::max_score(card_points, k), expected); + } + + #[test] + fn example_5() { + let card_points = vec![1, 79, 80, 1, 1, 1, 200, 1]; + let k = 3; + let expected = 202; + assert_eq!(Solution::max_score(card_points, k), expected); + } +} From 9f2cfb0ab37f26ceae13879f01bc93bdf8f2b1c3 Mon Sep 17 00:00:00 2001 From: Ryan Li Date: Tue, 11 May 2021 21:35:40 +0800 Subject: [PATCH 2/3] feat: resolved --- maximum_points_you_can_obtain_from_cards/src/lib.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/maximum_points_you_can_obtain_from_cards/src/lib.rs b/maximum_points_you_can_obtain_from_cards/src/lib.rs index d0cf67e..7136f07 100644 --- a/maximum_points_you_can_obtain_from_cards/src/lib.rs +++ b/maximum_points_you_can_obtain_from_cards/src/lib.rs @@ -1,7 +1,17 @@ pub struct Solution {} impl Solution { - pub fn max_score(card_points: Vec, k: i32) -> i32 {} + pub fn max_score(card_points: Vec, k: i32) -> i32 { + let k = k as usize; + let window_length = card_points.len() - k; + let mut sum = card_points[window_length..].iter().sum(); + let mut sums = vec![sum]; + for i in 0..k { + sum += card_points[i] - card_points[i + window_length]; + sums.push(sum); + } + *sums.iter().max().unwrap() + } } #[cfg(test)] From 41af11d619572c23b11957cfc5fed85f4fa6956f Mon Sep 17 00:00:00 2001 From: Ryan Li Date: Tue, 11 May 2021 21:53:54 +0800 Subject: [PATCH 3/3] feat: resolved with reduce --- maximum_points_you_can_obtain_from_cards/src/lib.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/maximum_points_you_can_obtain_from_cards/src/lib.rs b/maximum_points_you_can_obtain_from_cards/src/lib.rs index 7136f07..1114eee 100644 --- a/maximum_points_you_can_obtain_from_cards/src/lib.rs +++ b/maximum_points_you_can_obtain_from_cards/src/lib.rs @@ -4,13 +4,12 @@ impl Solution { pub fn max_score(card_points: Vec, k: i32) -> i32 { let k = k as usize; let window_length = card_points.len() - k; - let mut sum = card_points[window_length..].iter().sum(); - let mut sums = vec![sum]; - for i in 0..k { - sum += card_points[i] - card_points[i + window_length]; - sums.push(sum); - } - *sums.iter().max().unwrap() + let mut current: i32 = card_points[window_length..].iter().sum(); + let max = current; + (0..k).fold(max, |acc, i| { + current += card_points[i] - card_points[i + window_length]; + acc.max(current) + }) } }