From 9e10c5f9e97cab4b42b893031a54c910a0da5d61 Mon Sep 17 00:00:00 2001 From: Luca Lombardo Date: Mon, 6 Nov 2023 16:02:20 +0100 Subject: [PATCH] new problems solved, fixed old problems --- 2023_10_12/continuous-subarray-sum/Cargo.toml | 8 +++++ .../continuous-subarray-sum/src/main.rs | 34 +++++++++++++++++++ 2023_10_12/good-subarrays/Cargo.toml | 8 +++++ 2023_10_12/good-subarrays/src/main.rs | 3 ++ 2023_10_16/update-the-array/Cargo.toml | 8 +++++ 2023_10_16/update-the-array/src/main.rs | 3 ++ 6 files changed, 64 insertions(+) create mode 100644 2023_10_12/continuous-subarray-sum/Cargo.toml create mode 100644 2023_10_12/continuous-subarray-sum/src/main.rs create mode 100644 2023_10_12/good-subarrays/Cargo.toml create mode 100644 2023_10_12/good-subarrays/src/main.rs create mode 100644 2023_10_16/update-the-array/Cargo.toml create mode 100644 2023_10_16/update-the-array/src/main.rs diff --git a/2023_10_12/continuous-subarray-sum/Cargo.toml b/2023_10_12/continuous-subarray-sum/Cargo.toml new file mode 100644 index 0000000..a406bd5 --- /dev/null +++ b/2023_10_12/continuous-subarray-sum/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "continuous-subarray-sum" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/2023_10_12/continuous-subarray-sum/src/main.rs b/2023_10_12/continuous-subarray-sum/src/main.rs new file mode 100644 index 0000000..40587db --- /dev/null +++ b/2023_10_12/continuous-subarray-sum/src/main.rs @@ -0,0 +1,34 @@ +use std::collections::HashMap; + +impl Solution { + pub fn check_subarray_sum(nums: Vec, k: i32) -> bool { + let mut map: HashMap = HashMap::new(); + map.insert(0, -1); + let mut sum = 0; + for i in 0..nums.len() { + sum += nums[i]; + if k != 0 { + sum %= k; + } + match map.get(&sum) { + Some(prev) => { + if i as i32 - *prev > 1 { + return true; + } + }, + None => { + map.insert(sum, i as i32); + }, + } + } + false + } +} + +struct Solution; + +fn main() { + let nums = vec![23, 2, 4, 6, 7]; + let k = 6; + println!("{}", Solution::check_subarray_sum(nums, k)); +} diff --git a/2023_10_12/good-subarrays/Cargo.toml b/2023_10_12/good-subarrays/Cargo.toml new file mode 100644 index 0000000..6474846 --- /dev/null +++ b/2023_10_12/good-subarrays/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "good-subarrays" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/2023_10_12/good-subarrays/src/main.rs b/2023_10_12/good-subarrays/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/2023_10_12/good-subarrays/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/2023_10_16/update-the-array/Cargo.toml b/2023_10_16/update-the-array/Cargo.toml new file mode 100644 index 0000000..c980c60 --- /dev/null +++ b/2023_10_16/update-the-array/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "update-the-array" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/2023_10_16/update-the-array/src/main.rs b/2023_10_16/update-the-array/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/2023_10_16/update-the-array/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +}