Skip to content

Commit acca451

Browse files
committed
LC 2938. Separate Black and White Balls (Rust)
1 parent b91a129 commit acca451

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,7 @@ to the solution in this repository.
758758
| [2849. Determine if a Cell Is Reachable at a Given Time][lc2849] | 🟠 Medium | [![rust](res/rs.png)][lc2849rs] |
759759
| [2864. Maximum Odd Binary Number][lc2864] | 🟢 Easy | [![rust](res/rs.png)][lc2864rs] |
760760
| [2870. Minimum Number of Operations to Make Array Empty][lc2870] | 🟠 Medium | [![rust](res/rs.png)][lc2870rs] |
761+
| [2938. Separate Black and White Balls][lc2938] | 🟠 Medium | [![rust](res/rs.png)][lc2938rs] |
761762
| [2958. Length of Longest Subarray With at Most K Frequency][lc2958] | 🟠 Medium | [![rust](res/rs.png)][lc2958rs] |
762763
| [2962. Count Subarrays Where Max Element Appears at Least K Times][lc2962] | 🟠 Medium | [![rust](res/rs.png)][lc2962rs] |
763764
| [2966. Divide Array Into Arrays With Max Difference][lc2966] | 🟠 Medium | [![rust](res/rs.png)][lc2966rs] |
@@ -2438,6 +2439,8 @@ to the solution in this repository.
24382439
[lc2864rs]: leetcode/maximum-odd-binary-number.rs
24392440
[lc2870]: https://leetcode.com/problems/minimum-number-of-operations-to-make-array-empty/
24402441
[lc2870rs]: leetcode/minimum-number-of-operations-to-make-array-empty.rs
2442+
[lc2938]: https://leetcode.com/problems/separate-black-and-white-balls/
2443+
[lc2938rs]: leetcode/separate-black-and-white-balls.rs
24412444
[lc2958]: https://leetcode.com/problems/length-of-longest-subarray-with-at-most-k-frequency/
24422445
[lc2958rs]: leetcode/length-of-longest-subarray-with-at-most-k-frequency.rs
24432446
[lc2962]: https://leetcode.com/problems/count-subarrays-where-max-element-appears-at-least-k-times/
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// 2938. Separate Black and White Balls
2+
// 🟠 Medium
3+
//
4+
// https://leetcode.com/problems/separate-black-and-white-balls/
5+
//
6+
// Tags: Two Pointers - String - Greedy
7+
8+
struct Solution;
9+
impl Solution {
10+
/// Iterate the string from the left keeping track of where the last white ball in placed, for
11+
/// each white ball found, move it to the furthest left spot possible and increment the index
12+
/// of the next white ball, add the number of moves done to the result. Once you have placed
13+
/// all the white balls on the left, all the black ones will be on the right.
14+
///
15+
/// Time complexity: O(n) - O(1) work for each element in the input.
16+
/// Space complexity: O(1)
17+
///
18+
/// Runtime 0 ms Beats 100%
19+
/// Memory 2.43 MB Beats 40%
20+
#[allow(dead_code)]
21+
pub fn minimum_steps_loop(s: String) -> i64 {
22+
let (mut res, mut next_white_idx) = (0, 0);
23+
for (i, b) in s.bytes().enumerate() {
24+
if b == b'0' {
25+
res += (i - next_white_idx) as i64;
26+
next_white_idx += 1;
27+
}
28+
}
29+
res
30+
}
31+
32+
/// Same logic but using an iterator.
33+
///
34+
/// Time complexity: O(n) - O(1) work for each element in the input.
35+
/// Space complexity: O(1)
36+
///
37+
/// Runtime 0 ms Beats 100%
38+
/// Memory 2.34 MB Beats 100%
39+
pub fn minimum_steps(s: String) -> i64 {
40+
s.bytes()
41+
.enumerate()
42+
.fold((0, 0), |(res, idx), (i, b)| {
43+
if b == b'0' {
44+
(res + (i - idx) as i64, idx + 1)
45+
} else {
46+
(res, idx)
47+
}
48+
})
49+
.0
50+
}
51+
}
52+
53+
// Tests.
54+
fn main() {
55+
let tests = [("101", 1), ("100", 2), ("0111", 0), ("0011000010", 11)];
56+
println!("\n\x1b[92m» Running {} tests...\x1b[0m", tests.len());
57+
let mut success = 0;
58+
for (i, t) in tests.iter().enumerate() {
59+
let res = Solution::minimum_steps(t.0.to_string());
60+
if res == t.1 {
61+
success += 1;
62+
println!("\x1b[92m✔\x1b[95m Test {} passed!\x1b[0m", i);
63+
} else {
64+
println!(
65+
"\x1b[31mx\x1b[95m Test {} failed expected: {:?} but got {}!!\x1b[0m",
66+
i, t.1, res
67+
);
68+
}
69+
}
70+
println!();
71+
if success == tests.len() {
72+
println!("\x1b[30;42m✔ All tests passed!\x1b[0m")
73+
} else if success == 0 {
74+
println!("\x1b[31mx \x1b[41;37mAll tests failed!\x1b[0m")
75+
} else {
76+
println!(
77+
"\x1b[31mx\x1b[95m {} tests failed!\x1b[0m",
78+
tests.len() - success
79+
)
80+
}
81+
}

0 commit comments

Comments
 (0)