-
Notifications
You must be signed in to change notification settings - Fork 0
/
check-if-all-1s-are-at-least-length-k-places-away_1437.py
47 lines (35 loc) · 1.38 KB
/
check-if-all-1s-are-at-least-length-k-places-away_1437.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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.
# Example 1:
# 
# Input: nums = [1,0,0,0,1,0,0,1], k = 2
# Output: true
# Explanation: Each of the 1s are at least 2 places away from each other.
# Example 2:
# 
# Input: nums = [1,0,0,1,0,1], k = 2
# Output: false
# Explanation: The second 1 and third 1 are only one apart from each other.
# ---------------------------------------Runtime 559 ms Beats 49.41% Memory 22.3 MB Beats 7.99%---------------------------------------
from typing import List
# My Solution 1
# Time Complexity O(n)
class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
units = [x for x in range(len(nums)) if nums[x] == 1]
for x in range(len(units) - 1):
if units[x + 1] - units[x] <= k:
return False
return True
# ---------------------------------------Runtime 559 ms Beats 49.41% Memory 22.2 MB Beats 8.88%---------------------------------------
# My Solution 1
# Time Complexity O(n)
class Solution:
def kLengthApart(self, nums: list[int], k: int) -> bool:
units = [x for x in range(len(nums)) if nums[x] == 1]
return all(
map(
lambda x: False if units[x + 1] - units[x] <= k else True,
range(len(units) - 1),
)
)