Leetcode 1848. Minimum Distance to the Target Element
Problem Explanation
The problem asks for finding the minimum absolute distance between the given start index and target element's index in the given integer array, nums.
For example, given an array nums = [1,2,3,4,5], target = 5, and start = 3, we have only one element equal to target at index 4. So, the minimum absolute distance between 4 and 3 is |4-3| = 1. Thus, the output is 1.
Approach
The solution can be approached by iterating through the array and finding the indexes that have the target value. We then calculate the absolute difference between the current index and the given start index, and keep track of the minimum difference.
Let's understand this through an example:
nums = [1, 2, 3, 4, 5], target = 5, start = 3
1- we iterate through the array index by index.
2 - At index 4, we find the target value 5.
3 - Calculate the absolute difference between the current index (4) and start index (3), which is |4-3| = 1.
4- We keep track of this minimum difference, and after iteration, we return the minimum difference found.
Solution
C++ Solution
1class Solution {
2public:
3 int getMinDistance(vector<int>& nums, int target, int start) {
4 int minDistance = INT_MAX;
5
6 for (int i = 0; i < nums.size(); ++i) {
7 if (nums[i] == target) {
8 minDistance = min(minDistance, abs(i - start));
9 }
10 }
11
12 return minDistance;
13 }
14};
Java Solution
1class Solution {
2 public int getMinDistance(int[] nums, int target, int start) {
3 int minDistance = Integer.MAX_VALUE;
4
5 for (int i = 0; i < nums.length; ++i) {
6 if (nums[i] == target) {
7 minDistance = Math.min(minDistance, Math.abs(i - start));
8 }
9 }
10
11 return minDistance;
12 }
13}
JavaScript Solution
1class Solution {
2 getMinDistance(nums, target, start) {
3 let minDistance = Number.MAX_SAFE_INTEGER;
4
5 for (let i = 0; i < nums.length; ++i) {
6 if (nums[i] === target) {
7 minDistance = Math.min(minDistance, Math.abs(i - start));
8 }
9 }
10
11 return minDistance;
12 }
13}
Python Solution
1class Solution:
2 def getMinDistance(self, nums: List[int], target: int, start: int) -> int:
3 min_distance = float('inf')
4
5 for i in range(len(nums)):
6 if nums[i] == target:
7 min_distance = min(min_distance, abs(i - start))
8
9 return min_distance
C# Solution
1public class Solution {
2 public int GetMinDistance(int[] nums, int target, int start) {
3 int minDistance = int.MaxValue;
4
5 for (int i = 0; i < nums.Length; ++i) {
6 if (nums[i] == target) {
7 minDistance = Math.Min(minDistance, Math.Abs(i - start));
8 }
9 }
10
11 return minDistance;
12 }
13}
14```## Conclusion
15
16The problem can be solved by iterating through the given integer array and finding the target values' indexes. For each target value, we calculate the absolute difference between the current index and the given start index and keep track of the minimum difference found. After completing the iteration, we return the minimum difference.
17
18The solutions presented here can be easily translated into other programming languages, and the overall complexity of the algorithm is O(n), where n is the length of the input array.
Got a question?ย Ask the Teaching Assistantย anything you don't understand.
Still not clear? Ask in the Forum, ย Discordย orย Submitย the part you don't understand to our editors.