↓Skip to main content
  1. LeetCode/

LeetCode 3810: Minimum Operations to Reach Target Array

·2 mins· ·
LeetCode Medium Greedy Hash-Table Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-01-17

  • Submissions: Accepted (first try)

Approach
#

Choosing x changes every position whose value is x to target, whether or not they are contiguous.

  • Upper bound: one operation for each value appearing at a mismatched position fixes every mismatch. Touching an already-correct position is harmless (it becomes target again).
  • Lower bound: a mismatched position changes only when its current value is chosen. That value is nums[i] until the position changes, so every value at a mismatched position must be chosen at least once.

They match, so the answer is the count of distinct values (Greedy Template 4).

My Solution
#

class Solution:
    def minOperations(self, nums: List[int], target: List[int]) -> int:
        return len({a for a, b in zip(nums, target) if a != b})

I originally wrote len(diff) - (len(diff) - len(set(diff))), which simplifies to len(set(diff)).

Complexity
#

Time O(n), space O(n).

Mistakes to Avoid
#

  • The “contiguous segments” wording only describes which positions change; all segments of one value change together in one operation.

Interview Explanation
#

Choosing a value rewrites every position holding it, so one operation per value at a mismatched position is enough. A mismatched position only changes when its current value is chosen, so each such value needs at least one operation. The answer is the number of distinct values at mismatched positions. Time O(n).

Related

LeetCode 1838: Frequency of the Most Frequent Element
·2 mins
LeetCode Medium Sliding-Window Sorting Greedy Prefix-Sum Binary-Search Array
LeetCode 1838 note: after sorting, raising the whole window to nums[right] costs nums[right] × length − window sum; shrink the left while that exceeds k
LeetCode 2271: Maximum White Tiles Covered by a Carpet
·2 mins
LeetCode Medium Sliding-Window Greedy Sorting Prefix-Sum Binary-Search Array
LeetCode 2271 note: starting the carpet at some segment’s start is never worse; after sorting, two pointers sum the fully covered segments and count the last one partially
LeetCode 3804: Number of Centered Subarrays
·1 min
LeetCode Medium Hash-Table Enumeration Array
LeetCode 3804 note: fix the left end and extend right, adding to the sum and to a set; count the subarray when the sum is in the set
LeetCode 2958: Length of Longest Subarray With at Most K Frequency
·1 min
LeetCode Medium Sliding-Window Hash-Table Array
LeetCode 2958 note: a longest-window slide that shrinks the left whenever the newly added number appears more than k times
LeetCode 1975: Maximum Matrix Sum
·2 mins
LeetCode Medium Greedy Matrix Array
LeetCode 1975 note: minus signs can travel anywhere in pairs but the parity of the negative count never changes; with an odd count, leave one on the smallest absolute value
LeetCode 2653: Sliding Subarray Beauty
·2 mins
LeetCode Medium Sliding-Window Hash-Table Array
LeetCode 2653 note: values are only -50 to 50, so the fixed window keeps a count array, and each window accumulates upward from -50 to find the x-th smallest negative