diff --git a/2023_10_02/maximum-path-sum/Cargo.toml b/2023_10_02/maximum-path-sum/Cargo.toml new file mode 100644 index 0000000..ef62e33 --- /dev/null +++ b/2023_10_02/maximum-path-sum/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "maximum-path-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_02/maximum-path-sum/src/main.rs b/2023_10_02/maximum-path-sum/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/2023_10_02/maximum-path-sum/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/2023_10_05/longest-k-good-segment/Cargo.toml b/2023_10_05/longest-k-good-segment/Cargo.toml new file mode 100644 index 0000000..f5e5734 --- /dev/null +++ b/2023_10_05/longest-k-good-segment/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "longest-k-good-segment" +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_05/longest-k-good-segment/README.md b/2023_10_05/longest-k-good-segment/README.md new file mode 100644 index 0000000..256a768 --- /dev/null +++ b/2023_10_05/longest-k-good-segment/README.md @@ -0,0 +1,84 @@ +# Comments on the solution + +The problem requires finding the longest segment of an array that contains no more than `k` different values. We can use a sliding window approach. We maintain a hashmap to store the count of each value in the current segment. We start with the left and right indices at 0 and keep incrementing the right index until the number of different values in the segment exceeds `k`. At this point, we increment the left index until the number of different values in the segment is less than or equal to `k`. We keep track of the maximum length of the segment seen so far and return the left and right indices of this segment. + +### Complexity analysis + +* **Time complexity**: $O(n)$ since we only iterate through the array once and perform constant time operations on each element. + +* **Space complexity**: $O(k)$ since we store the count of each value in the hashmap. + +--- + +### Rust Solution + +This version reads the input from stdin and writes the output to stdout (as required by the problem statement) + +```rust +fn main() { + let mut input = String::new(); + std::io::stdin().read_line(&mut input).unwrap(); + let mut iter = input.split_whitespace(); + let n: usize = iter.next().unwrap().parse().unwrap(); + let k: usize = iter.next().unwrap().parse().unwrap(); + + let mut input = String::new(); + std::io::stdin().read_line(&mut input).unwrap(); + let mut iter = input.split_whitespace(); + let mut a: Vec = Vec::with_capacity(n); + for _ in 0..n { + a.push(iter.next().unwrap().parse().unwrap()); + } + + let mut l = 0; + let mut r = 0; + let mut max = 0; + let mut map: std::collections::HashMap = std::collections::HashMap::new(); + while r < n { + let count = map.entry(a[r]).or_insert(0); + *count += 1; + while map.len() > k { + let count = map.entry(a[l]).or_insert(0); + *count -= 1; + if *count == 0 { + map.remove(&a[l]); + } + l += 1; + } + if r - l + 1 > max { + max = r - l + 1; + } + r += 1; + } + println!("{} {}", l + 1, r); +} +``` + +Here is a version that takes the input as arguments and returns the output as a tuple. + + +```rust +fn find_k_good_segment(n: usize, k: usize, a: &[usize]) -> (usize, usize) { + let mut l = 0; + let mut r = 0; + let mut max = 0; + let mut map: std::collections::HashMap = std::collections::HashMap::new(); + while r < n { + let count = map.entry(a[r]).or_insert(0); + *count += 1; + while map.len() > k { + let count = map.entry(a[l]).or_insert(0); + *count -= 1; + if *count == 0 { + map.remove(&a[l]); + } + l += 1; + } + if r - l + 1 > max { + max = r - l + 1; + } + r += 1; + } + (l + 1, r) +} +``` diff --git a/2023_10_05/longest-k-good-segment/src/main.rs b/2023_10_05/longest-k-good-segment/src/main.rs new file mode 100644 index 0000000..46a6bcd --- /dev/null +++ b/2023_10_05/longest-k-good-segment/src/main.rs @@ -0,0 +1,26 @@ +fn main() { + // reads two integers n and k from the standard input. + let mut input = String::new(); + std::io::stdin().read_line(&mut input).unwrap(); // read line from stdin + let mut iter = input.split_whitespace(); // split input string by whitespace + let n: usize = iter.next().unwrap().parse().unwrap(); // parse first element to usize + let k: usize = iter.next().unwrap().parse().unwrap(); // parse second element to usize + + // reads n integers from the standard input and stores them in an array a. + let mut input = String::new(); // input string + std::io::stdin().read_line(&mut input).unwrap(); // read line from stdin + let mut iter = input.split_whitespace(); // split input string by whitespace + let mut a: Vec = Vec::with_capacity(n); // create vector with capacity n + for _ in 0..n { + a.push(iter.next().unwrap().parse().unwrap()); // parse each element to usize and push to vector + } + + let mut l = 0; // left index + let mut r = 0; // right index + let mut max = 0; // max length + let mut count = 0; // count of different elements + + // We maintain a hashmap to store the count of each value in the current segment. We start with the left and right indices at 0 and keep incrementing the right index until the number of different values in the segment exceeds `k`. At this point, we increment the left index until the number of different values in the segment is less than or equal to `k`. We keep track of the maximum length of the segment seen so far and return the left and right indices of this segment. + + +}