mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-09 15:59:06 +01:00
rs: add «948. Bag of Tokens»
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
a2bebb36df
commit
4061ff562b
1 changed files with 56 additions and 0 deletions
56
rs/bag-of-tokens.rs
Normal file
56
rs/bag-of-tokens.rs
Normal file
|
@ -0,0 +1,56 @@
|
|||
struct Solution {}
|
||||
impl Solution {
|
||||
pub fn bag_of_tokens_score(mut tokens: Vec<i32>, mut power: i32) -> i32 {
|
||||
if tokens.is_empty() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
tokens.sort_unstable();
|
||||
|
||||
let mut i = 0;
|
||||
let mut j = tokens.len() - 1;
|
||||
|
||||
let mut score = 0;
|
||||
while i <= j {
|
||||
if power >= tokens[i] {
|
||||
// can trade score for power
|
||||
power -= tokens[i];
|
||||
score += 1;
|
||||
i += 1;
|
||||
} else if i < j && score > 0 {
|
||||
// can trade power for score
|
||||
score -= 1;
|
||||
power += tokens[j];
|
||||
j -= 1;
|
||||
} else {
|
||||
// no further action is possible
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
score
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn example_1() {
|
||||
assert_eq!(Solution::bag_of_tokens_score(vec![100], 50), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn example_2() {
|
||||
assert_eq!(Solution::bag_of_tokens_score(vec![200, 100], 150), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn example_3() {
|
||||
assert_eq!(
|
||||
Solution::bag_of_tokens_score(vec![100, 200, 300, 400], 200),
|
||||
2
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue