more readable

This commit is contained in:
Mars Ultor
2025-04-18 13:36:49 -05:00
parent ed4bb2fc5d
commit 5ea26cee9b
2 changed files with 4 additions and 5 deletions
+2 -3
View File
@@ -64,12 +64,11 @@ fn find_min(cache: &mut HashMap<u64, u64>, target_sum: u64, coins: &Vec<u64>) ->
if running_min != u64::MAX { if running_min != u64::MAX {
cache.insert(target_sum, running_min); cache.insert(target_sum, running_min);
Some(running_min) return Some(running_min);
} else { } else {
cache.insert(target_sum, u64::MAX); // Mark as unreachable cache.insert(target_sum, u64::MAX); // Mark as unreachable
None return None;
} }
}
``` ```
Essentially, the purpose of the above code is to traverse a tree of possible coin decisions. As long as the coin denomination is smaller than the target, it's possible to recursively iterate through all combinations of coins while both caching and checking for a base case. This behavior is called memorization. The base case for any branch of the tree is when the target sum is equivalent to the coin of interest. Essentially, the purpose of the above code is to traverse a tree of possible coin decisions. As long as the coin denomination is smaller than the target, it's possible to recursively iterate through all combinations of coins while both caching and checking for a base case. This behavior is called memorization. The base case for any branch of the tree is when the target sum is equivalent to the coin of interest.
+2 -2
View File
@@ -42,10 +42,10 @@ fn find_min(cache: &mut HashMap<u64, u64>, target_sum: u64, coins: &Vec<u64>) ->
if running_min != u64::MAX { if running_min != u64::MAX {
cache.insert(target_sum, running_min); cache.insert(target_sum, running_min);
Some(running_min) return Some(running_min);
} else { } else {
cache.insert(target_sum, u64::MAX); // Mark as unreachable cache.insert(target_sum, u64::MAX); // Mark as unreachable
None return None;
} }
} }