Skip to content

Commit ef3952e

Browse files
committed
feat: add more
1 parent 8d6237c commit ef3952e

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,46 @@
11
// https://leetcode.com/problems/check-if-all-1s-are-at-least-length-k-places-away
2-
//
2+
//
33
// Given an binary array `nums` and an integer `k`, return `true` _if all_ `1`_'s are at least_ `k` _places away from each other, otherwise return_ `false`.
4-
//
4+
//
55
// **Example 1:**
6-
//
6+
//
77
// ![](https://assets.leetcode.com/uploads/2020/04/15/sample_1_1791.png)
88
// ```
99
// **Input:** nums = [1,0,0,0,1,0,0,1], k = 2
1010
// **Output:** true
1111
// **Explanation:** Each of the 1s are at least 2 places away from each other.
1212
// ```
13-
//
13+
//
1414
// **Example 2:**
15-
//
15+
//
1616
// ![](https://assets.leetcode.com/uploads/2020/04/15/sample_2_1791.png)
1717
// ```
1818
// **Input:** nums = [1,0,0,1,0,1], k = 2
1919
// **Output:** false
2020
// **Explanation:** The second 1 and third 1 are only one apart from each other.
2121
// ```
22-
//
22+
//
2323
// **Constraints:**
24-
//
24+
//
2525
// * `1 <= nums.length <= 10<sup>5</sup>`
2626
// * `0 <= k <= nums.length`
2727
// * `nums[i]` is `0` or `1`
2828

2929
pub fn k_length_apart(nums: Vec<i32>, k: i32) -> bool {
30-
30+
let mut start_index = -k - 1;
31+
for i in 0..nums.len() {
32+
if nums[i] == 1 {
33+
if i as i32 - start_index - 1 < k {
34+
return false;
35+
}
36+
start_index = i as i32;
37+
}
3138
}
39+
return true;
40+
}
3241

3342
#[test]
3443
pub fn t1() {
44+
assert_eq!(k_length_apart(vec![1, 0, 0, 0, 1, 0, 0, 1], 2), true);
45+
assert_eq!(k_length_apart(vec![1, 0, 0, 1, 0, 1], 2), false);
3546
}

Array/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ mod _1413_minimum_value_to_get_positive_step_by_step_sum;
8181
mod _1426_counting_elements;
8282
mod _1427_perform_string_shifts;
8383
mod _1431_kids_with_the_greatest_number_of_candies;
84+
mod _1437_check_if_all_1s_are_at_least_length_k_places_away;
8485
mod _1441_build_an_array_with_stack_operations;
8586
mod _1450_number_of_students_doing_homework_at_a_given_time;
8687
mod _1460_make_two_arrays_equal_by_reversing_sub_arrays;

0 commit comments

Comments
 (0)