i love procrastinating

This commit is contained in:
Mars Ultor
2025-04-18 13:33:19 -05:00
parent bf89eae0a4
commit ed4bb2fc5d
4 changed files with 130 additions and 2 deletions
+1
View File
@@ -0,0 +1 @@
target/**
Generated
+7
View File
@@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "minimizing-coins"
version = "0.1.0"
+73 -1
View File
@@ -1,3 +1,75 @@
# minimizing-coins # minimizing-coins
Some demo of Rust for Comp Sci Club ## Problem
### Minimum Number of Coins
Consider a money system consisting of `n` coins. Each coin has a positive integer value. Your task is to produce a sum of money `x` using the available coins in such a way that the number of coins is minimized.
For example, if the coins are `{1, 5, 7}` and the desired sum is `11`, an optimal solution is `5 + 5 + 1`, which requires 3 coins.
---
### Input
The first input line has two integers `n` and `x`:
- `n` — the number of coins
- `x` — the desired sum of money
The second line has `n` distinct integers `c₁, c₂, ..., cₙ`: the value of each coin.
---
### Output
Print one integer: the **minimum** number of coins needed to produce the sum `x`.
If it is **not possible**, print `-1`.
---
### Constraints
- `1 ≤ n ≤ 100`
- `1 ≤ x ≤ 10⁶`
- `1 ≤ cᵢ ≤ 10⁶`
## Core logic
```rust
fn find_min(cache: &mut HashMap<u64, u64>, target_sum: u64, coins: &Vec<u64>) -> Option<u64> {
if let Some(&entry) = cache.get(&target_sum) {
return Some(entry);
}
let mut running_min: u64 = u64::MAX;
for &coin in coins {
if coin > target_sum {
continue;
}
let new_target = target_sum - coin;
if new_target == 0 {
cache.insert(target_sum, 1); // cache base case
return Some(1);
}
if let Some(path) = find_min(cache, new_target, coins) {
let num_coins = 1 + path;
if num_coins < running_min {
running_min = num_coins;
}
}
}
if running_min != u64::MAX {
cache.insert(target_sum, running_min);
Some(running_min)
} else {
cache.insert(target_sum, u64::MAX); // Mark as unreachable
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.
+49 -1
View File
@@ -1,3 +1,51 @@
use std::{collections::HashMap};
fn main() { fn main() {
println!("Hello, world!"); let mut line_buf: String = String::new();
std::io::stdin().read_line(&mut line_buf);
let first_line: Vec<u64> = line_buf.split_whitespace().into_iter().map(|item| item.parse::<u64>().unwrap()).collect();
line_buf=String::new();
std::io::stdin().read_line(&mut line_buf);
let second_line: Vec<u64> = line_buf.split_whitespace().into_iter().map(|item| item.parse::<u64>().unwrap()).collect();
//println!("Coins: {:#?}", second_line);
println!("{:#?}", find_min(&mut HashMap::new(), *first_line.get(1).unwrap(), &second_line));
} }
fn find_min(cache: &mut HashMap<u64, u64>, target_sum: u64, coins: &Vec<u64>) -> Option<u64> {
if let Some(&entry) = cache.get(&target_sum) {
return Some(entry);
}
let mut running_min: u64 = u64::MAX;
for &coin in coins {
if coin > target_sum {
continue;
}
let new_target = target_sum - coin;
if new_target == 0 {
cache.insert(target_sum, 1); // cache base case
return Some(1);
}
if let Some(path) = find_min(cache, new_target, coins) {
let num_coins = 1 + path;
if num_coins < running_min {
running_min = num_coins;
}
}
}
if running_min != u64::MAX {
cache.insert(target_sum, running_min);
Some(running_min)
} else {
cache.insert(target_sum, u64::MAX); // Mark as unreachable
None
}
}