{"id":2910,"date":"2018-06-11T22:39:42","date_gmt":"2018-06-12T05:39:42","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2910"},"modified":"2018-06-11T22:40:18","modified_gmt":"2018-06-12T05:40:18","slug":"leetcode-849-maximize-distance-to-closest-person","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-849-maximize-distance-to-closest-person\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 849. Maximize Distance to Closest Person"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>In a row of\u00a0<code>seats<\/code>,\u00a0<code>1<\/code>\u00a0represents a person sitting in that seat, and\u00a0<code>0<\/code>\u00a0represents that the seat is empty.<\/p>\n<p>There is at least one empty seat, and at least one person sitting.<\/p>\n<p>Alex wants to sit in the seat such that the distance between him and the closest person to him is maximized.<\/p>\n<p>Return that maximum distance to closest person.<\/p>\n<div>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-1-1\">[1,0,0,0,1,0,1]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">2<\/span>\r\n<strong>Explanation: <\/strong>\r\nIf Alex sits in the second open seat (seats[2]), then the closest person has distance 2.\r\nIf Alex sits in any other open seat, the closest person has distance 1.\r\nThus, the maximum distance to the closest person is 2.<\/pre>\n<div>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-2-1\">[1,0,0,0]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-2\">3<\/span>\r\n<strong>Explanation: <\/strong>\r\nIf Alex sits in the last seat, the closest person is 3 seats away.\r\nThis is the maximum distance possible, so the answer is 3.\r\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ol>\n<li><code>1 &lt;= seats.length &lt;= 20000<\/code><\/li>\n<li><code>seats<\/code>\u00a0contains only 0s or 1s, at least one\u00a0<code>0<\/code>, and at least one\u00a0<code>1<\/code>.<\/li>\n<\/ol>\n<h1><strong>Solution<\/strong><\/h1>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(1)<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 15 ms\r\nclass Solution {\r\npublic:\r\n  int maxDistToClosest(vector&lt;int&gt;&amp; seats) {\r\n    int n = seats.size();\r\n    int prev = -1;\r\n    int ans = 0;\r\n    for (int i = 0; i &lt;= n; ++i) {\r\n      if (i == n) ans = max(ans, n - prev - 1);\r\n      else if (seats[i]) {\r\n        if (prev == -1) ans = i;\r\n        else ans = max(ans, (i - prev) \/ 2);\r\n        prev = i;\r\n      }\r\n    }\r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem In a row of\u00a0seats,\u00a01\u00a0represents a person sitting in that seat, and\u00a00\u00a0represents that the seat is empty. There is at least one empty seat, and&#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,222,308],"class_list":["post-2910","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-easy","tag-scan","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2910","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=2910"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2910\/revisions"}],"predecessor-version":[{"id":2913,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2910\/revisions\/2913"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2910"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2910"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2910"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}