[LeetCode] 3375. Minimum Operations to Make Array Values Equal to K (Python)

2025. 4. 9. 14:30·알고리즘/LeetCode

 

 

난이도: Easy

Problem Description


You are given an integer array nums and an integer k.

 

An integer h is called valid if all values in the array that are strictly greater than h are identical.

 

For example, if nums = [10, 8, 10, 8], a valid integer is h = 9 because all nums[i] > 9 are equal to 10, but 5 is not a valid integer.

 

You are allowed to perform the following operation on nums:

  • Select an integer h that is valid for the current values in nums.
  • For each index i where nums[i] > h, set nums[i] to h.

Return the minimum number of operations required to make every element in nums equal to k. If it is impossible to make all elements equal to k, return -1.

 

Problem Example


Example 1:

Input: nums = [5,2,5,4,5], k = 2

Output: 2

Explanation:

The operations can be performed in order using valid integers 4 and then 2.

Example 2:

Input: nums = [2,1,2], k = 2

Output: -1

Explanation:

It is impossible to make all the values equal to 2.

Example 3:

Input: nums = [9,7,5,3], k = 1

Output: 4

Explanation:

The operations can be performed using valid integers in the order 7, 5, 3, and 1.

 

Constraints


  • 1 <= nums.length <= 100
  • 1 <= nums[i] <= 100
  • 1 <= k <= 100

 

 

✏️ Solution


class Solution:
    def minOperations(self, nums: List[int], k: int) -> int:
        if min(nums) < k:
            return -1
        
        nums = set(nums)

        return len(nums) if k not in nums else len(nums)-1

문제를 읽었을 때 복잡해 보일 수 있지만, 좀만 생각해보면 쉽게 풀 수 있는 문제였다.

 

일단 nums의 최소값이 k보다 작으면 -1을 return한다.

 

그리고 nums의 요소를 중복을 없애기 위해 set으로 변환시켜주었다.

 

이 때, set 안에 k 값이 들어있으면 걔는 변환을 안시켜도 되기 때문에 len(nums)-1을 return하였고, k 값이 안들어 있다면 모두 변환해주어야 하기 때문에 len(nums)를 return 하여 정답을 맞을 수 있었다.

 

 

⚙️ Runtime & Memory


 

 


문제: 3375. Minimum Operations to Make Array Values Equal to K

 

깃허브: github

 

algorithmPractice/LeetCode/3621-minimum-operations-to-make-array-values-equal-to-k at main · laewonJeong/algorithmPractice

하루 한 문제 챌린지. Contribute to laewonJeong/algorithmPractice development by creating an account on GitHub.

github.com

 

'알고리즘 > LeetCode' 카테고리의 다른 글

[LeetCode] 3396. Minimum Number of Operations to Make Elements in Array Distinct (Python)  (0) 2025.04.08
[LeetCode] 416. Partition Equal Subset Sum (C++)  (0) 2025.04.07
[LeetCode] 2873. Maximum Value of an Ordered Triplet I (Python)  (0) 2025.04.03
[LeetCode] 2140. Solving Questions With Brainpower (Python)  (0) 2025.04.01
[LeetCode] 2780. Minimum Index of a Valid Split (Python)  (0) 2025.03.27
'알고리즘/LeetCode' 카테고리의 다른 글
  • [LeetCode] 3396. Minimum Number of Operations to Make Elements in Array Distinct (Python)
  • [LeetCode] 416. Partition Equal Subset Sum (C++)
  • [LeetCode] 2873. Maximum Value of an Ordered Triplet I (Python)
  • [LeetCode] 2140. Solving Questions With Brainpower (Python)
Laewon Jeong
Laewon Jeong
  • Laewon Jeong
    래원
    Laewon Jeong
    글쓰기 | 관리
  • GitHub

    • github.com/laewonJeong
  • 전체
    오늘
    어제
    • 분류 전체보기 (172)
      • Docker 및 Kubernetes (11)
        • Docker (5)
        • Kubernetes (6)
      • Data Engineering (18)
        • Hadoop (5)
        • Spark (5)
        • Kafka (5)
        • Airflow (3)
      • CI|CD (3)
      • 알고리즘 (136)
        • 알고리즘 (2)
        • LeetCode (118)
        • 프로그래머스 (11)
        • BOJ (1)
        • 코딩테스트 대비 (4)
      • 서버 (2)
        • 미니 서버 (2)
      • 잡담 (1)
  • 태그

    BFS
    도커
    docker
    코딩테스트
    문자열
    백트래킹
    아파치 하둡
    Apache Spark
    Python
    티스토리챌린지
    분산처리
    DP
    쿠버네티스
    이진탐색
    아파치 스파크
    오블완
    우선순위큐
    프로그래머스
    String
    leetcode
    알고리즘
    programmers
    heapq
    그래프
    dfs
    누적합
    분산
    리트코드
    파이썬
    Kubernetes
  • 인기 글

  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
Laewon Jeong
[LeetCode] 3375. Minimum Operations to Make Array Values Equal to K (Python)
상단으로

티스토리툴바