Skip to content

Commit

Permalink
solve leetcode 2600
Browse files Browse the repository at this point in the history
  • Loading branch information
pymongo committed Jul 5, 2023
1 parent cb6d361 commit 0748d0f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,7 @@ leetcode_solutions
|2455|[Average...](https://leetcode.cn/problems/average-value-of-even-numbers-that-are-divisible-by-three/)|[Rust](src/easy/leetcode_very_easy.rs)|
|2469|[Convert The...](https://leetcode.cn/problems/convert-the-temperature)|[Rust](src/easy/leetcode_very_easy.rs)|
|2490|[Circular Sentence](https://leetcode.cn/problems/circular-sentence/)|[Rust](src/easy/leetcode_very_easy.rs)|
|2600|[K Items...](https://leetcode.cn/problems/k-items-with-the-maximum-sum/)|[Rust](src/easy/leetcode_very_easy.rs)|

---

Expand Down
18 changes: 16 additions & 2 deletions src/easy/leetcode_very_easy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4160,7 +4160,10 @@ fn test_num_tile_possibilities() {

/// https://leetcode.cn/problems/average-value-of-even-numbers-that-are-divisible-by-three/
fn average_value(nums: Vec<i32>) -> i32 {
let nums = nums.into_iter().filter(|num| num % 6 == 0).collect::<Vec<_>>();
let nums = nums
.into_iter()
.filter(|num| num % 6 == 0)
.collect::<Vec<_>>();
if nums.is_empty() {
return 0;
}
Expand All @@ -4177,9 +4180,20 @@ fn is_circular_sentence(sentence: String) -> bool {
word_len=3, paris=01,12,20
*/
for i in 0..word_len {
if *words[i].last().unwrap() != words[(i+1)%word_len][0] {
if *words[i].last().unwrap() != words[(i + 1) % word_len][0] {
return false;
}
}
true
}

/// https://leetcode.cn/problems/k-items-with-the-maximum-sum/
fn k_items_with_maximum_sum(num_ones: i32, num_zeros: i32, num_neg_ones: i32, k: i32) -> i32 {
if k <= num_ones + num_zeros {
num_ones.min(k)
} else if k <= num_ones + num_zeros + num_neg_ones {
num_ones - (k - num_ones - num_zeros)
} else {
num_ones - num_neg_ones
}
}

0 comments on commit 0748d0f

Please sign in to comment.