rs: add «2971. Find Polygon With the Largest Perimeter»
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
088cfb5560
commit
85d4d8d9ab
1 changed files with 20 additions and 0 deletions
20
rs/find-polygon-with-the-largest-perimeter.rs
Normal file
20
rs/find-polygon-with-the-largest-perimeter.rs
Normal file
|
@ -0,0 +1,20 @@
|
|||
impl Solution {
|
||||
pub fn largest_perimeter(mut nums: Vec<i32>) -> i64 {
|
||||
nums.sort();
|
||||
|
||||
let mut sum: i64 = 0;
|
||||
let mut max_perimeter: i64 = -1;
|
||||
|
||||
for x in &nums {
|
||||
let x = i64::from(*x);
|
||||
|
||||
if x < sum {
|
||||
max_perimeter = x + sum;
|
||||
}
|
||||
|
||||
sum += x;
|
||||
}
|
||||
|
||||
max_perimeter
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue