rs: add «2441. Largest Positive Integer That Exists With Its Negative»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
8c3d5463ea
commit
2808f7d5d8
1 changed files with 19 additions and 0 deletions
19
rs/largest-positive-integer-that-exists-with-its-negative.rs
Normal file
19
rs/largest-positive-integer-that-exists-with-its-negative.rs
Normal file
|
@ -0,0 +1,19 @@
|
|||
use std::cmp;
|
||||
use std::collections::HashSet;
|
||||
|
||||
impl Solution {
|
||||
pub fn find_max_k(nums: Vec<i32>) -> i32 {
|
||||
let mut seen = HashSet::<i32>::new();
|
||||
|
||||
let mut max_k = -1;
|
||||
for x in &nums {
|
||||
if seen.contains(&-x) {
|
||||
max_k = cmp::max(max_k, x.abs());
|
||||
}
|
||||
|
||||
seen.insert(*x);
|
||||
}
|
||||
|
||||
max_k
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue