{"id":8746,"date":"2021-11-20T23:48:43","date_gmt":"2021-11-21T07:48:43","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8746"},"modified":"2021-11-20T23:52:56","modified_gmt":"2021-11-21T07:52:56","slug":"leetcode-2078-two-furthest-houses-with-different-colors","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-2078-two-furthest-houses-with-different-colors\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2078. Two Furthest Houses With Different Colors"},"content":{"rendered":"\n<p>There are&nbsp;<code>n<\/code>&nbsp;houses evenly lined up on the street, and each house is beautifully painted. You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>colors<\/code>&nbsp;of length&nbsp;<code>n<\/code>, where&nbsp;<code>colors[i]<\/code>&nbsp;represents the color of the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;house.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>maximum<\/strong>&nbsp;distance between&nbsp;<strong>two<\/strong>&nbsp;houses with&nbsp;<strong>different<\/strong>&nbsp;colors<\/em>.<\/p>\n\n\n\n<p>The distance between the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;and&nbsp;<code>j<sup>th<\/sup><\/code>&nbsp;houses is&nbsp;<code>abs(i - j)<\/code>, where&nbsp;<code>abs(x)<\/code>&nbsp;is the&nbsp;<strong>absolute value<\/strong>&nbsp;of&nbsp;<code>x<\/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\/2021\/10\/31\/eg1.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> colors = [<strong>1<\/strong>,1,1,<strong><u>6<\/u><\/strong>,1,1,1]\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> In the above image, color 1 is blue, and color 6 is red.\nThe furthest two houses with different colors are house 0 and house 3.\nHouse 0 has color 1, and house 3 has color 6. The distance between them is abs(0 - 3) = 3.\nNote that houses 3 and 6 can also produce the optimal answer.\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\/2021\/10\/31\/eg2.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> colors = [<strong>1<\/strong>,8,3,8,<strong>3<\/strong>]\n<strong>Output:<\/strong> 4\n<strong>Explanation:<\/strong> In the above image, color 1 is blue, color 8 is yellow, and color 3 is green.\nThe furthest two houses with different colors are house 0 and house 4.\nHouse 0 has color 1, and house 4 has color 3. The distance between them is abs(0 - 4) = 4.\n<\/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> colors = [<strong>0<\/strong>,<strong><u>1<\/u><\/strong>]\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> The furthest two houses with different colors are house 0 and house 1.\nHouse 0 has color 0, and house 1 has color 1. The distance between them is abs(0 - 1) = 1.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>n ==&nbsp;colors.length<\/code><\/li><li><code>2 &lt;= n &lt;= 100<\/code><\/li><li><code>0 &lt;= colors[i] &lt;= 100<\/code><\/li><li>Test data are generated such that&nbsp;<strong>at least<\/strong>&nbsp;two houses have different colors.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Brute Force<\/strong><\/h2>\n\n\n\n<p>Try all pairs.<br>Time complexity: O(n<sup>2<\/sup>)<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  int maxDistance(vector<int>& colors) {\n    const int n = colors.size();\n    for (int d = n - 1; d > 0; --d)\n      for (int i = 0; i + d < n; ++i)\n        if (colors[i] != colors[i + d])\n          return d;\n    return 0;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: Greedy \/ One pass<\/strong><\/h2>\n\n\n\n<p>First house or last house must be involved in the ans.<\/p>\n\n\n\n<p>Scan the house and check with first and last house.<\/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  int maxDistance(vector<int>& colors) {\n    int ans = 0;\n    const int n = colors.size();\n    for (int i = 0; i < n; ++i)\n      ans = max({ans, \n                 i * (colors[i] != colors[0]), \n                 (n - i - 1) * (colors[i] != colors[n - 1])});\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>There are&nbsp;n&nbsp;houses evenly lined up on the street, and each house is beautifully painted. You are given a&nbsp;0-indexed&nbsp;integer array&nbsp;colors&nbsp;of length&nbsp;n, where&nbsp;colors[i]&nbsp;represents the color of the&nbsp;ith&nbsp;house.&#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,88],"class_list":["post-8746","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-easy","tag-greedy","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8746","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=8746"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8746\/revisions"}],"predecessor-version":[{"id":8749,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8746\/revisions\/8749"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8746"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8746"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8746"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}