Refactor code for trapping rain water problem
parent
3108b2e612
commit
92681d39e9
@ -1,7 +0,0 @@
|
|||||||
{
|
|
||||||
// Use IntelliSense to learn about possible attributes.
|
|
||||||
// Hover to view descriptions of existing attributes.
|
|
||||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
|
||||||
"version": "0.2.0",
|
|
||||||
"configurations": []
|
|
||||||
}
|
|
@ -1,36 +1,49 @@
|
|||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
|
|
||||||
|
// Function to calculate the total amount of water that can be trapped
|
||||||
fn trap(height: Vec<i32>) -> i32 {
|
fn trap(height: Vec<i32>) -> i32 {
|
||||||
// base case
|
// If the height vector is empty, return 0
|
||||||
if height.len() == 0 {
|
if height.len() == 0 {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
let (mut l, mut r) = (0, height.len() - 1); // left and right pointer
|
// Initialize left and right pointers
|
||||||
let (mut l_max, mut r_max) = (height[l], height[r]); // left and right max height of current pointer
|
let (mut l, mut r) = (0, height.len() - 1);
|
||||||
|
// Initialize the maximum height from the left and right
|
||||||
|
let (mut l_max, mut r_max) = (height[l], height[r]);
|
||||||
|
|
||||||
|
// Initialize the result
|
||||||
let mut res = 0;
|
let mut res = 0;
|
||||||
|
|
||||||
while l < r { // loop until left and right pointer meet
|
// Loop until the left pointer is less than the right pointer
|
||||||
match l_max.cmp(&r_max) { // compare left and right max height
|
while l < r {
|
||||||
Ordering::Less => { // if left max height is less than right max height means we can trap water in left side
|
// If the maximum height from the left is less than the maximum height from the right
|
||||||
l += 1; // move left pointer
|
if l_max < r_max {
|
||||||
l_max = std::cmp::max(l_max, height[l]); // update left max height
|
// Move the left pointer to the right
|
||||||
res += l_max - height[l]; // add water
|
l += 1;
|
||||||
},
|
// Update the maximum height from the left
|
||||||
_ => { // if right max height is less than left max height means we can trap water in right side
|
l_max = std::cmp::max(l_max, height[l]);
|
||||||
r -= 1; // move right pointer
|
// Add the difference between the maximum height from the left and the current height to the result
|
||||||
r_max = std::cmp::max(r_max, height[r]); // update right max height
|
res += l_max - height[l];
|
||||||
res += r_max - height[r]; // add water
|
} else {
|
||||||
}
|
// Move the right pointer to the left
|
||||||
|
r -= 1;
|
||||||
|
// Update the maximum height from the right
|
||||||
|
r_max = std::cmp::max(r_max, height[r]);
|
||||||
|
// Add the difference between the maximum height from the right and the current height to the result
|
||||||
|
res += r_max - height[r];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Return the result
|
||||||
res
|
res
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
// Initialize the height vector
|
||||||
let height = vec![0,1,0,2,1,0,1,3,2,1,2,1];
|
let height = vec![0,1,0,2,1,0,1,3,2,1,2,1];
|
||||||
|
// Call the trap function and store the result
|
||||||
let res = trap(height);
|
let res = trap(height);
|
||||||
|
// Print the result
|
||||||
println!("res = {}", res);
|
println!("res = {}", res);
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,9 @@
|
|||||||
|
[package]
|
||||||
|
name = "frogs-and-mosquitos"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
bst-rs = "0.1.0"
|
@ -0,0 +1,13 @@
|
|||||||
|
The algorithm is designed to solve a problem involving frogs and mosquitoes. The frogs are located at certain positions and have a certain tongue length. Mosquitoes land at certain positions. If a mosquito lands within the reach of a frog's tongue, the frog will eat the mosquito and its tongue length will increase by the size of the mosquito. If a mosquito lands outside the reach of any frog, it will not be eaten.
|
||||||
|
|
||||||
|
The algorithm uses a data structure called a segment tree to efficiently manage the positions and tongue lengths of the frogs. A segment tree is a binary tree used for storing information about segments or intervals, and is especially useful for range query problems.
|
||||||
|
|
||||||
|
The algorithm starts by initializing a segment tree with the positions and tongue lengths of the frogs. Each node in the segment tree represents a range of positions, and stores the maximum tongue length of any frog within that range.
|
||||||
|
|
||||||
|
When a mosquito lands, the algorithm searches the segment tree to find a frog that can reach the mosquito. This is done by checking if the mosquito's position is within the range of a node and if the maximum tongue length in that node is sufficient to reach the mosquito. If such a node is found, the frog's tongue length is updated and the node's maximum value is also updated.
|
||||||
|
|
||||||
|
If no such node is found, the mosquito is added to a set of uneaten mosquitoes.
|
||||||
|
|
||||||
|
The algorithm also includes a function to insert a new segment into the segment tree. This function is used to update the tree when a frog's tongue length increases. The function ensures that the tree remains balanced and that the maximum values in each node are correct.
|
||||||
|
|
||||||
|
Finally, the algorithm includes a function to check if there is a position within a given range that contains exactly a certain number of segments. This function is not used in the main part of the algorithm, but could be useful for additional queries about the positions and tongue lengths of the frogs.
|
Loading…
Reference in New Issue