Skip to content

Commit

Permalink
LC 1404. Number of Steps to Reduce a Number in Binary Representation …
Browse files Browse the repository at this point in the history
…to One (Rust BM)
  • Loading branch information
raul-sauco committed May 29, 2024
1 parent b1243fd commit bf2982c
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,7 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
| [1383. Maximum Performance of a Team][lc1383] | 🔴 Hard | [![python](res/py.png)][lc1383py] |
| [1396. Design Underground System][lc1396] | 🟠 Medium | [![rust](res/rs.png)][lc1396rs] |
| [1402. Reducing Dishes][lc1402] | 🔴 Hard | [![python](res/py.png)][lc1402py] [![rust](res/rs.png)][lc1402rs] |
| [1404. Number of Steps to Reduce a Number in Binary Representation to One][lc1404] | 🟠 Medium | [![rust](res/rs.png)][lc1404rs] |
| [1406. Stone Game III][lc1406] | 🔴 Hard | [![rust](res/rs.png)][lc1406rs] |
| [1416. Restore The Array][lc1416] | 🔴 Hard | [![python](res/py.png)][lc1416py] [![rust](res/rs.png)][lc1416rs] |
| [1423. Maximum Points You Can Obtain from Cards][lc1423] | 🟠 Medium | [![python](res/py.png)][lc1423py] |
Expand Down Expand Up @@ -1814,6 +1815,8 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
[lc1402]: https://leetcode.com/problems/reducing-dishes/
[lc1402py]: leetcode/reducing-dishes.py
[lc1402rs]: leetcode/reducing-dishes.rs
[lc1404]: https://leetcode.com/problems/number-of-steps-to-reduce-a-number-in-binary-representation-to-one/
[lc1404rs]: leetcode/number-of-steps-to-reduce-a-number-in-binary-representation-to-one.rs
[lc1406]: https://leetcode.com/problems/stone-game-iii/
[lc1406rs]: leetcode/stone-game-iii.rs
[lc1416]: https://leetcode.com/problems/restore-the-array/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// 1404. Number of Steps to Reduce a Number in Binary Representation to One
// 🟠 Medium
//
// https://leetcode.com/problems/number-of-steps-to-reduce-a-number-in-binary-representation-to-one/
//
// Tags: String - Bit Manipulation

struct Solution;
impl Solution {
/// Iterate from the back simulating the addition operation, when we find the first one, the
/// carry will become 1. When carry + the current digit result in an odd value, we need to do 2
/// operations, otherwise we can just shift right, one operation.
///
/// Time complexity: O(n) - Where n is the number of characters in the input.
/// Space complexity: O(1) - Constant extra memory used, an array with 2 i32 values.
///
/// Runtime 0 ms Beats 100%
/// Memory 2.13 MB Beats 100%
pub fn num_steps(s: String) -> i32 {
s[1..]
.chars()
.map(|c| if c == '1' { 1 } else { 0 })
.rev()
.fold([0, 0], |[res, carry], d| {
// if (d ^ carry) & 1 == 1 {
if (d + carry) % 2 == 1 {
[res + 2, 1]
} else {
[res + 1, carry]
}
})
.into_iter()
.sum::<i32>()
}
}

// Tests.
fn main() {
let tests = [
("1", 0),
("10", 1),
("1101", 6),
("11111111111111111111111111", 27),
("111111111000001111111111110000011111", 47),
];
println!("\n\x1b[92m» Running {} tests...\x1b[0m", tests.len());
let mut success = 0;
for (i, t) in tests.iter().enumerate() {
let res = Solution::num_steps(t.0.to_string());
if res == t.1 {
success += 1;
println!("\x1b[92m✔\x1b[95m Test {} passed!\x1b[0m", i);
} else {
println!(
"\x1b[31mx\x1b[95m Test {} failed expected: {:?} but got {}!!\x1b[0m",
i, t.1, res
);
}
}
println!();
if success == tests.len() {
println!("\x1b[30;42m✔ All tests passed!\x1b[0m")
} else if success == 0 {
println!("\x1b[31mx \x1b[41;37mAll tests failed!\x1b[0m")
} else {
println!(
"\x1b[31mx\x1b[95m {} tests failed!\x1b[0m",
tests.len() - success
)
}
}

0 comments on commit bf2982c

Please sign in to comment.