Skip to content

Commit 8db6d12

Browse files
committed
LC 3254. Find the Power of K-Size Subarrays I (Rust)
1 parent 1550bf7 commit 8db6d12

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,7 @@ to the solution in this repository.
790790
| [3110. Score of a String][lc3110] | 🟢 Easy | [![rust](res/rs.png)][lc3110rs] |
791791
| [3133. Minimum Array End][lc3133] | 🟠 Medium | [![rust](res/rs.png)][lc3133rs] |
792792
| [3217. Delete Nodes From Linked List Present in Array][lc3217] | 🟠 Medium | [![python](res/py.png)][lc3217py] |
793+
| [3254. Find the Power of K-Size Subarrays I][lc3254] | 🟠 Medium | [![rust](res/rs.png)][lc3254rs] |
793794

794795
[🔝 Back to Top 🔝](#coding-challenges)
795796

@@ -2523,6 +2524,8 @@ to the solution in this repository.
25232524
[lc3133rs]: leetcode/minimum-array-end.rs
25242525
[lc3217]: https://leetcode.com/problems/delete-nodes-from-linked-list-present-in-array/
25252526
[lc3217py]: leetcode/delete-nodes-from-linked-list-present-in-array.py
2527+
[lc3254]: https://leetcode.com/problems/find-the-power-of-k-size-subarrays-i/
2528+
[lc3254rs]: leetcode/find-the-power-of-k-size-subarrays-i.rs
25262529

25272530
## CodeWars problems
25282531

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// 3254. Find the Power of K-Size Subarrays I
2+
// 🟠 Medium
3+
//
4+
// https://leetcode.com/problems/find-the-power-of-k-size-subarrays-i/
5+
//
6+
// Tags: Array - Sliding Window
7+
8+
struct Solution;
9+
impl Solution {
10+
/// Iterate over the input vector, keep track of the count of increasing values up to the
11+
/// current point, if we have k or more, the k sized subarray is good, we can return its
12+
/// greatest value, the last one as the result for this subarray, otherwise -1.
13+
///
14+
/// Time complexity: O(n) - We iterate over the input and do constant time workfor each
15+
/// element that we visit.
16+
/// Space complexity: O(1) - We store the count of good elements.
17+
///
18+
/// Runtime 0 ms Beats 100%
19+
/// Memory 2.17 MB Beats 87%
20+
pub fn results_array(nums: Vec<i32>, k: i32) -> Vec<i32> {
21+
if k == 1 {
22+
return nums;
23+
}
24+
let n = nums.len();
25+
let k = k as usize;
26+
let mut res = vec![-1; n - k + 1];
27+
let mut good = 1;
28+
for i in 0..n - 1 {
29+
good = if nums[i] + 1 == nums[i + 1] {
30+
good + 1
31+
} else {
32+
1
33+
};
34+
if good >= k {
35+
res[2 + i - k] = nums[i + 1];
36+
}
37+
}
38+
res
39+
}
40+
}
41+
42+
// Tests.
43+
fn main() {
44+
let tests = [
45+
(vec![1, 2, 3, 4, 3, 2, 5], 3, vec![3, 4, -1, -1, -1]),
46+
(vec![2, 2, 2, 2, 2], 4, vec![-1, -1]),
47+
(vec![3, 2, 3, 2, 3, 2], 2, vec![-1, 3, -1, 3, -1]),
48+
];
49+
println!("\n\x1b[92m» Running {} tests...\x1b[0m", tests.len());
50+
let mut success = 0;
51+
for (i, t) in tests.iter().enumerate() {
52+
let res = Solution::results_array(t.0.clone(), t.1);
53+
if res == t.2 {
54+
success += 1;
55+
println!("\x1b[92m✔\x1b[95m Test {} passed!\x1b[0m", i);
56+
} else {
57+
println!(
58+
"\x1b[31mx\x1b[95m Test {} failed expected: {:?} but got {:?}!!\x1b[0m",
59+
i, t.2, res
60+
);
61+
}
62+
}
63+
println!();
64+
if success == tests.len() {
65+
println!("\x1b[30;42m✔ All tests passed!\x1b[0m")
66+
} else if success == 0 {
67+
println!("\x1b[31mx \x1b[41;37mAll tests failed!\x1b[0m")
68+
} else {
69+
println!(
70+
"\x1b[31mx\x1b[95m {} tests failed!\x1b[0m",
71+
tests.len() - success
72+
)
73+
}
74+
}

0 commit comments

Comments
 (0)