Skip to content

Commit 7942e72

Browse files
committed
Time: 19 ms (20.63%), Space: 24.6 MB (5.74%) - LeetHub
1 parent 840642f commit 7942e72

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# time complexity: O(n)
2+
# space complexity: O(n)
3+
from typing import List
4+
5+
6+
class Solution:
7+
def kLengthApart(self, nums: List[int], k: int) -> bool:
8+
onesIdx = []
9+
for i, num in enumerate(nums):
10+
if num:
11+
onesIdx.append(i)
12+
for i in range(1, len(onesIdx)):
13+
if (onesIdx[i] - onesIdx[i - 1] - 1) < k:
14+
return False
15+
return True
16+
17+
18+
nums = [1, 0, 0, 0, 1, 0, 0, 1]
19+
k = 2
20+
print(Solution().kLengthApart(nums, k))
21+
nums = [1, 0, 0, 1, 0, 1]
22+
k = 2
23+
print(Solution().kLengthApart(nums, k))

0 commit comments

Comments
 (0)