rust_test/src/main.rs

73 lines
1.6 KiB
Rust

use std::cmp::{max, Reverse};
use std::collections::BinaryHeap;
struct Solution;
mod arr;
#[derive(Debug)]
struct Node {
time: i32,
cnt: i32,
}
impl Node {
fn new(time: i32, cnt: i32) -> Node {
Node { time,cnt }
}
fn total(&self) -> i64 {
let cnt = self.cnt as i64;
self.time as i64 * cnt * (cnt + 1) / 2
}
}
impl Ord for Node {
// 默认的比较方式
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.total().cmp(&other.total())
}
}
impl PartialOrd for Node {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.total().cmp(&other.total()))
}
}
impl Eq for Node { }
impl PartialEq for Node {
fn eq(&self, other: &Self) -> bool {
self.total() == other.total()
}
fn ne(&self, other: &Self) -> bool {
self.total() != other.total()
}
}
impl Solution {
pub fn min_number_of_seconds(mountain_height: i32, worker_times: Vec<i32>) -> i64 {
let mut heap = BinaryHeap::<Reverse<Node>>::new();
for x in worker_times {
heap.push(Reverse(Node::new(x, 1)));
}
for _ in 0..mountain_height {
let mut node = heap.pop().unwrap();
node.0.cnt += 1;
heap.push(node);
}
let mut ans = 0_i64;
while !heap.is_empty() {
let mut node = heap.pop().unwrap().0;
node.cnt -= 1;
ans = max(ans, node.total())
}
ans
}
}
fn main() {
let result = Solution::min_number_of_seconds(
5,
vec![1, 7],
);
println!("{:?}", result);
}