Skip to content

Commit 9930709

Browse files
committed
LC 3133. Minimum Array End (Rust)
1 parent 2957dca commit 9930709

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,7 @@ to the solution in this repository.
783783
| [3068. Find the Maximum Sum of Node Values][lc3068] | 🔴 Hard | [![rust](res/rs.png)][lc3068rs] |
784784
| [3075. Maximize Happiness of Selected Children][lc3075] | 🟠 Medium | [![rust](res/rs.png)][lc3075rs] |
785785
| [3110. Score of a String][lc3110] | 🟢 Easy | [![rust](res/rs.png)][lc3110rs] |
786+
| [3133. Minimum Array End][lc3133] | 🟠 Medium | [![rust](res/rs.png)][lc3133rs] |
786787
| [3217. Delete Nodes From Linked List Present in Array][lc3217] | 🟠 Medium | [![python](res/py.png)][lc3217py] |
787788

788789
[🔝 Back to Top 🔝](#coding-challenges)
@@ -2503,6 +2504,8 @@ to the solution in this repository.
25032504
[lc3075rs]: leetcode/maximize-happiness-of-selected-children.rs
25042505
[lc3110]: https://leetcode.com/problems/score-of-a-string/
25052506
[lc3110rs]: leetcode/score-of-a-string.rs
2507+
[lc3133]: https://leetcode.com/problems/minimum-array-end/
2508+
[lc3133rs]: leetcode/minimum-array-end.rs
25062509
[lc3217]: https://leetcode.com/problems/delete-nodes-from-linked-list-present-in-array/
25072510
[lc3217py]: leetcode/delete-nodes-from-linked-list-present-in-array.py
25082511

leetcode/minimum-array-end.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// 3133. Minimum Array End
2+
// 🟠 Medium
3+
//
4+
// https://leetcode.com/problems/minimum-array-end/
5+
//
6+
// Tags: Bit Manipulation
7+
8+
struct Solution;
9+
impl Solution {
10+
/// For n steps choose the smallest next number that me can use. Barely passes.
11+
///
12+
/// Time complexity: O(n)
13+
/// Space complexity: O(1)
14+
///
15+
/// Runtime 1735 ms Beats 100%
16+
/// Memory 2.16 MB Beats 100%
17+
#[allow(dead_code)]
18+
pub fn min_end_on(n: i32, x: i32) -> i64 {
19+
let x = x as i64;
20+
(0..n - 1).fold(x, |acc, _| (acc + 1) | x)
21+
}
22+
23+
/// For n steps choose the smallest next number that me can use. Barely passes.
24+
///
25+
/// Time complexity: O(n)
26+
/// Space complexity: O(1)
27+
///
28+
/// Runtime 1735 ms Beats 100%
29+
/// Memory 2.16 MB Beats 100%
30+
pub fn min_end(n: i32, x: i32) -> i64 {
31+
let mut x = x as i64;
32+
let mut n = n as i64 - 1;
33+
let mut b = 1i64;
34+
for _ in 0..64i64 {
35+
if b & x == 0 {
36+
x |= (n & 1) * b;
37+
n >>= 1;
38+
}
39+
b <<= 1;
40+
}
41+
x
42+
}
43+
}
44+
45+
// Tests.
46+
fn main() {
47+
let tests = [(3, 4, 6), (2, 7, 15)];
48+
println!("\n\x1b[92m» Running {} tests...\x1b[0m", tests.len());
49+
let mut success = 0;
50+
for (i, t) in tests.iter().enumerate() {
51+
let res = Solution::min_end(t.0, t.1);
52+
if res == t.2 {
53+
success += 1;
54+
println!("\x1b[92m✔\x1b[95m Test {} passed!\x1b[0m", i);
55+
} else {
56+
println!(
57+
"\x1b[31mx\x1b[95m Test {} failed expected: {:?} but got {}!!\x1b[0m",
58+
i, t.2, res
59+
);
60+
}
61+
}
62+
println!();
63+
if success == tests.len() {
64+
println!("\x1b[30;42m✔ All tests passed!\x1b[0m")
65+
} else if success == 0 {
66+
println!("\x1b[31mx \x1b[41;37mAll tests failed!\x1b[0m")
67+
} else {
68+
println!(
69+
"\x1b[31mx\x1b[95m {} tests failed!\x1b[0m",
70+
tests.len() - success
71+
)
72+
}
73+
}

0 commit comments

Comments
 (0)