LeetCode 1090. Largest Values From Labels

Problem Description: We have a set of items: the i-th item has value values[i] and label labels[i].

Then, we choose a subset S of these items, such that:

  • |S| <= num_wanted
  • For every label L, the number of items in S with label L is <= use_limit.

Return the largest possible sum of the subset S.

Example 1:

Input: values = [5,4,3,2,1], labels = [1,1,2,2,3], num_wanted = 3, use_limit = 1
Output: 9
Explanation: The subset chosen is the first, third, and fifth item.

Example 2:

Input: values = [5,4,3,2,1], labels = [1,3,3,3,2], num_wanted = 3, use_limit = 2
Output: 12
Explanation: The subset chosen is the first, second, and third item.

今天周赛这道题没做出来,当时想到了才有贪心的思想,但是想得过于复杂,想对每一个label单独做一个优先队列,用一个map来映射label和对应的优先队列,另一个map来映射label和个数。后来想到其实一个优先队列和一个记录label对应个数的map就可以解决。代码如下:
public int largestValsFromLabels(int[] values, int[] labels, int num_wanted, int use_limit) {
        PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> (values[b] - values[a]));
        for(int i = 0; i < values.length; i++) {
            pq.offer(i);
        }
        Map<Integer, Integer> labelCounter = new HashMap<>();
        int res = 0;
        while(!pq.isEmpty() && num_wanted > 0) {
            int index = pq.poll();
            int value = values[index];
            int label = labels[index];

            if(labelCounter.getOrDefault(label, 0) < use_limit) {
                res += value;
                num_wanted--;
                labelCounter.put(label, labelCounter.getOrDefault(label, 0) + 1);
            } else {
                continue;
            }
        }
        return res;
    }


原文地址:https://www.cnblogs.com/rookielet/p/11037560.html

时间: 2024-10-29 11:14:54

LeetCode 1090. Largest Values From Labels的相关文章

【leetcode】1090. Largest Values From Labels

题目如下: We have a set of items: the i-th item has value values[i] and label labels[i]. Then, we choose a subset S of these items, such that: |S| <= num_wanted For every label L, the number of items in Swith label L is <= use_limit. Return the largest

LeetCode[Sort]: Largest Number

LeetCode[Sort]: Largest Number Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. Note: The result may be very large, so you need to

[LeetCode OJ] Largest Rectangle in Histogram

Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3]. The largest

LeetCode:Largest Number

1.题目名称 Largest Number(求整型数组中各元素可拼合成的最大数字) 2.题目地址 https://leetcode.com/problems/largest-number/ 3.题目内容 英文:Given a list of non negative integers, arrange them such that they form the largest number. 中文:给出一组非负整数,求这些非负整数可以拼接出的最大数字 说明:例如,给出数组 [3, 30, 34,

8.15 [LeetCode] 179 Largest Number

[LeetCode 179] Largest Number | COMMENTS Question link Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. Note: The result may be ver

leetcode Kth Largest Element in an Array

题目连接 https://leetcode.com/problems/kth-largest-element-in-an-array/ Kth Largest Element in an Array Description Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct eleme

[LeetCode] Find Largest Value in Each Tree Row 找树每行最大的结点值

You need to find the largest value in each row of a binary tree. Example: Input: 1 / 3 2 / \ \ 5 3 9 Output: [1, 3, 9] 这道题让我们找二叉树每行的最大的结点值,那么实际上最直接的方法就是用层序遍历,然后在每一层中找到最大值,加入结果res中即可,参见代码如下: 解法一: class Solution { public: vector<int> largestValues(Tre

leetCode 179. Largest Number 字符串排序 | Medium

179. Largest Number Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. Note: The result may be very large, so you need to return a st

[LeetCode] Kth Largest Element in an Array 数组中第k大的数字

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. For example, Given [3,2,1,5,6,4] and k = 2, return 5. Note: You may assume k is always valid, 1 ≤ k ≤ array'