-
-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problem: No.3066
No.3066.Minimum Operations to Exceed Threshold Value II
- Loading branch information
Showing
8 changed files
with
94 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
solution/3000-3099/3066.Minimum Operations to Exceed Threshold Value II/Solution.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use std::collections::BinaryHeap; | ||
|
||
impl Solution { | ||
pub fn min_operations(nums: Vec<i32>, k: i32) -> i32 { | ||
let mut pq = BinaryHeap::new(); | ||
|
||
for &x in &nums { | ||
pq.push(-(x as i64)); | ||
} | ||
|
||
let mut ans = 0; | ||
|
||
while pq.len() > 1 && -pq.peek().unwrap() < k as i64 { | ||
let x = -pq.pop().unwrap(); | ||
let y = -pq.pop().unwrap(); | ||
pq.push(-(x * 2 + y)); | ||
ans += 1; | ||
} | ||
|
||
ans | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters