{"id":6698,"date":"2020-05-03T20:59:16","date_gmt":"2020-05-04T03:59:16","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6698"},"modified":"2020-05-03T20:59:45","modified_gmt":"2020-05-04T03:59:45","slug":"leetcode-1437-check-if-all-1s-are-at-least-length-k-places-away","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-1437-check-if-all-1s-are-at-least-length-k-places-away\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1437. Check If All 1&#8217;s Are at Least Length K Places Away"},"content":{"rendered":"\n<p>Given an array&nbsp;<code>nums<\/code>&nbsp;of 0s and 1s and an integer&nbsp;<code>k<\/code>, return&nbsp;<code>True<\/code>&nbsp;if all 1&#8217;s are at least&nbsp;<code>k<\/code>&nbsp;places away from each other, otherwise return&nbsp;<code>False<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/04\/15\/sample_1_1791.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1,0,0,0,1,0,0,1], k = 2\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong> Each of the 1s are at least 2 places away from each other.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/04\/15\/sample_2_1791.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1,0,0,1,0,1], k = 2\n<strong>Output:<\/strong> false\n<strong>Explanation: <\/strong>The second 1 and third 1 are only one apart from each other.<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1,1,1,1,1], k = 0\n<strong>Output:<\/strong> true\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [0,1,0,1], k = 1\n<strong>Output:<\/strong> true\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums.length &lt;= 10^5<\/code><\/li><li><code>0 &lt;= k &lt;= nums.length<\/code><\/li><li><code>nums[i]<\/code>&nbsp;is&nbsp;<code>0<\/code>&nbsp;or&nbsp;<code>1<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Scan the array<\/strong><\/h2>\n\n\n\n<p>Only need to check adjacent ones. This problem should be easy instead of medium.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  bool kLengthApart(vector<int>& nums, int k) {\n    int d = k + 1; \/\/ distant enough\n    for (int n : nums) {\n      if (n == 0) {\n        ++d;\n      } else {\n        if (d < k) return false;\n        d = 0;\n      }\n    }\n    return true;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an array&nbsp;nums&nbsp;of 0s and 1s and an integer&nbsp;k, return&nbsp;True&nbsp;if all 1&#8217;s are at least&nbsp;k&nbsp;places away from each other, otherwise return&nbsp;False. Example 1: Input: nums&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[20,130,177],"class_list":["post-6698","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-distance","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6698","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=6698"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6698\/revisions"}],"predecessor-version":[{"id":6700,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6698\/revisions\/6700"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6698"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6698"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6698"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}