↓Skip to main content
  1. LeetCode/

LeetCode 3375: Minimum Operations to Make Array Values Equal to K

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

Meta Data
#

First Attempt: 2025/04/09

  • Total time: 08:41.14

Thought Process
#

The problem describes a situation where we are asked to find an integer h such that all integers greater than h are identical — but this is just a misdirection. Actually, we can simplify the problem: there should be no duplicate numbers in the array, and we have to find h each time.

So the actual problem becomes: how many times do we find a number h such that h == k?

To solve this, we can use a set to track seen numbers and iterate through nums. When the current number is greater than k and not already in the set, we add it and count one operation.

After the iteration, we’ll know how many operations are needed. The only case we need to handle carefully is when a number is equal to k — this doesn’t require an extra operation. So we only need to focus on numbers greater than k.

My Approach
#

class Solution:
    def minOperations(self, nums: List[int], k: int) -> int:
        seen = set()
        op = 0
        for num in nums:
            if num > k and num not in seen:
                seen.add(num)
                op+=1
            elif num<k:
                return -1

        return op

Encountered Problems
#

  1. Some test cases failed when I tried to handle the case where num == k. I attempted to use a variable equal to record whether a num == k appeared, and returned op + (not equal). However, this added an extra 1 to the result even when unnecessary, which caused the test to fail.

Related

LeetCode 3507: Minimum Pair Removal to Sort Array I
·2 mins
LeetCode Easy Simulation Array
LeetCode 3507 note: n is tiny, so simulate; the right side of a slice assignment must be a list
LeetCode 3508: Implement Router
·3 mins
LeetCode Medium Design Binary-Search Queue Hash-Map
LeetCode 3508 note: a queue for FIFO, a set for duplicates, and an increasing time list per destination queried with bisect
LeetCode 3492: Maximum Containers on a Ship
·1 min
LeetCode Easy Math
LeetCode 3492 note: at most n² cells and at most maxWeight // w by weight, so take the smaller, using integer division
LeetCode 3493: Properties Graph
·2 mins
LeetCode Medium Graph Dfs Union-Find Hash-Map
LeetCode 3493 note: convert each row to a set, compare intersection sizes pairwise to add edges, then count components
LeetCode 3494: Find the Minimum Amount of Time to Brew Potions
·2 mins
LeetCode Medium Simulation Prefix-Sum Array
LeetCode 3494 note: for each potion, push forward to get the last wizard’s finish time (everyone must be free), then subtract backward to get each wizard’s finish time
LeetCode 1431: Kids With the Greatest Number of Candies
·1 min
LeetCode Easy Array
LeetCode 1431 note: compute the maximum once; a kid is True when their candies plus the extra reach >= the maximum, since a tie counts