Skip to content

Commit

Permalink
Update p2187
Browse files Browse the repository at this point in the history
  • Loading branch information
HuaHuaY committed Oct 6, 2024
1 parent bc9f184 commit ee5f6c0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ Call test by running `cargo test p[xxx]_`.
- [2864. Maximum Odd Binary Number](src/leetcode/p2864_maximum_odd_binary_number)
- [2917. Find the K-or of an Array](src/leetcode/p2917_find_the_k_or_of_an_array)

### Medium(190)
### Medium(191)

- [2. Add Two Numbers](src/leetcode/p2_add_two_numbers)
- [3. Longest Substring Without Repeating Characters](src/leetcode/p3_longest_substring_without_repeating_characters)
Expand Down Expand Up @@ -270,6 +270,7 @@ Call test by running `cargo test p[xxx]_`.
- [1976. Number of Ways to Arrive at Destination](src/leetcode/p1976_number_of_ways_to_arrive_at_destination)
- [2171. Removing Minimum Number of Magic Beans](src/leetcode/p2171_removing_minimum_number_of_magic_beans)
- [2182. Construct String With Repeat Limit](src/leetcode/p2182_construct_string_with_repeat_limit)
- [2187. Minimum Time to Complete Trips](src/leetcode/p2187_minimum_time_to_complete_trips)
- [2192. All Ancestors of a Node in a Directed Acyclic Graph](src/leetcode/p2192_all_ancestors_of_a_node_in_a_directed_acyclic_graph)
- [2368. Reachable Nodes With Restrictions](src/leetcode/p2368_reachable_nodes_with_restrictions)
- [2369. Check if There is a Valid Partition For The Array](src/leetcode/p2369_check_if_there_is_a_valid_partition_for_the_array)
Expand Down
1 change: 1 addition & 0 deletions src/leetcode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ pub mod p20_valid_parentheses;
pub mod p2129_capitalize_the_title;
pub mod p2171_removing_minimum_number_of_magic_beans;
pub mod p2182_construct_string_with_repeat_limit;
pub mod p2187_minimum_time_to_complete_trips;
pub mod p2192_all_ancestors_of_a_node_in_a_directed_acyclic_graph;
pub mod p21_merge_two_sorted_lists;
pub mod p221_maximal_square;
Expand Down
35 changes: 35 additions & 0 deletions src/leetcode/p2187_minimum_time_to_complete_trips/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
pub struct Solution;

impl Solution {
pub fn minimum_time(time: Vec<i32>, total_trips: i32) -> i64 {
let check =
|min: i64| time.iter().fold(0, |acc, &t| acc + (min / t as i64)) >= total_trips as i64;

let mut left = 1;
let mut right = *time.iter().min().unwrap() as i64 * total_trips as i64;
while left <= right {
let min = (right - left) / 2 + left;
if check(min) {
right = min - 1;
} else {
left = min + 1;
}
}
left
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test1() {
assert_eq!(Solution::minimum_time([1, 2, 3].to_vec(), 5), 3);
}

#[test]
fn test2() {
assert_eq!(Solution::minimum_time([2].to_vec(), 1), 2);
}
}

0 comments on commit ee5f6c0

Please sign in to comment.