{"id":8428,"date":"2021-05-02T22:58:10","date_gmt":"2021-05-03T05:58:10","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8428"},"modified":"2021-05-02T23:08:36","modified_gmt":"2021-05-03T06:08:36","slug":"leetcode-1848-minimum-distance-to-the-target-element","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-1848-minimum-distance-to-the-target-element\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1848. Minimum Distance to the Target Element"},"content":{"rendered":"\n<p>Given an integer array&nbsp;<code>nums<\/code>&nbsp;<strong>(0-indexed)<\/strong>&nbsp;and two integers&nbsp;<code>target<\/code>&nbsp;and&nbsp;<code>start<\/code>, find an index&nbsp;<code>i<\/code>&nbsp;such that&nbsp;<code>nums[i] == target<\/code>&nbsp;and&nbsp;<code>abs(i - start)<\/code>&nbsp;is&nbsp;<strong>minimized<\/strong>. Note that&nbsp;<code>abs(x)<\/code>&nbsp;is the absolute value of&nbsp;<code>x<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<code>abs(i - start)<\/code>.<\/p>\n\n\n\n<p>It is&nbsp;<strong>guaranteed<\/strong>&nbsp;that&nbsp;<code>target<\/code>&nbsp;exists in&nbsp;<code>nums<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1,2,3,4,5], target = 5, start = 3\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> nums[4] = 5 is the only value equal to target, so the answer is abs(4 - 3) = 1.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1], target = 1, start = 0\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> nums[0] = 1 is the only value equal to target, so the answer is abs(0 - 0) = 1.\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> nums = [1,1,1,1,1,1,1,1,1,1], target = 1, start = 0\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> Every value of nums is 1, but nums[0] minimizes abs(i - start), which is abs(0 - 0) = 0.\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;= 1000<\/code><\/li><li><code>1 &lt;= nums[i] &lt;= 10<sup>4<\/sup><\/code><\/li><li><code>0 &lt;= start &lt; nums.length<\/code><\/li><li><code>target<\/code>&nbsp;is in&nbsp;<code>nums<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Brute Force<\/strong><\/h2>\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 getMinDistance(vector<int>& nums, int target, int start) {\n    int ans = INT_MAX;\n    for (int i = 0; i < nums.size(); ++i)\n      if (nums[i] == target)\n        ans = min(ans, abs(i - start));    \n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an integer array&nbsp;nums&nbsp;(0-indexed)&nbsp;and two integers&nbsp;target&nbsp;and&nbsp;start, find an index&nbsp;i&nbsp;such that&nbsp;nums[i] == target&nbsp;and&nbsp;abs(i &#8211; start)&nbsp;is&nbsp;minimized. Note that&nbsp;abs(x)&nbsp;is the absolute value of&nbsp;x. Return&nbsp;abs(i &#8211; start). It is&nbsp;guaranteed&nbsp;that&nbsp;target&nbsp;exists&#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],"class_list":["post-8428","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-easy","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8428","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=8428"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8428\/revisions"}],"predecessor-version":[{"id":8430,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8428\/revisions\/8430"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8428"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8428"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8428"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}