20240721 Finished

This commit is contained in:
jackfiled 2024-07-21 11:21:16 +08:00
parent 26ffc40123
commit ec9cd1d603
2 changed files with 53 additions and 1 deletions

View File

@ -179,4 +179,5 @@ mod p74_search_a_2d_matrix;
mod p33_search_in_rotated_sorted_array;
mod p34_find_first_and_last_position_of_element_in_sorted_array;
mod p153_find_minimum_in_rotated_sorted_array;
mod p215_kth_largest_element_in_an_array;
mod p215_kth_largest_element_in_an_array;
mod p502_ipo;

51
src/problem/p502_ipo.rs Normal file
View File

@ -0,0 +1,51 @@
/**
* [502] IPO
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn find_maximized_capital(k: i32, w: i32, profits: Vec<i32>, capital: Vec<i32>) -> i32 {
use std::collections::BinaryHeap;
let mut array = vec![];
for (&p, &c) in profits.iter().zip(capital.iter()) {
array.push((p, c));
}
array.sort_by(|a, b| a.1.cmp(&b.1));
let mut result = w;
let mut current = 0;
let mut heap = BinaryHeap::new();
let n = array.len();
for _ in 0..k {
while current < n && array[current].1 <= result {
heap.push(array[current].0);
current += 1;
}
if let Some(&head) = heap.peek() {
result += head;
heap.pop();
} else {
break;
}
}
result
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_502() {
}
}