20240128 Finished

This commit is contained in:
jackfiled 2024-01-28 10:24:47 +08:00
parent a4b9ee9d4a
commit a556d5059b
2 changed files with 49 additions and 1 deletions

View File

@ -30,3 +30,4 @@ mod p2865_beautiful_towers_i;
mod p2859_sum_of_values_at_indices_with_k_set_bits;
mod p2846_minimum_edge_weight_equilibrium_queries_in_a_tree;
mod p2861_maximum_number_of_alloys;
mod p365_water_and_jug_problem;

View File

@ -0,0 +1,47 @@
/**
* [365] Water and Jug Problem
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn can_measure_water(jug1_capacity: i32, jug2_capacity: i32, target_capacity: i32) -> bool {
if jug1_capacity + jug2_capacity < target_capacity {
return false;
}
if jug1_capacity == 0 || jug2_capacity == 0 {
return target_capacity == 0 || jug1_capacity + jug2_capacity == target_capacity;
}
target_capacity % Solution::gcd(jug1_capacity, jug2_capacity) == 0
}
fn gcd(a: i32, b: i32) -> i32 {
let (mut a, mut b) = (a, b);
while b > 0 {
let t = a % b;
a = b;
b = t;
}
a
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_365() {
assert!(Solution::can_measure_water(3, 5, 4));
assert!(!Solution::can_measure_water(2, 6, 5));
assert!(Solution::can_measure_water(1,2,3));
}
}