TechNOTE

Leetcode : Check If All 1's Are at Least Length K Places Away 본문

coding

Leetcode : Check If All 1's Are at Least Length K Places Away

JU1234 2021. 1. 25. 22:30

난이도 : EASY 

Python

class Solution:
    def kLengthApart(self, nums: List[int], k: int) -> bool:
        
        cnt = -1
        for num in nums:
            if num == 1:
                if cnt != -1:
                    if cnt < k:
                        return False 
                cnt =0
                    
            else:
                if cnt != -1:
                    cnt += 1 
        return True

- time complexity O(n) 

- memory complexity O(1)

반응형

'coding' 카테고리의 다른 글

Iterm2 환경설정  (0) 2023.02.28
Add python virtualenv to jupyter kernel  (0) 2022.08.25
[React] Github page 배포하기  (0) 2021.04.06
Comments