Skip to content

Latest commit

 

History

History
32 lines (27 loc) · 772 Bytes

Question_1437.md

File metadata and controls

32 lines (27 loc) · 772 Bytes

LeetCode Records - Question 1437 Check If All 1's Are at Least Length K Places Away

Attempt 1: Record the last index of 1

class Solution {
    public boolean kLengthApart(int[] nums, int k) {
        int prevIndex = nums.length;

        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == 1) {
                prevIndex = i;
                break;
            }
        }

        for (int i = prevIndex + 1; i < nums.length; i++) {
            if (nums[i] == 1) {
                if (i - prevIndex <= k) {
                    return false;
                }
                prevIndex = i;
            }
        }

        return true;
    }
}
  • Runtime: 1 ms (Beats: 100.00%)
  • Memory: 56.55 MB (Beats: 37.27%)