From 08c3712b1d7066b08fb2dd9f904d2214a7cd8f2e Mon Sep 17 00:00:00 2001 From: jackfiled Date: Sun, 3 Mar 2024 10:05:20 +0800 Subject: [PATCH] add: double week contest 125 --- src/double_week_125/mod.rs | 3 ++ src/double_week_125/problem1.rs | 15 +++++++ src/double_week_125/problem2.rs | 29 ++++++++++++++ src/double_week_125/problem3.rs | 71 +++++++++++++++++++++++++++++++++ src/lib.rs | 1 + 5 files changed, 119 insertions(+) create mode 100644 src/double_week_125/mod.rs create mode 100644 src/double_week_125/problem1.rs create mode 100644 src/double_week_125/problem2.rs create mode 100644 src/double_week_125/problem3.rs diff --git a/src/double_week_125/mod.rs b/src/double_week_125/mod.rs new file mode 100644 index 0000000..17371d2 --- /dev/null +++ b/src/double_week_125/mod.rs @@ -0,0 +1,3 @@ +mod problem1; +mod problem2; +mod problem3; \ No newline at end of file diff --git a/src/double_week_125/problem1.rs b/src/double_week_125/problem1.rs new file mode 100644 index 0000000..a861d38 --- /dev/null +++ b/src/double_week_125/problem1.rs @@ -0,0 +1,15 @@ +pub struct Solution; + +impl Solution { + pub fn min_operations(nums: Vec, k: i32) -> i32 { + let mut result = 0; + + for i in nums { + if i < k { + result += 1; + } + } + + result + } +} \ No newline at end of file diff --git a/src/double_week_125/problem2.rs b/src/double_week_125/problem2.rs new file mode 100644 index 0000000..7f9c60c --- /dev/null +++ b/src/double_week_125/problem2.rs @@ -0,0 +1,29 @@ +pub struct Solution; + +use std::collections::BinaryHeap; +use std::cmp::Reverse; + +impl Solution { + pub fn min_operations(nums: Vec, k: i32) -> i32 { + let mut heap = BinaryHeap::new(); + let k = k as i64; + + for i in nums { + let i = i as i64; + heap.push(Reverse(i)); + } + + let mut result = 0; + + while heap.peek().unwrap().0 < k { + let x = heap.pop().unwrap().0; + let y = heap.pop().unwrap().0; + + heap.push(Reverse(x + y + x.min(y))); + result += 1; + } + + + result + } +} \ No newline at end of file diff --git a/src/double_week_125/problem3.rs b/src/double_week_125/problem3.rs new file mode 100644 index 0000000..8c469e1 --- /dev/null +++ b/src/double_week_125/problem3.rs @@ -0,0 +1,71 @@ +pub struct Solution; + +use std::collections::HashMap; + +impl Solution { + pub fn count_pairs_of_connectable_servers(edges: Vec>, signal_speed: i32) -> Vec { + let n = edges.len(); + let mut graph = vec![vec![];n]; + let signal_speed = signal_speed as i64; + + for edge in &edges { + let x = edge[0] as usize; + let y = edge[1] as usize; + let weight = edge[2] as i64; + + graph[x].push((y, weight)); + graph[y].push((x, weight)); + } + + let mut connection = vec![(0,0);n]; + for next in &graph[0] { + + } + + let mut result = Vec::with_capacity(n); + + result + } + + fn dfs(graph: &Vec>, conection: &mut Vec<(i64, usize)>, now: usize, pre: usize, distance: i64, start: usize) { + for next in &graph[now] { + if next.0 == pre { + continue; + } + + conection[next.0] = (distance + next.1, start); + + Solution::dfs(graph, conection, next.0, now, distance + next.1, start); + } + } + + fn tree_dp(graph: &Vec>, conection: &mut Vec<(i64, usize)>, result: &mut Vec, + now: usize, pre: usize, signal_speed: i64) { + let mut count = HashMap::new(); + + for node in conection { + if node.0 % signal_speed == 0 { + let entry = count.entry(node.1).or_insert(0); + + *entry += 1; + } + } + + let count: Vec<&i32> = count.values().collect(); + let mut ans = 0; + + for i in 0..(count.len() - 1) { + for j in (i + 1)..count.len() { + ans += count[i] * count[j]; + } + } + + result[now] = ans; + + for next in &graph[now] { + if next.0 == pre { + continue; + } + } + } +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 144c1b0..eb9b5e5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,3 +2,4 @@ pub mod util; pub mod problem; pub mod week_385; +pub mod double_week_125;