not completed, still need to be fixes. Not fully working/tested

This commit is contained in:
Luca Lombardo
2023-10-25 16:06:06 +02:00
parent 1ea44355a8
commit 31ba3abfe4
6 changed files with 75 additions and 26 deletions
@@ -0,0 +1,8 @@
[package]
name = "check-if-all-the-integers-in-a-range-are-covered"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
@@ -0,0 +1,22 @@
fn is_covered(ranges: Vec<Vec<i32>>, left: i32, right: i32) -> bool {
let mut line = vec![0; 52]; //
for r in ranges {
line[r[0] as usize] += 1;
line[r[1] as usize + 1] -= 1;
}
let mut overlaps = 0;
for i in 1..=right {
overlaps += line[i as usize];
if i >= left && overlaps == 0 {
return false;
}
}
true
}
fn main() {
let ranges = vec![vec![1, 2], vec![3, 4], vec![5, 6]];
let left = 2;
let right = 7;
println!("{}", is_covered(ranges, left, right));
}
@@ -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]
@@ -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<usize> = 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<usize, usize> = 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<usize, usize> = 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)
}
```
@@ -0,0 +1,45 @@
// https://codeforces.com/contest/616/problem/D?locale=en
use std::collections::HashMap;
use std::io::{self, Read};
fn main() {
let mut buffer = String::new();
io::stdin().read_to_string(&mut buffer).unwrap();
let mut lines = buffer.lines();
let _ = lines.next();
let a: Vec<i32> = lines
.next()
.unwrap()
.split_whitespace()
.map(|x| x.parse().unwrap())
.collect();
let k: i32 = lines.next().unwrap().parse().unwrap();
let mut map = HashMap::new();
let mut max = 0;
let mut max_l = 0;
let mut max_r = 0;
let mut l = 0;
let mut r = 0;
while r < a.len() {
let count = map.entry(a[r]).or_insert(0);
*count += 1;
while map.len() > k as usize {
let count = map.get_mut(&a[l]).unwrap();
*count -= 1;
if *count == 0 {
map.remove(&a[l]);
}
l += 1;
}
if r - l + 1 > max {
max = r - l + 1;
max_l = l;
max_r = r;
}
r += 1;
}
println!("{} {}", max_l + 1, max_r + 1);
}