1848. Minimum Distance to the Target Element Easy
1/**
2 * [1848] Minimum Distance to the Target Element
3 *
4 * Given an integer array nums (0-indexed) and two integers target and start, find an index i such that nums[i] == target and abs(i - start) is minimized. Note that abs(x) is the absolute value of x.
5 * Return abs(i - start).
6 * It is guaranteed that target exists in nums.
7 *
8 * Example 1:
9 *
10 * Input: nums = [1,2,3,4,5], target = 5, start = 3
11 * Output: 1
12 * Explanation: nums[4] = 5 is the only value equal to target, so the answer is abs(4 - 3) = 1.
13 *
14 * Example 2:
15 *
16 * Input: nums = [1], target = 1, start = 0
17 * Output: 0
18 * Explanation: nums[0] = 1 is the only value equal to target, so the answer is abs(0 - 0) = 0.
19 *
20 * Example 3:
21 *
22 * Input: nums = [1,1,1,1,1,1,1,1,1,1], target = 1, start = 0
23 * Output: 0
24 * Explanation: Every value of nums is 1, but nums[0] minimizes abs(i - start), which is abs(0 - 0) = 0.
25 *
26 *
27 * Constraints:
28 *
29 * 1 <= nums.length <= 1000
30 * 1 <= nums[i] <= 10^4
31 * 0 <= start < nums.length
32 * target is in nums.
33 *
34 */
35pub struct Solution {}
36
37// problem: https://leetcode.com/problems/minimum-distance-to-the-target-element/
38// discuss: https://leetcode.com/problems/minimum-distance-to-the-target-element/discuss/?currentPage=1&orderBy=most_votes&query=
39
40// submission codes start here
41
42impl Solution {
43 pub fn get_min_distance(nums: Vec<i32>, target: i32, start: i32) -> i32 {
44 0
45 }
46}
47
48// submission codes end
49
50#[cfg(test)]
51mod tests {
52 use super::*;
53
54 #[test]
55 fn test_1848() {
56 }
57}
58
Back
© 2025 bowen.ge All Rights Reserved.