Skip to content

Commit 1294633

Browse files
committed
Added maximum ones solution.
1 parent 128e14b commit 1294633

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ programming challenges completed in other languages.
178178
- [Reverse Vowels of a String](https://leetcode.com/problems/reverse-vowels-of-a-string)
179179
- [Minimum Recolors to Get K Consecutive Black Blocks](https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks)
180180
- [Minimum Operations to Make Array Distinct](https://leetcode.com/problems/minimum-number-of-operations-to-make-elements-in-array-distinct)
181+
- [Row with Maximum Ones](https://leetcode.com/problems/row-with-maximum-ones/)
181182

182183
#### Medium
183184

src/leetcode/maximum_ones.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
pub fn row_and_maximum_ones(matrix: Vec<Vec<i32>>) -> Vec<i32> {
2+
let mut result = vec![0, i32::MIN];
3+
(0..matrix.len()).for_each(|idx| {
4+
let one_count = matrix[idx].iter().filter(|&&i| i == 1).count() as i32;
5+
if one_count > result[1] {
6+
result[0] = idx as i32;
7+
result[1] = one_count;
8+
}
9+
});
10+
result
11+
}
12+
13+
#[cfg(test)]
14+
mod tests {
15+
use super::*;
16+
17+
#[test]
18+
fn finds_row_with_maximum_ones() {
19+
assert_eq!(
20+
vec![0, 1],
21+
row_and_maximum_ones(vec![vec![0, 1], vec![1, 0]])
22+
);
23+
24+
assert_eq!(
25+
vec![1, 2],
26+
row_and_maximum_ones(vec![vec![0, 0, 0], vec![0, 1, 1]])
27+
);
28+
}
29+
}

src/leetcode/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,4 @@ mod xor_subarray;
176176
mod zero_or_one;
177177
mod zigzag_conversion;
178178
mod min_operations;
179+
mod maximum_ones;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy