feat: add initial implementation of new 21 game solution
This commit is contained in:
parent
cd1b2b071d
commit
8bc5d237f3
7
837_new_21_game/Cargo.lock
generated
Normal file
7
837_new_21_game/Cargo.lock
generated
Normal file
@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "new_21_game"
|
||||
version = "0.1.0"
|
6
837_new_21_game/Cargo.toml
Normal file
6
837_new_21_game/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "new_21_game"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
35
837_new_21_game/src/main.rs
Normal file
35
837_new_21_game/src/main.rs
Normal file
@ -0,0 +1,35 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
|
||||
struct Solution;
|
||||
|
||||
impl Solution {
|
||||
pub fn new21_game(n: i32, k: i32, max_pts: i32) -> f64 {
|
||||
if k == 0 || n >= k + max_pts {
|
||||
return 1f64;
|
||||
}
|
||||
|
||||
let mut result = 0f64;
|
||||
let mut table = vec![0f64; (n + 1) as usize];
|
||||
table[0] = 1f64;
|
||||
let mut sum = 1f64;
|
||||
|
||||
for i in 1..(n + 1) {
|
||||
let prob = sum / max_pts as f64;
|
||||
table[i as usize] = prob;
|
||||
|
||||
if i < k {
|
||||
sum += prob;
|
||||
} else {
|
||||
result += prob;
|
||||
}
|
||||
|
||||
if i >= max_pts {
|
||||
sum -= table[(i - max_pts) as usize];
|
||||
}
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user