-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
better solution is to build result by adding new dishes
- Loading branch information
1 parent
780a074
commit 68f36b1
Showing
1 changed file
with
21 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
impl Solution { | ||
pub fn max_satisfaction(mut dishes: Vec<i32>) -> i32 { | ||
satisfaction(dishes) | ||
} | ||
} | ||
|
||
fn satisfaction(mut dishes: Vec<i32>) -> i32 { | ||
dishes.sort(); | ||
let positive_sum: i32 = dishes.iter().filter(|&&n| n > 0).sum(); | ||
let negative: Vec<i32> = dishes.iter().filter(|&&n| n < 0).cloned().collect(); | ||
let mut i = 0; | ||
loop { | ||
let diff = negative[i..].iter().sum::<i32>().abs(); | ||
if diff > positive_sum { | ||
i += 1; | ||
} else { | ||
break; | ||
} | ||
} | ||
dishes[i..].iter().enumerate().map(|(i, &k)| (i as i32 + 1) * k).sum() | ||
} |