[코테 풀이] Minimum Operations to Exceed Threshold Value I

시내·2024년 6월 12일

Q_3065) Minimum Operations to Exceed Threshold Value I

출처 : https://leetcode.com/problems/minimum-operations-to-exceed-threshold-value-i/

You are given a 0-indexed integer array nums, and an integer k.

In one operation, you can remove one occurrence of the smallest element of nums.

Return the minimum number of operations needed so that all elements of the array are greater than or equal to k.

class Solution {
    public int minOperations(int[] nums, int k) {
        List<Integer> list = new ArrayList<>();
        for (int a : nums) list.add(a);
        Collections.sort(list, Comparator.reverseOrder());
        int res = 0;
        boolean flag = true;
        while (flag) {
            boolean flag2 = true;
            for (int n = 0; n < list.size(); n++) {
                if (list.get(n) < k) {
                    res++;
                    list.remove(list.size() - 1);
                    flag2 = false;
                    break;
                }
            }
            if (flag2) {
                return res;
            }
        }
        return res;
    }
}
profile
contact 📨 ksw08215@gmail.com

0개의 댓글