Skip to content

Commit

Permalink
Update p2861
Browse files Browse the repository at this point in the history
  • Loading branch information
HuaHuaY committed Jan 27, 2024
1 parent 3b8a495 commit ff58ae6
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ Call test by running `cargo test p[xxx]_`.
- [2788. Split Strings by Separator](src/leetcode/p2788_split_strings_by_separator)
- [2859. Sum of Values at Indices With K Set Bits](src/leetcode/p2859_sum_of_values_at_indices_with_k_set_bits)

### Medium(147)
### Medium(148)

- [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 @@ -233,6 +233,7 @@ Call test by running `cargo test p[xxx]_`.
- [2707. Extra Characters in a String](src/leetcode/p2707_extra_characters_in_a_string)
- [2735. Collecting Chocolates](src/leetcode/p2735_collecting_chocolates)
- [2807. Insert Greatest Common Divisors in Linked List](src/leetcode/p2807_insert_greatest_common_divisors_in_linked_list)
- [2861. Maximum Number of Alloys](src/leetcode/p2861_maximum_number_of_alloys)
- [2865. Beautiful Towers I](src/leetcode/p2865_beautiful_towers_i)

### Hard(54)
Expand Down
1 change: 1 addition & 0 deletions src/leetcode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ pub mod p2807_insert_greatest_common_divisors_in_linked_list;
pub mod p2809_minimum_time_to_make_array_sum_at_most_x;
pub mod p2846_minimum_edge_weight_equilibrium_queries_in_a_tree;
pub mod p2859_sum_of_values_at_indices_with_k_set_bits;
pub mod p2861_maximum_number_of_alloys;
pub mod p2865_beautiful_towers_i;
pub mod p28_find_the_index_of_the_first_occurrence_in_a_string;
pub mod p29_divide_two_integers;
Expand Down
91 changes: 91 additions & 0 deletions src/leetcode/p2861_maximum_number_of_alloys/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
pub struct Solution;

impl Solution {
pub fn max_number_of_alloys(
_: i32,
_: i32,
budget: i32,
composition: Vec<Vec<i32>>,
stock: Vec<i32>,
cost: Vec<i32>,
) -> i32 {
fn valid(composition: &[i32], stock: &[i32], cost: &[i32], budget: i32, mid: i32) -> bool {
let mut total = 0;
for ((&c, &s), &p) in composition.iter().zip(stock).zip(cost) {
let diff = c as i64 * mid as i64 - s as i64;
if diff > 0 {
total += diff * p as i64;
}
}
total <= budget as i64
}

let mut result = 0;
for composition in composition {
let mut left = 0;
let mut right = 2 * 100_000_000;
let mut mid;
while left <= right {
mid = (right - left) / 2 + left;
if valid(&composition, &stock, &cost, budget, mid) {
left = mid + 1;
} else {
right = mid - 1;
}
}
result = result.max(right);
}
result
}
}

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

#[test]
fn test1() {
assert_eq!(
Solution::max_number_of_alloys(
3,
2,
15,
[[1, 1, 1], [1, 1, 10]].to_vec_vec(),
[0, 0, 0].to_vec(),
[1, 2, 3].to_vec()
),
2
);
}

#[test]
fn test2() {
assert_eq!(
Solution::max_number_of_alloys(
3,
2,
15,
[[1, 1, 1], [1, 1, 10]].to_vec_vec(),
[0, 0, 100].to_vec(),
[1, 2, 3].to_vec()
),
5
);
}

#[test]
fn test3() {
assert_eq!(
Solution::max_number_of_alloys(
2,
3,
10,
[[2, 1], [1, 2], [1, 1]].to_vec_vec(),
[1, 1].to_vec(),
[5, 5].to_vec()
),
2
);
}
}

0 comments on commit ff58ae6

Please sign in to comment.