Skip to content

Commit 6d4f4ea

Browse files
chore: add LeetCode daily solution
1 parent f4eefcc commit 6d4f4ea

File tree

5 files changed

+73
-0
lines changed

5 files changed

+73
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Check If All 1's Are at Least Length K Places Away (Easy)
2+
3+
**Problem ID:** 1437
4+
**Date:** 2025-11-17
5+
**Link:** https://leetcode.com/problems/check-if-all-1s-are-at-least-length-k-places-away/
6+
7+
## Approach
8+
9+
To solve the problem of checking if all `1`s in a binary array are at least `k` places away from each other, we can adopt a straightforward linear scan approach. Here’s a concise breakdown of the solution:
10+
11+
### Main Idea:
12+
The core idea is to iterate through the binary array and keep track of the last position where a `1` was found. For each `1` encountered, we check the distance from the last recorded position of `1`. If this distance is less than `k`, we immediately return `false`. If we finish scanning the array without finding any violations, we return `true`.
13+
14+
### Steps:
15+
1. **Initialize a Variable**: Start with a variable to store the index of the last `1` found. Initialize it to a value that indicates no `1` has been found yet (e.g., `-1`).
16+
17+
2. **Iterate Through the Array**: Loop through each element of the array using an index:
18+
- If the current element is `1`, check the distance from the last recorded position of `1`.
19+
- If the distance is less than `k`, return `false`.
20+
- If the distance is valid (greater than or equal to `k`), update the last position to the current index.
21+
22+
3. **Return Result**: If the loop completes without finding any violations, return `true`.
23+
24+
### Data Structures:
25+
- We primarily use a simple integer variable to track the last index of `1`. No additional data structures such as arrays or lists are required, which keeps the space complexity minimal.
26+
27+
### Complexity:
28+
- **Time Complexity**: O(n), where n is the length of the input array. We make a single pass through the array.
29+
- **Space Complexity**: O(1), since we only use a fixed amount of extra space regardless of the input size.
30+
31+
This approach is efficient and straightforward, making it suitable for the constraints provided in the problem.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public boolean kLengthApart(int[] nums, int k) {
3+
int lastIndex = -1;
4+
5+
for (int i = 0; i < nums.length; i++) {
6+
if (nums[i] == 1) {
7+
if (lastIndex != -1 && i - lastIndex - 1 < k) {
8+
return false;
9+
}
10+
lastIndex = i;
11+
}
12+
}
13+
14+
return true;
15+
}
16+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var kLengthApart = function(nums, k) {
2+
let lastIndex = -1;
3+
4+
for (let i = 0; i < nums.length; i++) {
5+
if (nums[i] === 1) {
6+
if (lastIndex !== -1 && i - lastIndex - 1 < k) {
7+
return false;
8+
}
9+
lastIndex = i;
10+
}
11+
}
12+
13+
return true;
14+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def kLengthApart(self, nums: List[int], k: int) -> bool:
3+
last_position = -1
4+
5+
for i in range(len(nums)):
6+
if nums[i] == 1:
7+
if last_position != -1 and i - last_position <= k:
8+
return False
9+
last_position = i
10+
11+
return True

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,3 +281,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
281281
- 2025-11-14 — [Increment Submatrices by One](https://leetcode.com/problems/increment-submatrices-by-one/) (Medium) → `Medium/2025-11-14-2536-Increment-Submatrices-by-One`
282282
- 2025-11-15 — [Count the Number of Substrings With Dominant Ones](https://leetcode.com/problems/count-the-number-of-substrings-with-dominant-ones/) (Medium) → `Medium/2025-11-15-3234-Count-the-Number-of-Substrings-With-Dominant-Ones`
283283
- 2025-11-16 — [Number of Substrings With Only 1s](https://leetcode.com/problems/number-of-substrings-with-only-1s/) (Medium) → `Medium/2025-11-16-1513-Number-of-Substrings-With-Only-1s`
284+
- 2025-11-17 — [Check If All 1's Are at Least Length K Places Away](https://leetcode.com/problems/check-if-all-1s-are-at-least-length-k-places-away/) (Easy) → `Easy/2025-11-17-1437-Check-If-All-1-s-Are-at-Least-Length-K-Places-Away`

0 commit comments

Comments
 (0)