20240816 Finished
This commit is contained in:
parent
2da0c3af3b
commit
2d191e4d62
|
@ -205,4 +205,5 @@ mod p97_interleaving_string;
|
|||
mod p72_edit_distance;
|
||||
mod p123_best_time_to_buy_and_sell_stock_iii;
|
||||
mod p188_best_time_to_buy_and_sell_stock_iv;
|
||||
mod p221_maximal_square;
|
||||
mod p221_maximal_square;
|
||||
mod p3117_minimum_sum_of_values_by_dividing_array;
|
65
src/problem/p3117_minimum_sum_of_values_by_dividing_array.rs
Normal file
65
src/problem/p3117_minimum_sum_of_values_by_dividing_array.rs
Normal file
|
@ -0,0 +1,65 @@
|
|||
/**
|
||||
* [3117] Minimum Sum of Values by Dividing Array
|
||||
*/
|
||||
pub struct Solution {}
|
||||
|
||||
|
||||
// submission codes start here
|
||||
use std::collections::HashMap;
|
||||
|
||||
const INFINITE: i32 = (1 << 20) - 1;
|
||||
|
||||
impl Solution {
|
||||
pub fn minimum_value_sum(nums: Vec<i32>, and_values: Vec<i32>) -> i32 {
|
||||
let (n, m) = (nums.len(), and_values.len());
|
||||
let mut memory = vec![HashMap::new(); m * n];
|
||||
|
||||
let result = Self::dfs(&mut memory, 0, 0, INFINITE, &nums, &and_values);
|
||||
if result == INFINITE {
|
||||
-1
|
||||
} else {
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
fn dfs(memory: &mut Vec<HashMap<i32, i32>>, i: usize, j: usize, current: i32,
|
||||
nums: &Vec<i32>, and_values: &Vec<i32>) -> i32 {
|
||||
let (n, m) = (nums.len(), and_values.len());
|
||||
let key = m * i + j;
|
||||
|
||||
if i == n && j == m {
|
||||
return 0;
|
||||
}
|
||||
if i == n || j == m {
|
||||
return INFINITE;
|
||||
}
|
||||
if let Some(r) = memory[key].get(¤t) {
|
||||
return *r;
|
||||
}
|
||||
|
||||
let current = current & nums[i];
|
||||
if current & and_values[j] < and_values[j] {
|
||||
return INFINITE;
|
||||
}
|
||||
|
||||
let mut result = Self::dfs(memory, i + 1, j, current, nums, and_values);
|
||||
if current == and_values[j] {
|
||||
result = result.min(Self::dfs(memory, i + 1, j + 1, INFINITE, nums, and_values) + nums[i]);
|
||||
}
|
||||
|
||||
memory[key].insert(current, result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
// submission codes end
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_3117() {
|
||||
assert_eq!(12, Solution::minimum_value_sum(vec![1, 4, 3, 3, 2], vec![0, 3, 3, 2]));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user